Jump to content
  • Advertisement
Sign in to follow this  

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

This topic is 4351 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=*******;");
            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.Update(Set, Set.Tables[0].TableName);

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!