Note that there are some explanatory texts on larger screens.

plurals
  1. POIn what order C# execute sqlstatement?
    primarykey
    data
    text
    <p>I have two table related by foreign and primary key constraint.The Visit_Number in Patient table must exist in the Visit table.In my code in define create two instance of my connection string such that i can use one instance to insert record:VisitNumber in Visit table first and then the other instance to insert record in the patient table,with the believe that the cord runs from top to bottom.But this was n't the case. i still get foreign key constraint errror:</p> <pre><code>Error Number:547 Error MessageThe INSERT statement conflicted with the FOREIGN KEY constraint "Patient_Vist_FK". The conflict occurred in database "TestDB", table "dbo.Visit", column 'Visit_Number'. The statement has been terminated.On line Number: 1 </code></pre> <p>meaning the code is running as i expected. Please do you have a better approach and why isn't mine working code:</p> <pre><code>protected void btn_save_Click(object sender, EventArgs e) { string connect = System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString(); SqlConnection con = new SqlConnection(connect); SqlConnection con2 = new SqlConnection(connect); string visitnumber = txtVistNumber.Text.ToString(); string insert_statement = "Insert into Patient(Patient_Number,FirstName,LastName,Gender,Tribe,Date_Of_Birth,Visit_Number)" + "Values(@Patient_Number,@FirstName,@LastName,@Gender,@Tribe,@Date_Of_Birth,@Visit_Number)"; string insert_stament2 = "Insert into Visit(Visit_Number)" + "Values(@Visit_Number)"; SqlCommand cmd = new SqlCommand(insert_statement, con); SqlCommand cmd2 = new SqlCommand(insert_stament2, con2); cmd2.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text); cmd.Parameters.AddWithValue("@Patient_Number",txtpatientNum.Text); cmd.Parameters.AddWithValue("@FirstName",txtfirstName.Text); cmd.Parameters.AddWithValue("@LastName",txtlastname.Text); cmd.Parameters.AddWithValue("@Gender", drl_gender.SelectedValue); cmd.Parameters.AddWithValue("@Tribe",DropDownList1.Text); cmd.Parameters.AddWithValue("@Date_Of_Birth", val_age.Text); cmd.Parameters.AddWithValue("@Visit_Number", txtVistNumber.Text); try { using (con) { con.Open(); int count = cmd.ExecuteNonQuery(); if (count &gt; 0) { Response.Write("&lt;script language=javascript&gt;alert('Record Sucessfully Inserted!');&lt;/script&gt;"); //Success_Message.Text = "Record inserted"; txtpatientNum.Text = String.Empty; txtfirstName.Text = String.Empty; txtlastname.Text = String.Empty; txtVistNumber.Text = String.Empty; DropDownList1.Text = String.Empty; val_age.Text = String.Empty; } } } catch (SqlException ex) { { VisitError_Message.Text = "Error Number:" + ex.Number.ToString() + " Error Message" + ex.Message + "On line Number" + ": " + ex.LineNumber; } } catch (NullReferenceException nullexception) { VisitError_Message.Text = "Error Occurred, Error Type:" + nullexception.GetType().ToString() + "Error Message:" + nullexception.Message; } catch (DllNotFoundException dllexception) { VisitError_Message.Text = dllexception.GetType().ToString() + dllexception.Message; } finally { con.Close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload