I am a new ASP.NET developer and trying to use a GridView control to show all the employees in the employee table in the database. I am working now in deleting the whole information of any employee in the GridView. I am facing the following probelm and I don’t know why:
FYI, I have the following database design:
Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
Groups Table: GroupID, GroupName
Courses Table: CourseID, CourseName, GroupID
Employee_Courses Table: EmployeeID, CourseID
(IsActive is like a flag (bit datatype) to indicate if the employee is in an assignment or not)
Now, I want the Admin to be able to delete an employee, and to delete of all his training information which is exisited in the Employee_Courses Table. How to do that?
My Query for deletion:
DELETE employee where dbo.employee.Username = @Username
And the code-behind for deleting a record is as following:
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
string networkID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string deleteCommand = "DELETE employee where dbo.employee.Username = " + networkID;
SqlCommand cmd = new SqlCommand(deleteCommand, conn);
cmd.Parameters.AddWithValue("@username", networkID);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (SqlException se)
{
throw se;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
GridView1.DataBind();
}
It works well for the employee who has no training record, but it doesn’t work for any employee who has a training record and it gives me the following error:
The DELETE statement conflicted with the REFERENCE constraint
“FK_employee_courses_employee”. The conflict occurred in database
“UsersInfoDB”, table “dbo.employee_courses”, column ’employeeId’. The
statement has been terminated.
So how to solve this issue?
UPDATE:
FYI, Username is the primary key in the Employee Table, and the EmployeeID is a foreign key to that primary key. So how can I delete the employee from the Employee table with all of his information in all other tables.
One mistake is in your delete query, you are missing the table name Employee_Courses
your query should be this
DELETE employee from Employee_Courses where dbo.employee.Username = @UsernameThe error comes because you have a foreign key relation. First delete the data from the child table then delete it from the parent table. The UsersInfoDB has a relationship with the Employee_Courses table, first delete the record from UsersInfoDB, then the SQL SErver will allow you to delete a record from the parent table.
Possible Solution.
Go to your Table design, Right Click on the table, a menu will appear select Relationships, a popup window with name Foreign Key RelationShips will open, Select your desired key from left pane, Then select Update and Delete Specification from the right side and specify the delete rule. For DELETE, you should set it to CASCADE. After setting the Delete rule you don’t need to manually remove the record from the child table, Now your code will work, whenever you delete a record from Employee_Courses table it will automatically removed from the child table ( UserInfoDB) as well.