Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder's AppendFormat Method to create Table

Tags:

c#

asp.net

I have a problem with AppendFormat method of StringBuilder class. I am creating table and appending data in string builders object to send it as mail, but when i saw mail sent by me, it does not look like table, its header and there corresponding contents are miss-placed. I want table to be separated by Lines as it generally there in microsoft word's table. how can i achieve it.. I am using following code: Body is a StringBuilder Object

if (  dic1 != null )
{
     //Body.AppendFormat("Client: " + cl + " Success. " + dic1.Count + " :");
     Body.AppendFormat("<br/><table>");
     Body.AppendFormat("<h1><tr><td>#</td><td>Files Name</td></tr></h1>");
     int count = 1;
      foreach ((KeyValuePair<string, string> pair in  dic1)
               {
                    if (!String.IsNullOrEmpty(pair.Key))
                     {

                      Body.AppendFormat("<tr><td>"+count.ToString()+"</td><td>" + pair.Key + "</td><td> " + pair.Value + "</td></tr>");
                        count++;
                        //Body.Append( );
                    }
                }

                Body.AppendFormat("</table>");

Following is output i am getting in my inbox.

 # File Name Error 
 1 txt1.txt Loading File 'txt1.txt' failed: The specified File already exists in the system 
 2 txt2.txt Loading File 'txt2.txt' failed: The specified File already exists in the system 
 3 txt3.txt Loading File 'txt3.txt' failed: The specified File already exists in the system 
like image 916
Pramod Avatar asked Oct 07 '22 16:10

Pramod


1 Answers

Hope you can use this example to understand how it should be. If not please let me know. Will help you.

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<table>");
sb.AppendFormat("<tr><td>Request Name:</td><td>{0}</td></tr>", txtBugName.Text.Trim());
sb.AppendFormat("<tr><td>Category:</td><td>{0}</td></tr>", ddlModule.SelectedValue);
sb.AppendFormat("<tr><td>Sub-Category:</td><td>{0}</td></tr>", ddlPage.SelectedValue);
sb.AppendFormat("<tr><td>Description:</td><td>{0}</td></tr>", txtComments.Text.Trim());
sb.AppendFormat("<tr><td>Email is:</td><td>{0}</td></tr>", txtemail.Text.Trim());
sb.Append("<table>");

Then I'm assuming the IsBodyHtml property is true, since you were using HTML already

like image 92
Roshana Avatar answered Oct 10 '22 03:10

Roshana