Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that takes data and inserts it into three databases when the submit button is clicked. When a user clicks the submit button there are two inserts in two of the tables where there should be one insert per table. The last table is not getting any data at all. What did I do wrong?

C#
protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table22 (USERID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@USERID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS)Insert into Table23 (USERID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@USERID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS)Insert into Table24 (USERID, FirstName, MiddleName, LastName, Prefix, Suffix, Salutation, Title, Address1, Address2, City, State, Zip, Country, Phone, Fax, Email, DATE, accessLevel) values (@USERID, @FirstName, @MiddleName, @LastName, @Prefix, @Suffix, @Salutation, @Title, @Address1, @Address2, @City, @State, @Zip, @Country, @Phone, @Fax, @Email, @DATE, @accessLevel)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTRIR.Text);
        cmd.Parameters.AddWithValue("@USERID", TextBoxUSERID.Text);
        cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
        cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
        cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
        cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
        cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
        cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
        cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
        cmd.Parameters.AddWithValue("@NET_AID", TextBoxNGAS.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOE.Text);
        cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
        cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
        cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
        cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
        cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
        cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
        cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
        cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
        cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text);
        cmd.Parameters.AddWithValue("@TOTALNETASSETS", TextBoxTNA.Text);
        cmd.Parameters.AddWithValue("@FINYR", TextBoxDATE2.Text);
        cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);

        cmd.Parameters.AddWithValue("@FirstName", TextBoxFName.Text);
        cmd.Parameters.AddWithValue("@LastName", TextBoxLName.Text);
        cmd.Parameters.AddWithValue("@MiddleName", TextBoxMName.Text);
        cmd.Parameters.AddWithValue("@Prefix", TextBoxPrefix.Text);
        cmd.Parameters.AddWithValue("@Suffix", TextBoxSuffix.Text);
        cmd.Parameters.AddWithValue("@Salutation", TextBoxSal.Text);
        cmd.Parameters.AddWithValue("@Title", TextBoxTitle.Text);
        cmd.Parameters.AddWithValue("@Address1", TextBoxMA1.Text);
        cmd.Parameters.AddWithValue("@Address2", TextBoxMA2.Text);
        cmd.Parameters.AddWithValue("@City", TextBoxCity.Text);
        cmd.Parameters.AddWithValue("@State", TextBoxState.Text);
        cmd.Parameters.AddWithValue("@Zip", TextBoxZip.Text);
        cmd.Parameters.AddWithValue("@Country", TextBoxCoun.Text);
        cmd.Parameters.AddWithValue("@Phone", TextBoxTN.Text);
        cmd.Parameters.AddWithValue("@Fax", TextBoxFN.Text);
        cmd.Parameters.AddWithValue("@Email", TextBoxEA.Text);
        cmd.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        cmd.ExecuteNonQuery();
        con.Close();
Posted
Updated 29-May-14 3:12am
v2
Comments
Naz_Firdouse 29-May-14 9:17am    
you mean to say you want to insert into three tables using a single query???
Computer Wiz99 29-May-14 9:19am    
Yes, But the third table, table24, is different data.

You Have to insert 'GO' Keyword between three insert statments

and other one solution to put semicolon ; between three statments to resolve the problems..
 
Share this answer
 
Comments
Computer Wiz99 29-May-14 9:20am    
Hardikidea, Can you show me how it should look?
Naz_Firdouse 29-May-14 9:23am    
Check the updated answer
Computer Wiz99 29-May-14 9:44am    
I have tried that way and I get an error: Incorrect syntax near ';'. How can we fix this?
Computer Wiz99 29-May-14 10:06am    
I got it. I miss typed something.
Put semicolons after each insert statement
i.e
C#
SqlCommand cmd = new SqlCommand("Insert into Table22 (USERID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@USERID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into Table23 (USERID, TOTAL_REVE, DATE, FINYR, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@USERID, @TOTAL_REVE, @DATE, @FINYR, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into Table24 (USERID, FirstName, MiddleName, LastName, Prefix, Suffix, Salutation, Title, Address1, Address2, City, State, Zip, Country, Phone, Fax, Email, DATE, accessLevel) values (@USERID, @FirstName, @MiddleName, @LastName, @Prefix, @Suffix, @Salutation, @Title, @Address1, @Address2, @City, @State, @Zip, @Country, @Phone, @Fax, @Email, @DATE, @accessLevel);", con);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900