Create Table Using StringBuilder in ASP.NET From Code Behind

What is StringBuilder in C#.NET? 
 
String builder is mutable. It means once we create a string builder object we can perform any operation like insert, replace, or append without creatinga new instance every time. 
 
For Example:
  1. using System;  
  2. using System.Text;   
  3. StringBuilder MyStringBuilder = new StringBuilder("Hello World!");   
  4. MyStringBuilder.Append(" What a beautiful day.");   
  5. Console.WriteLine(MyStringBuilder);  
Output:

Hello World! What a beautiful day.

Advantages of String Builder
  • It’s mutable.
  • Performance wise string builder is high because it will use same instance of object to perform any action.
  • In String Builder we can use append keyword.
  • Stringbuilder belongs to System.Text namespace. 
Open -> MS SQL SERVER 2012 ->Create an Table in database.

 

Press F5 for execution
 
Insert data into your table
 
  
Now Create Store Procedure in your database. 
 
Press F5 For Execution
 
Test the stored procedure.
 
  
Create Connection from database------->Open web.config--------->Add below Code in Configuration.
  1. <connectionStrings>  
  2.     <add name="dbConnect" connectionString="Data Source=xxxxxxx;Initial Catalog=xxxxx;Integrated Security=True" providerName="System.data.SqlClient" />   
  3. </connectionStrings>  
Open Visual Studio 2013------>File------>New------->Website------>Websit_Name.

Right Click on Solution------>WebForm------->Default.aspx.

Add App_Code folder in Solution------>Add on class(Research).

  
 
Add one method in Research.cs   
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  7. using System.Configuration;  
  8. public static class Research  
  9. {  
  10.     public static string GetConnectionString()  
  11.     {  
  12.         string conString = ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString;  
  13.         return conString;  
  14.     }  
  15.     public static DataSet GetData(int CID, string CNAME, string CADDRESS, string CMOBILE, string OPT)  
  16.     {  
  17.         DataSet ds = new DataSet();  
  18.         try  
  19.         {  
  20.             string CS = Research.GetConnectionString();  
  21.             SqlConnection con = new SqlConnection(CS);  
  22.             SqlCommand cmd = new SqlCommand("CUSTOMER_PROC", con);  
  23.             cmd.CommandType = CommandType.StoredProcedure;  
  24.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  25.             con.Open();  
  26.             cmd.Parameters.AddWithValue("@CID", CID);  
  27.             cmd.Parameters.AddWithValue("@CNAME", Convert.ToString(CNAME));  
  28.             cmd.Parameters.AddWithValue("@CADDRESS", Convert.ToString(CADDRESS));  
  29.             cmd.Parameters.AddWithValue("@CMOBILE", Convert.ToString(CMOBILE));  
  30.             cmd.Parameters.AddWithValue("@OPT", Convert.ToString(OPT));  
  31.             da.Fill(ds);  
  32.             con.Close();  
  33.         }  
  34.         catch (Exception err)  
  35.         {  
  36.             throw err;  
  37.         }  
  38.         return ds;  
  39.     }  
  40. }  
Add one table in Default.aspx page and give id to TD and runat="Server" in body section.  
  1. <table>  
  2.       <tr>  
  3.             <td id="Grd1" runat="server">  
  4.             </td>  
  5.      </tr>  
  6. </table>  
Write code in Default.aspx.cs  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Text;  
  8. using System.Data;  
  9. public partial class _Default: System.Web.UI.Page  
  10. {  
  11.     DataSet ds = null;  
  12.     StringBuilder sb = new StringBuilder();  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         try  
  16.         {  
  17.             BindTable();  
  18.         }  
  19.         catch (Exception err)  
  20.         {  
  21.             Response.Write(err.Message);  
  22.         }  
  23.     }  
  24.     public void BindTable()  
  25.     {  
  26.         try  
  27.         {  
  28.             ds = Research.GetData(0, """""""S");  
  29.             if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)  
  30.             {  
  31.                 sb.Append("<table border='1' cellpadding='0' cellspacing='0'>");  
  32.                 sb.Append("<tr>");  
  33.                 sb.Append("<td>CID</td>");  
  34.                 sb.Append("<td>CNAME</td>");  
  35.                 sb.Append("<td>CADDRESS</td>");  
  36.                 sb.Append("<td>CMOBILE</td>");  
  37.                 sb.Append("</tr>");  
  38.                 sb.Append("</table>");  
  39.                 sb.Append("<table border='1' cellpadding='0' cellspacing='0' width='100%'>");  
  40.                 for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  41.                 {  
  42.                     sb.Append("<tr>");  
  43.                     sb.Append("<td>" + Convert.ToString(ds.Tables[0].Rows[i]["CID"] + "</td>"));  
  44.                     sb.Append("<td>" + Convert.ToString(ds.Tables[0].Rows[i]["CNAME"] + "</td>"));  
  45.                     sb.Append("<td>" + Convert.ToString(ds.Tables[0].Rows[i]["CADDRESS"] + "</td>"));  
  46.                     sb.Append("<td>" + Convert.ToString(ds.Tables[0].Rows[i]["CMOBILE"] + "</td>"));  
  47.                     sb.Append("</tr>");  
  48.                 }  
  49.                 sb.Append("</table>");  
  50.                 Grd1.InnerHtml = Convert.ToString(sb);  
  51.             }  
  52.             else  
  53.             {  
  54.                 Response.Write("NO DATA FOUND");  
  55.             }  
  56.         }  
  57.         catch (Exception err)  
  58.         {  
  59.             Response.Write(err.Message);  
  60.         }  
  61.         finally  
  62.         {  
  63.             if (ds != null)  
  64.             {  
  65.                 ds.Dispose();  
  66.             }  
  67.         }  
  68.     }  
  69. }  
Run your Application
 
  
I hope you liked this blog. Please share with me your valuable suggestions and feedback.