ado.net - In what order C# execute sqlstatement? -


i have 2 table related foreign , primary key constraint.the visit_number in patient table must exist in visit table.in code in define create 2 instance of connection string such can use 1 instance insert record:visitnumber in visit table first , other instance insert record in patient table,with believe cord runs top bottom.but n't case. still foreign key constraint errror:

error number:547  error messagethe insert statement conflicted foreign key constraint "patient_vist_fk".  conflict occurred in database "testdb", table "dbo.visit", column 'visit_number'. statement has been terminated.on line number: 1 

meaning code running expected. please have better approach , why isn't mine working 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 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 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 > 0)                 {                     response.write("<script language=javascript>alert('record sucessfully inserted!');</script>");                     //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;         }                  {             con.close();         }      } 

you not excuting cmd2, must execute insert visit_number in cmd2 excute cmd, can test code

         using (con2)         {             con2.open();             cmd2.executenonquery();         } 

then can excute cmd

        using (con)         {             con.open();             int count = cmd.executenonquery();         } 

and can work same connexion if want

    string connect = system.configuration.configurationmanager.connectionstrings["db_connection"].tostring();     sqlconnection con = new sqlconnection(connect);     string visitnumber = txtvistnumber.text.tostring();     string insert_statement = "insert 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 visit(visit_number)"         + "values(@visit_number)";          using(con)         {             con.open;              sqlcommand cmd2 = new sqlcommand(insert_stament2, con);              cmd2.parameters.addwithvalue("@visit_number", txtvistnumber.text);             cmd2.executenonquery();              sqlcommand cmd = new sqlcommand(insert_statement, con);              cmd.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);             cmd.executenonquery();           } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -