Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I`m trying to delete a row in an access database, but when I try to update the dataset it give me this error:

"Update requires a valid DeleteCommand when passed DataRow collection with deleted rows."

I tried to solve it on my own, but I can't seem to fix it.

If anyone can give me any advice I will be very thankful.

Here is my code.
Thanks for all the help in advance.


currentRow = e.RowIndex;
ds1 = new DataSet();
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=DataSource/PhoneBookData.mdb";
con.Open();
string sql = "SELECT * From CONTACT";
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
da.Fill(ds1, "CONTACT");

DataRow dRow = ds1.Tables["CONTACT"].Rows[0];
ds1.Tables["CONTACT"].Rows[currentRow].Delete();
da.Update(ds1, "CONTACT");
Posted
Updated 20-Oct-11 21:36pm
v2
Comments
Dalek Dave 21-Oct-11 3:37am    
Edited for Grammar and Syntax.

You need to use SQLCommandBuilder for creating the delete command.(Remember you always need primary key for using SQLCommandBuilder)

Or you can try This[^] for SQLDataAdapter without using SQLCommandBuilder.

for further queries comment here!

hope it helps :)
 
Share this answer
 
You need to create an OledbCommand and then assign it to the DataAdapter, something like this:-

C#
command = new OleDbCommand(
        "DELETE * FROM Contacts WHERE ContactID = ?",
        con);
    parameter = command.Parameters.Add(
        "ContactID", OleDbType.Char, 5, "ContactID");
    parameter.SourceVersion = DataRowVersion.Original;
    dataAdapter.DeleteCommand = command;


Hope this helps
 
Share this answer
 
Comments
Dalek Dave 21-Oct-11 3:48am    
Looks good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900