none
Delete row from datatable based on column value RRS feed

  • Question

  • hi ,

    i want to delete the row that column id=15 but i can't do it iwrote this code but it generate an invalidoperationexception error:

    this my code :

     public static void dataSetDelete(SqlConnection oConnection) {
     
          oConnection.Open();
    
          SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
          DataSet oDataSet = new DataSet("DataSetTable4");
         oSqlDataAdapter.Fill(oDataSet, "Table4");
          foreach (DataRow dr in oDataSet.Tables["Table4"].Rows)
          {
            if (dr["id"].ToString() == 15)
              dr.Delete();
          }
        oDataSet.AcceptChanges();
    
          
         oSqlDataAdapter.Update(oDataSet.Tables["Table4"]);
    
          
         
        }
    
    

    What is the problem ,

    Help Please

    Thank you

     


    .NET forever
    Sunday, October 3, 2010 11:04 AM

Answers

  •  

     

    What error do you get? Have you stepped through and checked the values are as you expect. Between what Mike and I have told you, you should have something like:

     

    foreach (DataRow dr in oDataSet.Tables["Table4"].Rows)
       {
        if (Convert.ToInt32(dr["id"]) == 15)
         dr.Delete();
       }
      
     
       oSqlDataAdapter.Update(oDataSet.Tables["Table4"]);
    
      //oDataSet.AcceptChanges(); //Don't need this. Update() should take care of it for you.
    

    • Marked as answer by ManaiAmira Sunday, October 3, 2010 4:40 PM
    Sunday, October 3, 2010 2:39 PM

All replies

  • Probably you wanted to write:

     if (Convert.ToInt32(dr["id"]) == 15)

    15 is not a String.

    Sunday, October 3, 2010 12:00 PM
  • Do you have a "delete command' attached to the Adapter object? Row.Delete will just mark 'RowState' value as 'Deleted'. The actual delete operation will happen when you call AcceptChanges at this time; the DataRow is removed from the table. Hope you understood the problem. While calling AcceptChanges , you are saying 'row is already deleted', so Adapter.Update ( ) got nothing to do, so it throws invalid operation error.

    Sunday, October 3, 2010 12:02 PM
  • Thank u, i underlstand the problem but i can not fix it

    .NET forever
    Sunday, October 3, 2010 12:18 PM
  •  

     

    What error do you get? Have you stepped through and checked the values are as you expect. Between what Mike and I have told you, you should have something like:

     

    foreach (DataRow dr in oDataSet.Tables["Table4"].Rows)
       {
        if (Convert.ToInt32(dr["id"]) == 15)
         dr.Delete();
       }
      
     
       oSqlDataAdapter.Update(oDataSet.Tables["Table4"]);
    
      //oDataSet.AcceptChanges(); //Don't need this. Update() should take care of it for you.
    

    • Marked as answer by ManaiAmira Sunday, October 3, 2010 4:40 PM
    Sunday, October 3, 2010 2:39 PM
  • Thank u so  mutch sir i fixed the problem
    .NET forever
    Sunday, October 3, 2010 4:38 PM