Question

I've initialized a dataAdapter :

string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");

And this works just fine. The problem is when i try to delete a record and update the database. I remove a record from the dataset as such :

ds1.Tables["localitati"].Rows.Remove(dRow);

And this works just fine as well(verified).

The problem is when i update the DataAdapter, the DataBase doesn't get modified :

con.Open()
da1.Update(ds1, "localitati");
con.Close();

What could be the problem ?

Was it helpful?

Solution

You need to make sure you've set the da1.DeleteCommand - this is the command that will be fired for each row in the DataTable that has been deleted. See this MSDN reference for example.

OTHER TIPS

What fixed it for me was to call the Delete method on the DataRow instead of the Remove method on the DataTable.

ds.Tables["localitati"].Rows.Find(primaryKeyValue).Delete();

or just simply

dr.Delete();

try below code assuming that Database is not throwing any exception.

con.Open() 
da1.Update(ds1.Tables["localitati"].GetChanges()); 
con.Close(); 

In the code that you have posted only the SelectCommand is set for the DataAdapter. You could use the following code to generate the Insert, Update and Delete commands for da1.

string sql = "SELECT * From localitati";
da1 = new SqlDataAdapter(sql, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da1);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
da1.Fill(ds1, "localitati");

The CommandBuilder however should be used only for relatively simple scenarios (details). For he rest is recommended to write your own commands that rely on custom command texts/stored procedures.

If it still doesn't work, you could start a SQL Server Profiler on the server to trace what command gets to the database when you execute the Update method.

DataRow dr = datatable.Rows.Find(key);      
int Index = datatable.Rows.IndexOf(dr);

BindingContext[DataTable].RemoveAt(Index);     
BindingContext[DataTable].EndCurrentEdit(); 

dataAdapter.Update(DataTable);    
DataTable.AcceptChanges();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top