0

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

4
  • remove the +: eml = Row["Email"];? Commented Sep 22, 2017 at 16:35
  • sorry - not sure I understand the comment. I have that statement remarked but I do need the variable for the To: field in the email Commented Sep 22, 2017 at 17:26
  • Yes, you said this Email result column is the same for all rows, so if you always set eml to Row["Email"] then after foreach the Email value of the last row will be in eml and you can use it for the To: field. In your current code (except for the //) you add each row's email to eml. Commented Sep 22, 2017 at 17:29
  • Ohhh - I'm sorry. eml = Row["Email"]; gives me a 'object to string' error but if i do eml = ""+Row["Email"]; it works perfectly. Thanks so much! please make this your answer and I will give you a vote. Thank you! Commented Sep 22, 2017 at 17:40

1 Answer 1

1

Just call ToString() on the object returned by the row indexer:

eml = Row["Email"].ToString();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.