c# - Unable to do consecutive database inserts -


i'm attempting 2 sql inserts consecutively 2 different tables.

the first table insert works fine. uses select scope_identity() pull index number store in variable registree_index. works ok.

i try insert registree_index , other variables second table. not write second table @ all. no error message either.

initially thought error had reusing old query , connection string variables, created new ones. has not helped.

does have thoughts on this? code follows...

private void writetodatabase() {     guid newguid = guid.newguid();      string yearstring = datetime.now.year.tostring();     string twodigityear = yearstring.substring(yearstring.length-2);     string dateandguid = twodigityear + "-" + datetime.now.month.tostring() + "-" + datetime.now.day.tostring() + "-"  + datetime.now.hour.tostring() + "-"  + datetime.now.minute.tostring() + "-"  + datetime.now.second.tostring() + "-" + newguid;      string connectionstring = getconnectionstring();      sqlconnection connection = new sqlconnection();     connection.connectionstring = connectionstring;     connection.open();      string insertquery = "insert registrees (uidindex, submission_number, homecoming_form, hc_form, newrecord, first_name, last_name, billing_phone, addresses_same, email) values (@uidindex, @submission_number, @homecoming_form, @hc_form, @newrecord, @first_name, @last_name, @billing_phone, @addresses_same, @email)               select scope_identity()";      sqlcommand cmd = new sqlcommand(insertquery, connection);      cmd.parameters.addwithvalue("@uidindex", dateandguid);     cmd.parameters.addwithvalue("@submission_number", 1);     cmd.parameters.addwithvalue("@homecoming_form", 1);     cmd.parameters.addwithvalue("@hc_form", "platform");     cmd.parameters.addwithvalue("@newrecord", 1);      cmd.parameters.addwithvalue("@first_name", first_name.text);     cmd.parameters.addwithvalue("@last_name", last_name.text);      cmd.parameters.addwithvalue("@billing_phone", phone.text);     cmd.parameters.addwithvalue("@addresses_same", 1);      cmd.parameters.addwithvalue("@email", email.text);      ///get index scope identity     int registree_index = convert.toint32(cmd.executescalar());        ///so far works great! rest of code fails somehow.     connection.close();     connection = null;     insertquery = null;     cmd = null;      string connectionstring2 = getconnectionstring();     sqlconnection connection2 = new sqlconnection();     connection2.connectionstring = connectionstring2;     connection2.open();      string insertquery2 = "insert event_registration (registree_index, uidindex, submission_number) values (@registree_index, @uidindex, @submission_number)";      sqlcommand cmd2 = new sqlcommand(insertquery2, connection2);      cmd2.parameters.addwithvalue("@registree_index", registree_index);     cmd2.parameters.addwithvalue("@uidindex", dateandguid);     cmd2.parameters.addwithvalue("@submission_number", 1);  } 

how this...

private void writetodatabase()     {         guid newguid = guid.newguid();          string yearstring = datetime.now.year.tostring();         string twodigityear = yearstring.substring(yearstring.length - 2);         string dateandguid = twodigityear + "-" + datetime.now.month.tostring() + "-" + datetime.now.day.tostring() + "-" + datetime.now.hour.tostring() + "-" + datetime.now.minute.tostring() + "-" + datetime.now.second.tostring() + "-" + newguid;         int registree_index;          using (sqlconnection connection = new sqlconnection(getconnectionstring()))         {             connection.open();              string insertquery = "insert registrees (uidindex, submission_number, homecoming_form, hc_form, newrecord, first_name, last_name, billing_phone, addresses_same, email) values (@uidindex, @submission_number, @homecoming_form, @hc_form, @newrecord, @first_name, @last_name, @billing_phone, @addresses_same, @email)               select scope_identity()";              using (sqlcommand cmd = new sqlcommand(insertquery, connection))             {                  cmd.parameters.addwithvalue("@uidindex", dateandguid);                 cmd.parameters.addwithvalue("@submission_number", 1);                 cmd.parameters.addwithvalue("@homecoming_form", 1);                 cmd.parameters.addwithvalue("@hc_form", "platform");                 cmd.parameters.addwithvalue("@newrecord", 1);                  cmd.parameters.addwithvalue("@first_name", first_name.text);                 cmd.parameters.addwithvalue("@last_name", last_name.text);                  cmd.parameters.addwithvalue("@billing_phone", phone.text);                 cmd.parameters.addwithvalue("@addresses_same", 1);                 cmd.parameters.addwithvalue("@email", email.text);                  ///get index scope identity                 registree_index = convert.toint32(cmd.executescalar());             }              string insertquery2 = "insert event_registration (registree_index, uidindex, submission_number) values (@registree_index, @uidindex, @submission_number)";              using (sqlcommand cmd = new sqlcommand(insertquery2, connection))             {                 cmd.parameters.addwithvalue("@registree_index", registree_index);                 cmd.parameters.addwithvalue("@uidindex", dateandguid);                 cmd.parameters.addwithvalue("@submission_number", 1);                  cmd.executenonquery();             }         }      } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -