Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:28:17+00:00 2026-06-07T07:28:17+00:00

I am a new ASP.NET developer and trying to use a GridView control to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:28:20+00:00Added an answer on June 7, 2026 at 7:28 am

    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 = @Username

    The 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a new ASP.NET developer and trying to use a GridView control to
I am a new ASP.NET developer and trying to use ASP.NET Ajax BallouPopupExtender with
I am a brand new Java developer (I have been working in asp.net) and
I'm new to ASP.NET (I'm a PHP developer) and I'm trying to understand how
I am a new ASP.NET developer and I am trying to change the value
I am a New ASP.NET Developer and I am trying to develop a simple
I am a new ASP.NET developer and I am trying to develop a simple
I created a new ASP.NET website using Visual Web Developer 2008 Express edition and
I'm developer moving from C# to Java. Heard about new ASP net feature. <%:
I have a new asp.net mvc project and i am trying to figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.