Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help : Rows.Remove() not Working

mostwanted115mostwanted115 Member Posts: 3
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]);

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

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

Comments

  • Termc1224Termc1224 Member Posts: 84
    : 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]);
    :
    : //Update the database
    : adp.Update(ds, "Employees");
    : adp2.Update(ds2, "TimeAttendance");
    :
    : //Update the session DataSet
    : Session["ds"] = ds;
    : Session["ds2"] = ds2;
    : ................
    :

    You should have created a relation between those table.
    to create a relation with sql server. select diagram. you should have created a primary to 1 of the table. then check cascade delete related records and cascade updated related fields



    I'm the best there is,
    the best there was,
    and the best there ever will be.
    hahahahaha lolz :)
Sign In or Register to comment.