Insert ASP.Net GridView data to Database Error: Column Codeitem does not belong to table Table1

Last Reply 18 days ago By pandeyism

Posted 18 days ago

When i am inserting data from gridview to database then exception is coming that 

(Column 'Codeitem' does not belong to table Table1.)

i am trying to insert codeitem into database not description of item 

here is my code of inserting data into table.

//Save Data Into Gridview//
protected void GVadd_Click(object sender, EventArgs e)
{
    dt = ViewState["dt"] as DataTable;
    dr = dt.NewRow();
    //  dr["Codeitem"] = DropDownList1.SelectedValue;
    dr["Descriptionitem"] = DropDownList1.SelectedItem.Text.Trim();

    dr["QTY"] = txtqty.Text;
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    clear();
}

private void clear()
{
    // Codeitem.Text = "";
    txtqty.Text = "";
}

//Save Data into Database//
protected void Save_Click(object sender, EventArgs e)
{
    DataTable dt = (DataTable)ViewState["dt"];
    int codeitem, qty;

    foreach (DataRow row in dt.Rows)
    { 
        qty = int.Parse(row["QTY"].ToString());
        codeitem = int.Parse(row["Codeitem"].ToString());
   
        this.InsertRows(codeitem, qty);
    }
}

private void InsertRows(int codeitem, int qty)
{
    using (SqlCommand cmd = new SqlCommand("Insert_PackDetail", con))
    {
        con.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        // cmd.Parameters.AddWithValue("@PID", pid);
        cmd.Parameters.AddWithValue("@CodeItem", codeitem);
        cmd.Parameters.AddWithValue("@QTY", qty);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

 

 

Posted 18 days ago

Hi akhter,

Refer below sample.

HTML

Qty :
<asp:TextBox runat="server" ID="txtqty" />
Description :
<asp:DropDownList runat="server" ID="ddlItems">
</asp:DropDownList>
<asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
        <asp:BoundField DataField="Codeitem" HeaderText="Codeitem" />
        <asp:BoundField DataField="QTY" HeaderText="QTY" />
    </Columns>
</asp:GridView>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />

Namespaces

C#

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

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        dt.Columns.Add("Descriptionitem");
        dt.Columns.Add("Codeitem");
        dt.Columns.Add("Qty");
        DataTable dtddl = new DataTable();
        dtddl.Columns.Add("Descriptionitem");
        dtddl.Columns.Add("Codeitem");
        dtddl.Rows.Add("A", 1);
        dtddl.Rows.Add("B", 2);
        dtddl.Rows.Add("C", 3);
        ddlItems.DataSource = dtddl;
        ddlItems.DataTextField = "Descriptionitem";
        ddlItems.DataValueField = "Codeitem";
        ddlItems.DataBind();
        ddlItems.Items.Insert(0, new ListItem("Select", "0"));
        ViewState["dt"] = dt;
    }
}

protected void GVadd_Click(object sender, EventArgs e)
{
    dt = ViewState["dt"] as DataTable;
    dr = dt.NewRow();
    dr["Descriptionitem"] = ddlItems.SelectedItem.Text;
    dr["Codeitem"] = ddlItems.SelectedItem.Value;
    dr["QTY"] = txtqty.Text;
    dt.Rows.Add(dr);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

protected void Insert(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    foreach (GridViewRow row in GridView1.Rows)
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)", con);
        cmd.Parameters.AddWithValue("@Descriptionitem", row.Cells[0].Text);
        cmd.Parameters.AddWithValue("@Codeitem", row.Cells[1].Text);
        cmd.Parameters.AddWithValue("@QTY", row.Cells[2].Text);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

VB.Net

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("Descriptionitem")
        dt.Columns.Add("Codeitem")
        dt.Columns.Add("Qty")
        Dim dtddl As DataTable = New DataTable()
        dtddl.Columns.Add("Descriptionitem")
        dtddl.Columns.Add("Codeitem")
        dtddl.Rows.Add("A", 1)
        dtddl.Rows.Add("B", 2)
        dtddl.Rows.Add("C", 3)
        ddlItems.DataSource = dtddl
        ddlItems.DataTextField = "Descriptionitem"
        ddlItems.DataValueField = "Codeitem"
        ddlItems.DataBind()
        ddlItems.Items.Insert(0, New ListItem("Select", "0"))
        ViewState("dt") = dt
    End If
End Sub

Protected Sub GVadd_Click(ByVal sender As Object, ByVal e As EventArgs)
    dt = TryCast(ViewState("dt"), DataTable)
    dr = dt.NewRow()
    dr("Descriptionitem") = ddlItems.SelectedItem.Text
    dr("Codeitem") = ddlItems.SelectedItem.Value
    dr("QTY") = 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("constr").ConnectionString
    Dim con As SqlConnection = New SqlConnection(constr)
    For Each row As GridViewRow In GridView1.Rows
        Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)", con)
        cmd.Parameters.AddWithValue("@Descriptionitem", row.Cells(0).Text)
        cmd.Parameters.AddWithValue("@Codeitem", row.Cells(1).Text)
        cmd.Parameters.AddWithValue("@QTY", row.Cells(2).Text)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    Next
End Sub