Sign in to follow this  

[.net] DataRow.Delete doesn't work, Won't update database.

This topic is 4071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to simply delete a row in a table but the changes are not taking place in the database. Here's the code I'm using.
            SqlConnection con = new SqlConnection("Server=*******;Database=*******;User Id=*******;password=*******;");
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("Select * From Test_B", con);           
            SqlCommandBuilder cmdBldr = new SqlCommandBuilder(adapter);
            adapter.InsertCommand = cmdBldr.GetInsertCommand();
            adapter.DeleteCommand = cmdBldr.GetDeleteCommand();
            adapter.UpdateCommand = cmdBldr.GetUpdateCommand();
            DataSet Set = new DataSet();
            adapter.Fill(Set);   
   
            Set.Tables[0].Rows[0].Delete();       
            Set.Tables[0].AcceptChanges();
            Set.AcceptChanges();
            adapter.Update(Set, Set.Tables[0].TableName);
       
            con.Close();

After I execute it, the first row still exists in the database. What am I doing wrong?

Share this post


Link to post
Share on other sites
Fairly sure you shouldn't call .AcceptChanges before doing the update. When you do .AcceptChanges, the subsequent .Update will be a no-op because the data adapter thinks the data table has no changes.

Share this post


Link to post
Share on other sites
You're calling AcceptChanges(), twice. Don't call this, it'll be automatically called by the data adapter when the update is complete.

By calling it prematurely you're telling the dataset that the changes should be committed to the dataset (note: NOT the database), and it'll mark the data as unchanged. When DataAdapter.Update() gets called it examines the dataset/table in question and sees that it has no changed records, and thus won't do anything.

Share this post


Link to post
Share on other sites

This topic is 4071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this