SqlDataAdapter question

J

Jan Nielsen

I'm trying to use SqlDataAdapter, SqlCommandBuilder and DataSet to modify a
table in a database.

My problem is that I can't delete any records in my table. I've tried to
call the Clear, RemoveAt and Delete methods. All of the methods seems to
have an effect on the DataSet. At least that's what Rows.Count indicates.
But after calling the Update method, no changes has been made to the table
anyway. And no exceptions are thrown either. Return value from Update is 0.
I can add new records through the same DataSet. It's not a security problem.

Any ideas of what I could be doing wrong ?


/Jan Nielsen
 
G

Gerben van Loon

Hi Jan,

You need to call the delete method on a row, and after that call the update
method of the dataadapter. Like the folowing example:

DAitems.Fill(DSres1,"items"); //Fill the dataset
DSres.itemsRow rowdel = DSres1.items.FindByItem_ID(itemid); //Search the row
you want to delete
rowdel.Delete(); //Delete the row
DAitems.Update(DSres1,"items"); //Update the dataset, otherwise the row
won't be deleted

hope this helps you,

greets Gerben
 
B

Bob Grommes

Jan,

A frequent error for those learning the framework is to explicitly call
AcceptChanges() before calling Update(). Check to see if your code is doing
that, and if so, remove the AcceptChanges() call. AcceptChanges() will
reset all rows to a status of "unchanged" so that appropriate SQL will not
be generated to record the changes to the back end.

Update() and Fill() both implicitly call AcceptChanges() when they have
finished their work. Ordinarily, you don't have to call it yourself.

--Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top