none
Rows.Remove() not removing datarow from table

    Question

  • Hello. i Am using Visual Studio 2003 .NET and C#. i have a timesheet database. in there are 2 tables: employees and timeattendance. these 2 tables have a common column: employeeID.
    On the add employee screen, an employee is added and a new row is created in the employees tabele filled with required fields.similarly, a new row is automatically added to the timeattendance table whenever a new employee is added. the new row in the timeattendance table has the employeeid field filled.
    Adding is working perfectly in both tables.
    the problem is when deleteing; the row is being deleted from the employees table only and not from the timeattendance table.
    Compilation is error-free and no run-time errors exist.Here's the code:
    ================================================== ========================
    protected void OnItemClicked(object source, DataGridCommandEventArgs e)
    {
    //Get the index of the item that was selected
    int itemindex = (int)e.Item.ItemIndex;

    if (e.CommandName == "Select")
    {
    //Save the index of the selected item in the view state
    ViewState["SelectedItemIndex"] = itemindex;

    //Fill the edit boxes with the selected employees name
    DataRow selectedRow = ds.Tables["Employees"].Rows[itemindex];
    txtNameEdit.Text = selectedRow["Name"].ToString();
    txtEmployeeIDUpdate.Text = selectedRow["EmployeeID"].ToString();


    //Enable the edit controls
    txtNameEdit.Enabled = true;
    txtEmployeeIDUpdate.Enabled = true;
    btnEmployeeUpdate.Enabled = true;
    }
    else
    {
    DataRow drs= ds.Tables["Employees"].Rows[itemindex];
    string dc =drs["EmployeeId"].ToString();
    DataRow[] drc= ds2.Tables["TimeAttendance"].Select("EmployeeId = " + dc);
    try
    {
    //Delete the selected employee, delete works fine here
    ds.Tables["Employees"].Rows[itemindex].Delete();

    ===========>>>>//Something is not workoing in this line
    ds2.Tables["TimeAttendance"].Rows.Remove(drc[0]);

     

    ds.AcceptChanges();

    ds2.AcceptChanges();

    //Update the database
    adp.Update(ds, "Employees");
    adp2.Update(ds2, "TimeAttendance");

    //Update the session DataSet
    Session["ds"] = ds;
    Session["ds2"] = ds2;
    ................

    Sunday, August 12, 2007 7:27 AM

All replies

  •  

    have you tried to use the .Delete() method instead of the .Remove(index) method? What happens?

    Have you tried to update adp2 first then adp? (Remember, remove the relational data first before removing the "master" table).

    Are you sure it is not appearing to be not removing data from the code point of view but it actually is when you open up SQL Server and view the tables?

    Sunday, August 12, 2007 9:21 PM
  • The Remove() and RemoveAt() methods remove a DataRow from a DataRowCollection.  They don't mark it for deletion.  A typical use case for Remove() is to prune rows from a DataTable once the application knows it's not going to update or delete them and it doesn't need to display them anymore. 

     

    In your application, the code you're using would work just fine (indeed, would probably be the right approach) if your database managed the relation between Employee and TimeAttendance tables and performed cascading deletes:  deleting the Employee row automatically deletes all of the related TimeAttendance rows, so you don't want to delete them yourself. 

     

    Also:  why are you using two DataSets?

    Monday, August 13, 2007 12:30 AM
  • In reply to Mr. ahmedilyas
    rows.delete() method will require to assign an integer index to rows. this works with the employees table using the declared itemindex. but the index of the same row corresponding in  TimeAttendance table is different so i cannot use the same row.delete() method in TimeAttendance. what is common between the 2 rows in both tables is the EmployeeID which am trying to save it as string upon selection and pass it to the rows.remove() method. so am trying to delete the corresponding row in timeAttendance table using employeeID as an index. is this theoretically functional?

     

    i checked with the sql tables, still the deletion is only taking place in Employees table and not in TimeAttendance table.

    plz help!
    Monday, August 13, 2007 6:30 AM
  • In reply to Mr. Robert Rossney

    this is exactly what i need, concurrent deletion of the same row from both tables (timeattendance and employees) when i hit the delete button. but this is not working out fine. deletion is only taking place in the employees sql table and not in timeattendance as well. am sorry i didnt understand what should i do to make it work?

     

    thank you

    Monday, August 13, 2007 6:36 AM
  • If your database is not cascading deletes, you need to do the deletes in the application.  Which means that instead of using the Remove() method to remove the DataRows from the TimeAttendance DataTable, you need to use the Delete() method.  The Delete() method is a method of the DataRow, so you'd do something like:

     

       string filter = String.Format("EmployeeID = {0}", employeeId);

       foreach (DataRow row in DS.TimeAttendance.Select(filter))

       {

          row.Delete();

       }

       DS.Employee.FindByEmployeeId(employeeId).Delete();

     

     

    Monday, August 13, 2007 7:58 PM
  • " string filter = String.Format("EmployeeID = {0}", employeeId); "

    a compiltion error says employeeId is not declared in namespace....

    i need this object to the employeeID integer of the employee am deleting, so how do i declare and define the object

    employeeId here?

     

    thanks again.

    Tuesday, August 14, 2007 7:05 AM
  • That was just an example, not working code.  You just want to set the filter expression for the Select() method to "EmployeeID = x", where x is the employee ID.  Using String.Format's just an easy way to do it readably.

    Tuesday, August 14, 2007 9:04 AM