Skip to content

How to delete specific DataRow in DataSet?

Chris

Asked Nov 28, 2006, 5:35 AM EST8 answers
Share
LinkedInX
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.

7 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    It seems to be incorrect approach. In your code you create absolutely new row and try to use it as a criteria for matching. I think it should be better to use method Select of DataTable instance, after reading data from XML.

    Should be something like this: (I didn't check this code. Take it as an idea only.)

    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);
    // }

    string criteria = "PublicationToAlert = " + publication;
    foreach (DataRow r in ds.Table[0].Select(criteria)) r.Delete();

    ds.AcceptChanges();
    ...
    0
  • 0
    Hello and thanks for replying. I tried what you suggested and received an error that said: "Could not find column [test]" ("test" being the name of the parameter passed in of the name of the value I want deleted) Here's how the xml looks: BriefChart Schedule test I want the row that has the value "test" deleted, or any value dynamically for that matter. Thanks!!!
    0
  • 0
    Why is this forum not display my xml I pasted in? I wrapped in code tags.
    0
  • 0
    string criteria = "PublicationToAlert = " + publication; foreach (DataRow r in ds.Tables[0].Select(criteria)) r.Delete(); ds.AcceptChanges(); ds.WriteXml(pathToFile + "AlertSettings.xml", XmlWriteMode.IgnoreSchema); Why is my xml not posting??
    0
  • 0
    Looks like the dataset has not field named "PublicationToAlert".

    Really I don't know why your XML is not shown. Possible you try paste XML in HTML Source Mode (in this case you can change the mode to normal by clicking the button with "<>" symbol).

    As another solution I suggest to send to me your XML by e-mail. :)
    0
  • 0
    ok, I got it to work. here is the code I used: DataSet ds = new DataSet(); StreamReader sr = new StreamReader(pathToFile + "AlertSettings.xml"); ds.ReadXml(sr); sr.Close(); DataTable dt = ds.Tables[0]; DataRow[] dr = dt.Select("PublicationToAlert = '" + publication + "'"); dr[0].Delete(); dt.AcceptChanges(); dt.WriteXml(pathToFile + "AlertSettings.xml", XmlWriteMode.IgnoreSchema); I would like though to check and see if that value exits in the xml before I attempt to delete it. Any ideas?
    0
  • 0
    OK, great.

    Heh... You can determine existing of value to be deleted by simple construction:

    bool exists = (dr.Length > 0); // insert this after calling Select method
    0
  • 1 short reply
    • 0
      Hm.. Show your new code, pls.
      0