I have this nifty code that takes selected data from a sql table and adds it to an email body. It will (thanks to the help of stack overflow geniuses) return multiple records and group them in the email body very nicely. Problem is - the email field. of course, it is in the For each loop and returns multiple instances of that record - which, in turn, writes to the To field in email. This record is always the same and I only want one instance written to my variable (eml). I'm guessing this would be simple - but I am not coming up with a good solution short of performing the query again.
string str = @"Data Source=srvr;Initial Catalog=db;Integrated
Security=True";
SqlConnection scn;
SqlDataAdapter da;
DataSet ds;
salesOrdersTableAdapter.SO(_TST_TWIDataSet.SalesOrders);
scn = new SqlConnection(str);
da = new SqlDataAdapter("SELECT DISTINCT DATEADD (dd, DATEDIFF(dd,0,ShipDate),0) AS ShipDate,RTRIM(SalesOrder) AS [Sales Order], RTRIM(PartNum) AS [Part Number]," +
"RTRIM(Description) AS Description,RTRIM(DueQty) AS Quantity,RTRIM(CustPartNum) AS[Customer Part No], RTRIM(CustPo) AS[Customer PO], " +
"RTRIM(CustRev) AS[Customer Rev], RTRIM(email) AS [Email] " +
"FROM tbl WHERE Ack <> 'Y'AND SalesOrder =" + MyGlobals.ord, scn);
ds = new DataSet();da.Fill(ds, "SalesOrders");
var orderListBody = new StringBuilder();
var sbj = string.Empty;
foreach (DataRow Row in ds.Tables["SalesOrders"].Rows)
{
orderListBody.Append("Order Number " + Row["Sales Order"] + "<br />");
orderListBody.Append("Part Number: " + Row["Part Number"] + "<br />");
orderListBody.Append("Description: " + Row["Description"] + "<br />");
orderListBody.Append("Customer Part Number: " + Row["Customer Part No"] + "<br />");
orderListBody.Append("Customer Revision: " + Row["Customer Rev"] + "<br />");
DateTime dte = DateTime.Now;
orderListBody.Append("Expected Ship Date: " + dte.ToShortDateString() + "<br />");
orderListBody.Append("Quantity: " + Row["Quantity"] + "<br />");
orderListBody.Append("<br />");
// eml += Row["Email"];
sbj = "Order Acknowledgement for your PO " + Row["Customer PO"];
}
thanks in advance
+:eml = Row["Email"];?Emailresult column is the same for all rows, so if you always setemltoRow["Email"]then afterforeachtheEmailvalue of the last row will be inemland you can use it for theTo:field. In your current code (except for the//) you add each row's email toeml.