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

  • Home
  • SEARCH
  • 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 8768957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:12:15+00:00 2026-06-13T17:12:15+00:00

In my project I am filling two database tables with two DataSets. The method

  • 0

In my project I am filling two database tables with two DataSets. The method which I am using to fill those database tables is shown below:

SqlCommand sqlCommand = new SqlCommand();            
SqlCommand command;
using (SqlConnection conn = new SqlConnection(strConn))
{
    using (SqlCommand cmd = new SqlCommand(strQuery, conn))
    {
        using (SqlCommand cmdReset = new SqlCommand("DBCC CHECKIDENT('Doctor', RESEED, 0)", conn))
        {
            using (SqlCommand cmdUnCheck = new SqlCommand("alter table [dbo].[Doctor] nocheck constraint all",conn))
            {
                using (SqlCommand cmdCheck = new SqlCommand("alter table [dbo].[Doctor] with check check constraint all",conn))
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    conn.Open();
                    SqlTransaction sqlTransaction = conn.BeginTransaction();
                    cmd.Transaction = sqlTransaction;
                    cmdReset.Transaction = sqlTransaction;
                    cmdUnCheck.Transaction = sqlTransaction;
                    cmdCheck.Transaction = sqlTransaction;
                    try
                    {
                        cmdUnCheck.ExecuteNonQuery();
           /*ERROR*/    cmd.ExecuteNonQuery();
                        cmdReset.ExecuteNonQuery();//deleting database table data
                        foreach (DataRow dr in ds.Tables[0].Rows) //Inserting new data into the Database table
                        {
                            command = new SqlCommand(query.createDoctorRow("Doctor",
                            dr[1].ToString(), dr[2].ToString(), Convert.ToInt64(dr[3]), Convert.ToInt32(dr[4])), conn, sqlTransaction);
                            command.ExecuteNonQuery();
                        }
                        cmdCheck.ExecuteNonQuery();
                        sqlTransaction.Commit();
                        conn.Close();
                    }
                    catch (Exception e)
                    {
                        sqlTransaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }                
}

The above code was working fine till the second Database table was empty but when I inserted some data in it and then run the code, VS is showing error –>

The DELETE statement conflicted with the REFERENCE constraint “FK_Dr_ID”. The conflict occurred in database “Hospital”, table “dbo.Patient”, column ‘Doctor_ID’.
The statement has been terminated.

This seems clear that the foreign key is restricting the query to take place but as I am a beginner and dont know what to do I am asking your help..

In the above code I made changes referring to this SO answer but no joy, the error still exists at the same point.

Optional: Is there anyway to reduce the using statements in my code?

  • 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-13T17:12:18+00:00Added an answer on June 13, 2026 at 5:12 pm

    You can’t delete the data in the DOCTOR table since you have a relation to the Patient table via the Id of the DOCTOR.

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

Sidebar

Related Questions

Yeah, so I'm filling out a requirements document for a new client project and
I am working on a project which will include automatically filling out some templates.
I am filling a ListView with data from my SQLite Database using a SimpleCursorAdapter.
I have just started a new database project to price customer bid proposals: Some
I have a form in an ASP.Net MVC project on which I am using
I'm working on a project which is using Audio Toolbox for recording and playback
I'm new to OpenCv and have been using it for a small project. I
I have a new project which simply put, is an attempt to formalize the
My project consists of an app that links to two static libraries. Each of
This project is in Obj-C for iphone. I'm using the double float version of

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.