ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert dynamically created Table value in Database using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nid@patel"> nid@patel</a>,</p> <p>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.</p> <p>Refer below sample for better understand and also refer below to save datatable to database.</p> <h1 class="header"><a title="SqlBulkCopy: Bulk Copy data from DataTable (DataSet) to SQL Server Table using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/SqlBulkCopy-Bulk-Copy-data-from-DataTable-DataSet-to-SQL-Server-Table-using-C-and-VBNet.aspx">SqlBulkCopy: Bulk Copy data from DataTable (DataSet) to SQL Server Table using C# and VB.Net</a></h1> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">Height:&lt;div&gt; &lt;asp:TextBox ID=&#34;TextBox1&#34; runat=&#34;server&#34; Text=&#34;200px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/div&gt; Width:&lt;div&gt; &lt;asp:TextBox ID=&#34;TextBox2&#34; runat=&#34;server&#34; Text=&#34;150px&#34;&gt;&lt;/asp:TextBox&gt;&lt;/div&gt; Row:&lt;div&gt; &lt;asp:TextBox ID=&#34;TextBox3&#34; runat=&#34;server&#34; Text=&#34;5&#34;&gt;&lt;/asp:TextBox&gt;&lt;/div&gt; Column:&lt;div&gt; &lt;asp:TextBox ID=&#34;TextBox4&#34; runat=&#34;server&#34; Text=&#34;5&#34;&gt;&lt;/asp:TextBox&gt;&lt;/div&gt; &lt;br /&gt; &lt;div id=&#34;TableHtml&#34; runat=&#34;server&#34;&gt; &lt;/div&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; Text=&#34;Modify&#34; OnClick=&#34;Button1_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Text; using System.Data; using System.Configuration; using System.Data.SqlClient; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Text Imports System.Data Imports System.Configuration Imports System.Data.SqlClient </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); StringBuilder sb = new StringBuilder(); sb.Append(&#34;&lt;table border=&#39;1&#39; height = &#39;&#34; + TextBox1.Text + &#34;&#39; width=&#39;&#34; + TextBox2.Text + &#34;&#39;&gt;&#34;); sb.Append(&#34;&lt;tr&gt;&#34;); for (int i = 0; i &lt; Convert.ToInt16(TextBox4.Text); i++) { sb.Append(&#34;&lt;th&gt;&#34; + i + &#34;&lt;/th&gt;&#34;); dt.Columns.Add(i.ToString()); } sb.Append(&#34;&lt;/tr&gt;&#34;); for (int j = 1; j &lt;= Convert.ToInt16(TextBox3.Text); j++) { sb.Append(&#34;&lt;tr&gt;&#34;); dt.Rows.Add(j, j, j, j, j); for (int k = 0; k &lt; Convert.ToInt16(TextBox4.Text); k++) { sb.Append(&#34;&lt;td&gt;&#34; + j + &#34;&lt;/td&gt;&#34;); } sb.Append(&#34;&lt;/tr&gt;&#34;); } sb.Append(&#34;&lt;/tr&gt;&lt;/table&gt;&#34;); TableHtml.InnerHtml = sb.ToString(); ViewState[&#34;dt&#34;] = dt; } } protected void Button1_Click(object sender, EventArgs e) { DataTable dt = ViewState[&#34;dt&#34;] as DataTable; string consString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(consString)) { using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) { sqlBulkCopy.DestinationTableName = &#34;dbo.tblData&#34;; con.Open(); sqlBulkCopy.WriteToServer(dt); con.Close(); } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;&lt;table border=&#39;1&#39; height = &#39;&#34; &amp; TextBox1.Text &amp; &#34;&#39; width=&#39;&#34; + TextBox2.Text &amp; &#34;&#39;&gt;&#34;) sb.Append(&#34;&lt;tr&gt;&#34;) For i As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1 sb.Append(&#34;&lt;th&gt;&#34; &amp; i &amp; &#34;&lt;/th&gt;&#34;) dt.Columns.Add(i.ToString()) Next sb.Append(&#34;&lt;/tr&gt;&#34;) For j As Integer = 1 To Convert.ToInt16(TextBox3.Text) sb.Append(&#34;&lt;tr&gt;&#34;) dt.Rows.Add(j, j, j, j, j) For k As Integer = 0 To Convert.ToInt16(TextBox4.Text) - 1 sb.Append(&#34;&lt;td&gt;&#34; &amp; j &amp; &#34;&lt;/td&gt;&#34;) Next sb.Append(&#34;&lt;/tr&gt;&#34;) Next sb.Append(&#34;&lt;/tr&gt;&lt;/table&gt;&#34;) TableHtml.InnerHtml = sb.ToString() ViewState(&#34;dt&#34;) = dt End If End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim dt As DataTable = TryCast(ViewState(&#34;dt&#34;), DataTable) Dim consString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(consString) Using sqlBulkCopy As SqlBulkCopy = New SqlBulkCopy(con) sqlBulkCopy.DestinationTableName = &#34;dbo.tblData&#34; con.Open() sqlBulkCopy.WriteToServer(dt) con.Close() End Using End Using End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/125145/Insert-dynamically-created-Table-value-in-Database-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/125145/Insert-dynamically-created-Table-value-in-Database-using-C-and-VBNet-in-ASPNet/Mon, 05 Aug 2019 06:58:49 GMT