This is my table:
Student:StudentId int PK autoincrement,Name varchar(20)
When i am trying to update last added records then i am geting error:
Error: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
This is my code:
using (var connection = new SqlConnection("MyConnectionstring"))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.SelectCommand = new SqlCommand("select * from Student", connection);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataRow row = dt.NewRow();
row["Name"] = "Abc";
dt.Rows.Add(row);
var addedRecords = dt.GetChanges(DataRowState.Added);
adapter.Update(dt);
dt.AcceptChanges();
DataRow lastRow = dt.Rows[dt.Rows.Count - 1];
row["Name"] = "Pqr";
adapter.Update(dt); //Error Here
connection.Close();
}
Can anybody please tell me why this is happening and what can be the workaround for this problem??