Last post Aug 07, 2019 08:50 AM by Yang Shen
Member
116 Points
274 Posts
Aug 07, 2019 06:29 AM|akhterr|LINK
when i am inserting data into database it raising exception that Column 'Codeitem' does not belong to table Table1.
i want to insert code item in my table not description item ....so how to tackle this issue...
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication1 { public partial class WebForm2 : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=SPS;MultipleActiveResultSets=True;"); DataTable dt = new DataTable(); DataRow dr; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add("Descriptionitem"); // dt.Columns.Add("Descriptionitem"); dt.Columns.Add("QTY"); ViewState["dt"] = dt; itemload(); } } private void itemload() { con.Open(); SqlDataAdapter adpr1 = new SqlDataAdapter("select * from ItemMasterFile ", con); DataSet dspr1 = new DataSet(); adpr1.Fill(dspr1); DropDownList1.DataSource = dspr1.Tables[0]; DropDownList1.DataTextField = "Descriptionitem"; DropDownList1.DataValueField = "Codeitem"; DropDownList1.DataBind(); } //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(); } } } }
Contributor
3140 Points
983 Posts
Aug 07, 2019 07:24 AM|Yang Shen|LINK
Hi akhterr,
Nice to see you again.
You are missing dt.Columns.Add("Codeitem"); in your Page_Load event.
Please refer to below:
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add("Descriptionitem"); dt.Columns.Add("Codeitem"); dt.Columns.Add("QTY"); ViewState["dt"] = dt; itemload(); } }
Best Regard,
Yang Shen
Aug 07, 2019 08:00 AM|akhterr|LINK
giving error on that (Input string was not in a correct format.)
codeitem = int.Parse(row["Codeitem"].ToString());
Note : i did not mentioned codeitem column in gridview?
All-Star
48500 Points
18071 Posts
Aug 07, 2019 08:22 AM|PatriceSc|LINK
Hi,
You fixed the previous error ?
Look at the value for row["Codeitem"].ToString(). It means that this value can't be parsed to an int. If the db side column is already an int you could cast directly the column or use https://docs.microsoft.com/en-us/dotnet/api/system.data.datarowextensions?view=netframework-4.8 to write direcly:
codeitem=row.Field<int>("Codeitem");
More likely this is a (n)varchar column which is supposed to contain integers (but apparently not always) ?
Not directly related but if starting with web forms you may want to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data which is late to the party (new in 4.5) but can be interesting compared with older approach...
Aug 07, 2019 08:50 AM|Yang Shen|LINK
akhterr 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(); }
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(); }
// dr["Codeitem"] = DropDownList1.SelectedValue;
It seems you comment on this code?
dt.Columns.Add("CodeItem"); will add your datable a new CodeItem column but with null value which you can't convert to int.
Please cancel the comment and try again.
Member
116 Points
274 Posts
When i inserting data from gridview to database error coming(Column 'Codeitem' does not belong to...
Aug 07, 2019 06:29 AM|akhterr|LINK
when i am inserting data into database it raising exception that Column 'Codeitem' does not belong to table Table1.
i want to insert code item in my table not description item ....so how to tackle this issue...
Contributor
3140 Points
983 Posts
Re: When i inserting data from gridview to database error coming(Column 'Codeitem' does not belon...
Aug 07, 2019 07:24 AM|Yang Shen|LINK
Hi akhterr,
Nice to see you again.
You are missing dt.Columns.Add("Codeitem"); in your Page_Load event.
Please refer to below:
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { dt.Columns.Add("Descriptionitem"); dt.Columns.Add("Codeitem"); dt.Columns.Add("QTY"); ViewState["dt"] = dt; itemload(); } }
Best Regard,
Yang Shen
Member
116 Points
274 Posts
Re: When i inserting data from gridview to database error coming(Column 'Codeitem' does not belon...
Aug 07, 2019 08:00 AM|akhterr|LINK
giving error on that (Input string was not in a correct format.)
codeitem = int.Parse(row["Codeitem"].ToString());
Note : i did not mentioned codeitem column in gridview?
All-Star
48500 Points
18071 Posts
Re: When i inserting data from gridview to database error coming(Column 'Codeitem' does not belon...
Aug 07, 2019 08:22 AM|PatriceSc|LINK
Hi,
You fixed the previous error ?
Look at the value for row["Codeitem"].ToString(). It means that this value can't be parsed to an int. If the db side column is already an int you could cast directly the column or use https://docs.microsoft.com/en-us/dotnet/api/system.data.datarowextensions?view=netframework-4.8 to write direcly:
codeitem=row.Field<int>("Codeitem");
More likely this is a (n)varchar column which is supposed to contain integers (but apparently not always) ?
Not directly related but if starting with web forms you may want to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data which is late to the party (new in 4.5) but can be interesting compared with older approach...
Contributor
3140 Points
983 Posts
Re: When i inserting data from gridview to database error coming(Column 'Codeitem' does not belon...
Aug 07, 2019 08:50 AM|Yang Shen|LINK
Hi akhterr,
// dr["Codeitem"] = DropDownList1.SelectedValue;
It seems you comment on this code?
dt.Columns.Add("CodeItem"); will add your datable a new CodeItem column but with null value which you can't convert to int.
Please cancel the comment and try again.
Best Regard,
Yang Shen