Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I save textbox data to multiple tables?
    text
    copied!<p>I'm working on a project that is an employment application. I want to save textbox input from the user to multiple tables. My code below shows no warnings or errors when I build it, but when I go to debug, it just freezes. I think part of the problem is my commands. I'm not sure if I can have more than one, but I created a different command for each table and then at the end, close the connection to the database. I'd like to keep this as one page if I can, as I've put a lot of work into keeping it that way, but have also considered splitting it up into multiple pages and just issuing a new sqlcommand on each page. Here's the code: </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace SPApplication { public partial class Index : System.Web.UI.Page { protected void Button1_Click1(object sender, EventArgs e) { if (Page.IsPostBack) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn2"].ConnectionString; string sqlquery = "INSERT INTO [Applicant] (firstName, lastName, streetAddress, city, ZIP, phone, email) VALUES (@firstName, @lastName, @streetAddress, @city, @zip, @phone, @email)"; string sqlquery1 = "INSERT INTO [College] (CollegeName, CollegeCity, Major, CGPA, CGraduate) VALUES (@collegeName, @collegeCity, @major, @cGPA, @cGraduate)"; string sqlquery2 = "INSERT INTO [HS] (HSName, HSCity, HSGradYear, HSGraduate) VALUES (@hsName, @hsCity, @hsGradYear, @hsGraduate)"; string sqlquery3 = "INSERT INTO [Company] (CAddress, CCity, CState, CZIP, SupervisorName, SupervisorPhone, SupervisorEmail) VALUES (@cAddress, @cCity, @cStae, @cZIP, @supervisorName, @supervisorPhone, @supervisorEmail)"; string sqlquery4 = "INSERT INTO [References] (RName, RPhone, REmail, Relationship) VALUES (@rName, @rPhone, @rEmail, @relationship)"; SqlCommand command = new SqlCommand(sqlquery, conn); SqlCommand command1 = new SqlCommand(sqlquery1, conn); SqlCommand command2 = new SqlCommand(sqlquery2, conn); SqlCommand command3 = new SqlCommand(sqlquery3, conn); SqlCommand command4 = new SqlCommand(sqlquery4, conn); try { //open the connection to the database. conn.Open(); //First Name string FirstName = firstNameTextBox.Text; command.Parameters.AddWithValue("firstName", FirstName); //Last Name string LastName = lastNameTextBox.Text; command.Parameters.AddWithValue("lastName", LastName); //Address string StreetAddress = streetAddressTextBox.Text; command.Parameters.AddWithValue("streetAddress", StreetAddress); //City string City = CityTextBox.Text; command.Parameters.AddWithValue("city", City); //ZIP string zip = ZIPTextBox.Text; command.Parameters.AddWithValue("zip", zip); //Phone Number string PhoneNumber = telTextBox.Text; command.Parameters.AddWithValue("phone", PhoneNumber); //Email string Email = emailTextBox.Text; command.Parameters.AddWithValue("email", Email); //Command ExecuteNonQuery to return number of rows affected. command.ExecuteNonQuery(); //************************************************************************************************* //Add information to the College table //College Name string CollegeName = CollegeNameTextBox.Text; command1.Parameters.AddWithValue("collegeName", CollegeName); //College City string CollegeCity = CollegeCityTextBox.Text; command1.Parameters.AddWithValue("collegeCity", CollegeCity); //College State string CollegeState = CollegeStateTextBox.Text; command1.Parameters.AddWithValue("collegeState", CollegeName); //College Grad Year string CollegeGradYear = CollegeGradYearTextBox.Text; command1.Parameters.AddWithValue("collegeGradYear", CollegeName); //College Graduate string Graduate; if (CollegeGradCheckBox.Checked) { Graduate = "Yes"; } else { Graduate = "No"; } command1.Parameters.AddWithValue("cGraduate", Graduate); //Command1 ExecuteNonQuery to return number of rows affected. command1.ExecuteNonQuery(); //****************************************************************************************************** //Insert information into the HS table. //High School Name string HSName = HSNameTextBox.Text; command2.Parameters.AddWithValue("hsName", HSName); //High School City string HSCity = HSCityTextBox.Text; command2.Parameters.AddWithValue("hsCity", HSCity); //High School State string HSState = HSStateTextBox.Text; command2.Parameters.AddWithValue("hsState", HSState); //High School Graduation Year string HSGradYear = HSGradYearTextBox.Text; command2.Parameters.AddWithValue("hsGradYear", HSGradYear); //High School Graduate? string HSGraduate; if (HSGradCheckBox.Checked) { HSGraduate = "Yes"; } else { HSGraduate = "No"; } command2.Parameters.AddWithValue("hsGraduate", HSGraduate); //Command 2 Execute NonQuery to return number of rows affected command2.ExecuteNonQuery(); //*************************************************************************************** //Insert information into the Employers table //Employer 1 Name string CName1 = CNameTextBox.Text; command3.Parameters.AddWithValue("cName", CName1); //Employer 1 Address string CAddress1 = CAddressTextBox.Text; command3.Parameters.AddWithValue("cAddress", CAddress1); //Employer 1 City string CCity1 = CCityTextBox.Text; command3.Parameters.AddWithValue("cCity", CCity1); //Employer 1 State string CState1 = CStateTextBox.Text; command3.Parameters.AddWithValue("cState", CState1); //Employer 1 ZIP string CZIP1 = CZIPTextBox.Text; command3.Parameters.AddWithValue("cZIP", CZIP1); //Employer 1 Supervisor Name string SupervisorName = SupNameTextBox.Text; command3.Parameters.AddWithValue("supervisorName", SupervisorName); //Employer 1 Supervisor Phone Number string SupervisorPhone = SupPhoneTextBox.Text; command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone); //Employer 1 Supervisor Email Address string SupervisorEmail = SupEmailTextBox.Text; command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail); //Ok to Contact? string OkToContact1; if (OkToContactCheckBox.Checked) { OkToContact1 = "Yes"; } else { OkToContact1 = "No"; } command3.Parameters.AddWithValue("okToContact", OkToContact1); //************************************************************************************************** //Employer 2 Name string CName2 = CName2TextBox.Text; command3.Parameters.AddWithValue("cName", CName2); //Employer 2 Address string CAddress2 = CAddress2TextBox.Text; command3.Parameters.AddWithValue("cAddress", CAddress2); //Employer 2 City string CCity2 = CCity2TextBox.Text; command3.Parameters.AddWithValue("cCity", CCity2); //Employer 2 State string CState2 = CState2TextBox.Text; command3.Parameters.AddWithValue("cState", CState2); //Employer 2 ZIP string CZIP2 = CZIP2TextBox.Text; command3.Parameters.AddWithValue("cZIP", CZIP2); //Employer 2 Supervisor Name string SupervisorName2 = SupName2TextBox.Text; command3.Parameters.AddWithValue("supervisorName", SupervisorName2); //Employer 2 Supervisor Phone Number string SupervisorPhone2 = SupPhone2TextBox.Text; command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone2); //Employer 2 Supervisor Email Address string SupervisorEmail2 = SupEmail2TextBox.Text; command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail2); //Ok to Contact? string OkToContact2; if (OkToContact2CheckBox.Checked) { OkToContact2 = "Yes"; } else { OkToContact2 = "No"; } command3.Parameters.AddWithValue("okToContact", OkToContact2); //******************************************************************************************** //Employer 3 Name string CName3 = CName3TextBox.Text; command3.Parameters.AddWithValue("cName", CName3); //Employer 3 Address string CAddress3 = CAddress3TextBox.Text; command3.Parameters.AddWithValue("cAddress", CAddress3); //Employer 3 City string CCity3 = CCity3TextBox.Text; command3.Parameters.AddWithValue("cCity", CCity3); //Employer 3 State string CState3 = CState3TextBox.Text; command3.Parameters.AddWithValue("cState", CState3); //Employer 3 ZIP string CZIP3 = CZIP3TextBox.Text; command3.Parameters.AddWithValue("cZIP", CZIP3); //Employer 3 Supervisor Name string SupervisorName3 = SupName3TextBox.Text; command3.Parameters.AddWithValue("supervisorName", SupervisorName3); //Employer 3 Supervisor Phone Number string SupervisorPhone3 = SupPhone3TextBox.Text; command3.Parameters.AddWithValue("supervisorPhone", SupervisorPhone3); //Employer 3 Supervisor Email Address string SupervisorEmail3 = SupEmail3TextBox.Text; command3.Parameters.AddWithValue("supervisorEmail", SupervisorEmail3); //Ok to contact? string OkToContact3; if (OkToContact3CheckBox.Checked) { OkToContact3 = "Yes"; } else { OkToContact3 = "No"; } command4.Parameters.AddWithValue("okToContact", OkToContact3); //Command3 Execute Non Query to return number of rows affected command3.ExecuteNonQuery(); //***************************************************************************************************** //Insert information into the References database //Reference 1 Name string RName1 = RName1TextBox.Text; command4.Parameters.AddWithValue("rName", RName1); //Reference 1 Phone string RPhone1 = RPhone1TextBox.Text; command4.Parameters.AddWithValue("rPhone", RPhone1); //Reference 1 Email string REmail1 = REmail1TextBox.Text; command4.Parameters.AddWithValue("rEmail", REmail1); //Reference 1 Relationship string Relationship1 = Relationship1TextBox.Text; command4.Parameters.AddWithValue("relationship", Relationship1); // // // //Reference 2 Name string RName2 = RName2TextBox.Text; command4.Parameters.AddWithValue("rName", RName2); //Reference 2 Phone string RPhone2 = RPhone2TextBox.Text; command4.Parameters.AddWithValue("rPhone", RPhone2); //Reference 2 Phone Number string REmail2 = REmail2TextBox.Text; command4.Parameters.AddWithValue("rEmail", REmail2); //Reference 2 Email string Relationship2 = Relationship2TextBox.Text; command4.Parameters.AddWithValue("relationship", Relationship2); //Reference 2 Relationship // //Reference 2 Name string RName3 = RName3TextBox.Text; command4.Parameters.AddWithValue("rName", RName3); //Reference 2 Phone string RPhone3 = RPhone3TextBox.Text; command4.Parameters.AddWithValue("rPhone", RPhone3); //Reference 3 Email string REmail3 = REmail3TextBox.Text; command4.Parameters.AddWithValue("rEmail", REmail3); //Reference 3 Relationship string Relationship3 = Relationship3TextBox.Text; command4.Parameters.AddWithValue("relationship", Relationship3); //Execute Non Query to return the amount of rows affected command4.ExecuteNonQuery(); } // //close the connection to the database. catch (System.Data.SqlClient.SqlException exception) { string msg = "Some information has not been entered. Please go back and correct information."; } finally { conn.Close(); } } } } } </code></pre> <p>Thank you for your help, everyone. Forgive me, I'm new at this, and not a professional, but rather a student. I took out the ExecuteNonQuery(); for each block and the page would load, but wouldn't save any information to the database. I'm going to try your suggestions now that I got back here to see what you guys had to say about it. </p>
 

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