0

I have made a small purchasing application. The user selects a couple of Orders and clicks 'SEND EMAIL'. Email gets created with data from the database for those specific orders.

This is how I am doing it right now:

string mailbod = "Following are Orders that need your attention: ";  
mailbod=od.mailbody(orderid,mailbod);//Calling the method that sets up the string  

public string mailbody(List<int>oids,string mailbod)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            string output = "";
            using (SqlConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("TESTDB")))
            {
                StringBuilder sb = new StringBuilder("Select Distinct a.VEND_NAME,b.*  from dbo.Purch_Vendor a inner join dbo.purch_order b on a.VENDOR_ID=b.VENDOR_ID left join dbo.purch_item c on b.ORDER_ID=c.ORDER_ID Where ");
                if (oids.Count > 0)
                {
                    foreach(int x in oids)
                    {
                        sb.Append("b.ORDER_ID" + "=" +  x  + " OR ");
                    }
                    sb.Length--;
                    sb.Length--;
                    sb.Length--;

                    SqlCommand command = new SqlCommand(sb.ToString(), connection);
                    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(command);
                    DataTable dtRecord = new DataTable();
                    sqlDataAdap.Fill(dtRecord);

                    foreach (DataRow x in dtRecord.Rows)
                    {
                        //' Loop through each column. '
                        for (int i=0;i<dtRecord.Columns.Count; i++)
                        {// ' Output the value of each column's data.

                            sw.Write(x[i].ToString() + ", ");
                        }
                        output = sw.ToString();
                        //' Trim off the trailing ", ", so the output looks correct. '
                        if (output.Length > 2)
                        {
                            output = output.Substring(0, output.Length - 2);
                        }
                        //' Display the row in the console window. '
                        Console.WriteLine(output);
                    }

                    return output;
                }
                else
                {
                    return "";
                }
            }
        }  

The output that gets emailed looks like this:

Amazon, 2, 1, 2/20/2020 12:00:00 AM, kjhgg, 34.400, N, awefwef

The output I would like to get:
Following are the Orders that need your attention:

Just an actual table format of the above text

How can I get this to work as close as possible? Any ideas appreciated

SQLserving
  • 380
  • 1
  • 4
  • 16
  • Firstly, split this down so one method has one responsibility. Have one method return the datatable and then pass this to a method to get the string. Can the email you are sending use HTML as this could be formatted to a table quite easily, or are you restricted to using plain text? – Ryan Thomas Feb 26 '20 at 13:20
  • @RyanThomas Thanks for the suggestion, will clean that up. I am using the email code that I found here: [link](https://stackoverflow.com/a/42743804/7405312). I don't think it accepts HTML. – SQLserving Feb 26 '20 at 13:27
  • Whether that answer *accepts HTML* is irrelevant. Can you use HTML in your email message? If so, then it's easy to produce tabular data. If not, it's much more difficult. – Ken White Feb 26 '20 at 13:32
  • @KenWhite Yes, I can use either – SQLserving Feb 26 '20 at 13:39
  • 2
    Then change your code to output the proper HTML for a table (``, etc.) to contain the data. – Ken White Feb 26 '20 at 13:41
  • 1
    I agree with Ken, just build up a HTML string and set this as the body. Your email message class may have a property called UseHtml or something similar which you may have to set to true. Even better you could create a .html file as a template and then have some place holder text you replace with the rows for the table. There's probably a third-party package that does something like this. – Ryan Thomas Feb 26 '20 at 13:55
  • Thank you guys for pointing me in the right direction. I dont think i even need to post an answer since that would be a duplicate. I got it to work. – SQLserving Feb 26 '20 at 14:03
  • Glad to hear you sorted it, was going to post a full example with template, but don't think it is required now. :) – Ryan Thomas Feb 26 '20 at 16:14

0 Answers0