Insert dynamically created Table value in Database using C# and VB.Net in ASP.Net

Last Reply 13 days ago By pandeyism

Posted 13 days ago

How to create a dynamic table by inserting the height width row and column in asp.net.

i want to store this values in databse.

<body>
    <form id="form1" runat="server">
        <div>
            Height : <asp:TextBox ID="txt_height" runat="server"></asp:TextBox>
        </div><hr />
        <div>
            Width : <asp:TextBox ID="txt_width" runat="server"></asp:TextBox>
        </div><hr />
        <div>
            row : <asp:TextBox ID="txt_row" runat="server"></asp:TextBox>
        </div><hr />
        <div>
            column : <asp:TextBox ID="txt_column" runat="server"></asp:TextBox>
        </div><hr />
        <div id="TableHtml" runat="server"></div>        <div>
            <asp:Button ID="Button1" runat="server" Text="Modify" OnClick="Button1_Click" />
        </div><hr />  
    </form>
</body>

 

Posted 13 days ago

Hi nid@patel,

Use datatable to resolve your problems. When adding rows and columns in stringbuilder at the same time add rows and columns in datatable also after that put dataable in viewstate and get it when you want save.

Refer below sample for better understand and also refer below to save datatable to database.

SqlBulkCopy: Bulk Copy data from DataTable (DataSet) to SQL Server Table using C# and VB.Net

HTML

Height:<div>
    <asp:TextBox ID="TextBox1" runat="server" Text="200px"></asp:TextBox></div>
Width:<div>
    <asp:TextBox ID="TextBox2" runat="server" Text="150px"></asp:TextBox></div>
Row:<div>
    <asp:TextBox ID="TextBox3" runat="server" Text="5"></asp:TextBox></div>
Column:<div>
    <asp:TextBox ID="TextBox4" runat="server" Text="5"></asp:TextBox></div>
<br />
<div id="TableHtml" runat="server">
</div>
<asp:Button ID="Button1" runat="server" Text="Modify" OnClick="Button1_Click" />

Namespaces

C#

using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Text
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        StringBuilder sb = new StringBuilder();
        sb.Append("<table border='1' height = '" + TextBox1.Text + "' width='" + TextBox2.Text + "'>");
        sb.Append("<tr>");
        for (int i = 0; i < Convert.ToInt16(TextBox4.Text); i++)
        {
            sb.Append("<th>" + i + "</th>");
            dt.Columns.Add(i.ToString());
        }
        sb.Append("</tr>");
        for (int j = 1; j <= Convert.ToInt16(TextBox3.Text); j++)
        {
            sb.Append("<tr>");
            dt.Rows.Add(j, j, j, j, j);
            for (int k = 0; k < Convert.ToInt16(TextBox4.Text); k++)
            {
                sb.Append("<td>" + j + "</td>");
            }
            sb.Append("</tr>");
        }
        sb.Append("</tr></table>");
        TableHtml.InnerHtml = sb.ToString();
        ViewState["dt"] = dt;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = ViewState["dt"] as DataTable;
    string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(consString))
    {
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
        {
            sqlBulkCopy.DestinationTableName = "dbo.tblData";
            con.Open();
            sqlBulkCopy.WriteToServer(dt);
            con.Close();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        Dim sb As StringBuilder = New StringBuilder()
        sb.Append("<table border='1' height = '" & TextBox1.Text & "' width='" + TextBox2.Text & "'>")
        sb.Append("<tr>")
        For i As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1
            sb.Append("<th>" & i & "</th>")
            dt.Columns.Add(i.ToString())
        Next
        sb.Append("</tr>")
        For j As Integer = 1 To Convert.ToInt16(TextBox3.Text)
            sb.Append("<tr>")
            dt.Rows.Add(j, j, j, j, j)
            For k As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1
                sb.Append("<td>" & j & "</td>")
            Next
            sb.Append("</tr>")
        Next
        sb.Append("</tr></table>")
        TableHtml.InnerHtml = sb.ToString()
        ViewState("dt") = dt
    End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
    Dim consString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(consString)
        Using sqlBulkCopy As SqlBulkCopy = New SqlBulkCopy(con)
            sqlBulkCopy.DestinationTableName = "dbo.tblData"
            con.Open()
            sqlBulkCopy.WriteToServer(dt)
            con.Close()
        End Using
    End Using
End Sub