hai,
I am new to c#, actually i have a table adapter which is bind to a
grid, and i have three button insert,update and delete. insert and
update are working fine, when i delete a record in grid it is been
deleted but it is not been deleted in database.once again if i run the
application the deleted record is shown,please help me,any suggestion
will be appreciated. I have attached my code and error message.
Thanks
sham sumanPosted Aug 16, 2010, 6:26 AM
You can use Acceptchange() method after deleting code. Just check this.
Change the table name as per ur TableName.
Insted of session You can use direct table from DataAdapter. Just assign table name. Check this code...
Write this code in GridView1 RowDelete Command event.
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
int i;
DataSet objds = (DataSet)Session["ObjDataSet"]; //Assign table from database here.
DataTable dtNewAct = (DataTable)objds.Tables[0];
i = GridView1.SelectedIndex;
for (int iRow = 0; iRow < objds.Tables[0].Rows.Count - 1; iRow++)
{
if (i == iRow)
{
if (objds.Tables[0].Rows[i].RowState != DataRowState.Deleted)
{
objds.Tables[0].Rows[i].Delete();
objds.Tables[0].AcceptChanges();
Session["ObjDataSet"] = objds;
}
}
}
}
Sam HobbsPosted Aug 16, 2010, 11:42 PM
adam abdulPosted Aug 16, 2010, 5:30 AM
Thanks for kind response, i tried ur code but it fails to get table name.
Here is the code below
Note: adduser is my table name
private void simpleButton1_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
}
addUserTableAdapter.Update(checkOutDataSet.Tables[adduser]);
}
error message:
Error 1 The name 'adduser' does not exist in the current context
DRISHTYPosted Aug 16, 2010, 5:16 AM
sqllAdap.Update(ds.Tables[TBL_NAME]);