I am a new ASP.NET developer and use grid-view control to show all employees in the employee table I am trying. Database. Now I am working to remove all the information of any employee in Gridview. I am facing the following probelm and I do not know why:
FYI, I have the following database design:
Employee Table: Username, Name, JobTitle, Badgeo, Iactive, Division, Cad Division Table: SAPcode, Division Shortcut Group Table: Group ID, GroupName Commerce Table: Course ID, Cosmic, GroupID EmployeeBables Table: Employer ID, Course ID (Is as inactive as tax Employee is in an assignment or not), Now, I want the administrator to remove an employee, and remove all his training information, which is the Employee Courses Table Introduced in How to do it? My query to delete:
Delete the employee where dbo.employee.Username = @username To delete the record, the code-behind is as follows:
Secure Zero 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 the employee where dbo.employee.Username =" + networkID; SqlCommand CMD = New SqlCommand (deleteCommand, conn); Cmd.Parameters.AddWithValue ("Username", Network ID); Try {conn.Open (); Cmd.ExecuteNonQuery (); Conn.Close (); } Grip (from SqlException) {Throw off; } Finally {cmd.Dispose (); Conn.Close (); Conn.Dispose (); } GridView1.DataBind (); } It works well for the employee who has no training record, but it does not work for any employee whose training is a record, Returns the error:
The DELETE statement is contradictory with the reference reference "FK_employee_courses_employee" conflict database "UsersInfoDB", the table "dbo.employee_courses", column 'employee ID' terminates this statement has given.
How to solve this problem? Update: So how can I remove all the employee information from the employee table with all the information in the table. One mistake is in your deleted query, you are not losing the table name Employee_Courses. Your query should be
Delete the employee from Employee_Courses where dbo.employee.Username = @Username An error occurs because you have a foreign key relation. First of all delete the data from the child's table then delete it from the original table. UsersInfoDB has a connection with the Employee_Courses table, first delete the record from UsersInfoDB, then the SQL Sewer will allow you to remove a record from the parent table.
Right click on the potential solution table, right-click on the menu, choose a menu relationship , a popup window with the name opens foreign key relationship Select your desired key from the left side, then select Update and Delete Specification from the right side and specify the Deleted rule. For DELETE, you should set it in Cascade, after setting the deleted rule you do not need to manually remove the record from the child table, your code will now work, whenever you have a record from the Employee_Courses table If removed, it will automatically be removed from the Child Table (UserInfoDB).
Comments
Post a Comment