• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

How to update my database table from my grid? (Almost done--just need a little help.)

Hello.

I am trying to cascade my delete from the dataset back to the database.  Here's my code.  What am I leaving out?  The row disappears from the datagrid, but it's still in the database.

(Don't worry about the hardocded '2'.  That's just there to simply the problem.

Thanks in advance!

      string strSQLDelete = "delete from Inventory where ID= 2";
      OleDbCommand cmdDelete = new OleDbCommand (strSQLDelete, dbConn);
      resultDA.DeleteCommand = cmdDelete;
      DataRow rowDel = ds.Tables ["Inventory"].Rows[1];
      ds.Tables["Inventory"].Rows.Remove(rowDel);
      resultDA.Update (ds, "Inventory");
0
bek
Asked:
bek
  • 3
  • 2
1 Solution
 
jong3Commented:
Try doing a Dataset.AcceptChanges() right before the update command.

In your case:

ds.AcceptChanges();

This function should finalize the deleted rows, and remove it completely from the dataset.
0
 
bekAuthor Commented:
But how do I get those changes back to my database?
0
 
bekAuthor Commented:
I tried adding ds.AcceptChanges() where you said.  No errors, but it didn't remove the row from my database.

Any idea what's wrong?
0
 
jong3Commented:
Yes.  Sorry for the delay in answering your question.

The AcceptChanges() will only update your dataset, you will still need to run the Update command on your database.

Your dataset is basically a local copy of the data from your database.  When you edit the dataset, flags are set to identify what was done to it.  By using the AcceptChanges command, it will finalize your changes, and allow the Update command to execute the way you expect it to.

The Update command will run Insert/Update/Delete accordingly to what you have done with your dataset.

YOU will need to specify the UpdateCommand parameter to indicate to the database what is going on.  Read the following for more information on the DataAdapter.Update command:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatacommondbdataadapterclassupdatetopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatasqlclientsqldataadapterclassupdatecommandtopic.asp
0
 
bekAuthor Commented:
I never really got an answer, but figured it out myself.  However, if you wish to award the points to jong3, I have no problem with that because he was trying to help me.

We really need the option to delete questions on our own.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now