Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error deleting "deleted" datarow from sql database

Status
Not open for further replies.

alexjones

Programmer
Jul 27, 2001
132
US
I am attempting to persist my disconnected dataset deletes to my SQL database with a SQlCommand, using the following (C#) code:
Code:
DataRow[] products = ds.dt.Select("OfferID = " + offerID);
foreach (DataRow product in products)
{
   deleteCmd.Parameters[0].Value = product["ProductCode"].ToString().ToUpper();
   deleteCmd.Parameters[1].Value = Convert.ToInt32(product["OfferID"]);
   deleteCmd.ExecuteNonQuery();
}

The rows have rowstate of deleted.

This generates an exception: "Deleted row information cannot be accessed through the row"

Can anyone tell me why this should be so? And/or how else I might access the row information?

This code is part of a sequence of delete commands - some hierarchical, some not - that I am attempting to execute within a transaction.

I was not able to accomplish this using table adaptors.

 
Try the C# forum, they may be more helpful.

Also, if the ds.dt is bound to the database, you could wind up in a situation where you are deleting records (which updates your ds.dt, which removes the row from the products datarow array) while iterating through them. .Net does not like that so much.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Check if the row is deleted before trying to access it:

foreach (DataRow product in products)
{
if(product.RowState == DataRowState.Deleted)
{
//row is deleted, so do nothing
}
else
{
deleteCmd.Parameters[0].Value = product["ProductCode"].ToString().ToUpper();
deleteCmd.Parameters[1].Value = Convert.ToInt32(product["OfferID"]);
deleteCmd.ExecuteNonQuery();
}
}

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top