Hi all,
I have a gridview on my page and a delete function in the gridview. The WorkID in table Work is the foreign key for table Employee.
Before delete, i need system help me to check whether the WorkID exist in other table or not. If WorkID exist in other table, it will not allow user to delete it. Can anyone guide me with this.
The code i show below is only able to delete those row/data with no foreign key in other table.
protected void gvWork_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Loop all data rows
foreach (DataControlFieldCell cell in e.Row.Cells)
{
//Check all cells in one row
foreach (Control control in cell.Controls)
{
LinkButton button = control as LinkButton;
if (button != null && button.CommandName == "Delete")
{
// Add delete confirmation
button.Attributes.Add("onclick", "javascript:return " + "confirm('Do you want to delete the record " + DataBinder.Eval(e.Row.DataItem, "WorkName") + "?')");
}
}
}
}
}
Rahul BhattPosted Jul 18, 2012, 2:04 AM
First Remove child Table Records after then Remove Parent Table Records
IF record exist in Chid table then remove this Child Table Records
Next Remove Parent Table Records
As per my suggestion ,
when any records used in Child Table then not Allowed to Delete and Display Alert message.
First goes to this child page where your Parent ID used .... Delete this records first
After then goes to Master Page ... Delete this records
Note :: Use DB Transaction , If any case if you want to rollback your trancation then you can eaily rollback using Transaction
Jignesh TrivediPosted Jul 17, 2012, 11:55 PM
Inthis case RowCommand event of grid view may help you.
In Row commnad first check referance of foreign key if it not exist then run your delete code otherwise return.
please refer
http://www.c-sharpcorner.com/uploadfile/syedshakeer/rowcommand-event-in-gridview/
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
hope this will help you.