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 ASP.Net GridView data to Database Error: Column Codeitem does not belong to table Table1<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">Qty : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtqty&#34; /&gt; Description : &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlItems&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;GVadd_Click&#34; /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Descriptionitem&#34; HeaderText=&#34;Descriptionitem&#34; /&gt; &lt;asp:BoundField DataField=&#34;Codeitem&#34; HeaderText=&#34;Codeitem&#34; /&gt; &lt;asp:BoundField DataField=&#34;QTY&#34; HeaderText=&#34;QTY&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Insert&#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.Data; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data 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">DataTable dt = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add(&#34;Descriptionitem&#34;); dt.Columns.Add(&#34;Codeitem&#34;); dt.Columns.Add(&#34;Qty&#34;); DataTable dtddl = new DataTable(); dtddl.Columns.Add(&#34;Descriptionitem&#34;); dtddl.Columns.Add(&#34;Codeitem&#34;); dtddl.Rows.Add(&#34;A&#34;, 1); dtddl.Rows.Add(&#34;B&#34;, 2); dtddl.Rows.Add(&#34;C&#34;, 3); ddlItems.DataSource = dtddl; ddlItems.DataTextField = &#34;Descriptionitem&#34;; ddlItems.DataValueField = &#34;Codeitem&#34;; ddlItems.DataBind(); ddlItems.Items.Insert(0, new ListItem(&#34;Select&#34;, &#34;0&#34;)); ViewState[&#34;dt&#34;] = dt; } } protected void GVadd_Click(object sender, EventArgs e) { dt = ViewState[&#34;dt&#34;] as DataTable; dr = dt.NewRow(); dr[&#34;Descriptionitem&#34;] = ddlItems.SelectedItem.Text; dr[&#34;Codeitem&#34;] = ddlItems.SelectedItem.Value; dr[&#34;QTY&#34;] = txtqty.Text; dt.Rows.Add(dr); GridView1.DataSource = dt; GridView1.DataBind(); } protected void Insert(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); foreach (GridViewRow row in GridView1.Rows) { SqlCommand cmd = new SqlCommand(&#34;INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)&#34;, con); cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, row.Cells[0].Text); cmd.Parameters.AddWithValue(&#34;@Codeitem&#34;, row.Cells[1].Text); cmd.Parameters.AddWithValue(&#34;@QTY&#34;, row.Cells[2].Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Private dt As DataTable = New DataTable() Private dr As DataRow Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then dt.Columns.Add(&#34;Descriptionitem&#34;) dt.Columns.Add(&#34;Codeitem&#34;) dt.Columns.Add(&#34;Qty&#34;) Dim dtddl As DataTable = New DataTable() dtddl.Columns.Add(&#34;Descriptionitem&#34;) dtddl.Columns.Add(&#34;Codeitem&#34;) dtddl.Rows.Add(&#34;A&#34;, 1) dtddl.Rows.Add(&#34;B&#34;, 2) dtddl.Rows.Add(&#34;C&#34;, 3) ddlItems.DataSource = dtddl ddlItems.DataTextField = &#34;Descriptionitem&#34; ddlItems.DataValueField = &#34;Codeitem&#34; ddlItems.DataBind() ddlItems.Items.Insert(0, New ListItem(&#34;Select&#34;, &#34;0&#34;)) ViewState(&#34;dt&#34;) = dt End If End Sub Protected Sub GVadd_Click(ByVal sender As Object, ByVal e As EventArgs) dt = TryCast(ViewState(&#34;dt&#34;), DataTable) dr = dt.NewRow() dr(&#34;Descriptionitem&#34;) = ddlItems.SelectedItem.Text dr(&#34;Codeitem&#34;) = ddlItems.SelectedItem.Value dr(&#34;QTY&#34;) = txtqty.Text dt.Rows.Add(dr) GridView1.DataSource = dt GridView1.DataBind() End Sub Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) For Each row As GridViewRow In GridView1.Rows Dim cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)&#34;, con) cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, row.Cells(0).Text) cmd.Parameters.AddWithValue(&#34;@Codeitem&#34;, row.Cells(1).Text) cmd.Parameters.AddWithValue(&#34;@QTY&#34;, row.Cells(2).Text) con.Open() cmd.ExecuteNonQuery() con.Close() Next End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/975287/Insert-ASPNet-GridView-data-to-Database-Error-Column-Codeitem-does-not-belong-to-table-Table1/https://www.aspforums.net:443/Threads/975287/Insert-ASPNet-GridView-data-to-Database-Error-Column-Codeitem-does-not-belong-to-table-Table1/Wed, 07 Aug 2019 02:14:01 GMT