Sign in to follow this  

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

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

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