Chris

Chris

  • NA
  • 11
  • 0

How to delete specific DataRow in DataSet?

Nov 28 2006 10:35 AM
Hey guys, I am trying to delete a specific xml datarow with a specific value but it's not working out for me. Here's my code and the error I receive: DataSet ds = new DataSet(); StreamReader sr = new StreamReader(pathToFile + "AlertSettings.xml"); ds.ReadXml(sr); sr.Close(); DataRow rowToDelete = ds.Tables[0].NewRow(); rowToDelete["PublicationToAlert"] = publication; if (ds.Tables[0].Rows.Contains(rowToDelete)) { ds.Tables[0].Rows.Remove(rowToDelete); ds.AcceptChanges(); ds.WriteXml(pathToFile + "AlertSettings.xml", XmlWriteMode.IgnoreSchema); } Here is the error message: "The given DataRow is not in the current DataRowCollection" I want to delete the row where it's value is equal to the "publication" parameter's value that is being passed in. (i.e. rowToDelete["PublicationToAlert"] = publication;) The remove command breaks and so does this line of code: if (ds.Tables[0].Rows.Contains(rowToDelete)) I get the following error with the above line: "Table doesn't have a primary key" Any help would be greatly appreciated. Thanks guys.

Answers (8)