I have a grid view in asp.net which I am populating like this:
sda1 = new SqlDataAdapter("select * from tran_dtls where tc='" + tcdropDown.SelectedValue + "'and doc_no='" + DocNoTxt.Text + "'", con);
build = new SqlCommandBuilder(sda1);
sda1.Fill(ds); //ds is a data set
GridView1.DataSource = ds;
GridView1.DataBind();
I update the grid view by passing the ds to a new data table, adding a new row in datatable then again binding the datatable to the grid:
dt = (DataTable)ViewState["myViewState"];
dt = ds.Tables[0];
dr = dt.NewRow();
dr["TC"] = Session["TC"];
dr["sr_no"] = Session["i"].ToString(); //I have more fields but for simplicity only displaying few
dt.Rows.Add(dr);
dt.AcceptChanges();
ViewState["myViewState"] = dt;
Session["gridRow"] = dt;
Everything is smooth uptil now ,but when I try to update the database(after inserting new entries in the dataset) it doesn’t work the save button code is below:
protected void onSaveClick(object sender, EventArgs e)
{
dt = (DataTable)Session["gridRow"];// I am passing the data table in a session as I am entering the new entry from a pop up window
dt.GetChanges(DataRowState.Modified);
sda1.Fill(dt);
sda1.Update(dt);
}
Funny part is when I edit a the existing entry in dt it does edit(classic sql update) but it never insert any new entries which are added in the data table ,and it doesn’t even throw a error
This line
marks all rows in the datatable as Unchanged.
When you call
you get only the rows modified after the AcceptChanges()
Moreover, to get the rows Added to the database the correct statement will be
but, as I have said, this will return nothing due to the AcceptChanges() call.
You should remove the AcceptChanges() statement.
Also, I don’t think you need to call this in the logic of the save button