I am inserting data into Database from GridView, after CheckBox checked rows, but data is not inserted into the Database.
How to use try and catch?
SqlCommand cmdbno = new SqlCommand("[Sp_all_items_SB_order]", con);
cmdbno.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmdbno))
{
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
this.GVSO.DataSource = dt;
this.GVSO.DataBind();
ViewState["dt"] = dt;
}
cmdbno.Dispose();
con.Close();
}
Inserting code
DataTable dt = (DataTable)ViewState["dt"];
// int codeitem, orderqty;
foreach (GridViewRow row in dt.Rows)
{
if ((row.FindControl("chkRows") as CheckBox).Checked)
{
string codeitem = row.Cells[1].Text;
string orderqty = row.Cells[3].Text;
this.Insert(codeitem, orderqty);
}
}
private void Insert(string codeitem, string orderqty)
{
con.Open();
using (SqlCommand cmd = new SqlCommand("Sp_OrderDetail_CRUD", con))
{
cmd.Parameters.AddWithValue("@Action", "INSERT");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Orderno", Orderno);
cmd.Parameters.AddWithValue("@CodeItem", codeitem);
cmd.Parameters.AddWithValue("@orderqty", orderqty);
cmd.ExecuteNonQuery();
con.Close();
}
}