Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form that has about 20 textboxes. The user enters the data and submits the data into the database. All of the data gets saved except the data of the user information like First name, Last Name and so on. It is not saving into Table3. Why is this happening? Here is my code.

C#
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("Insert into Table1 (INST_ID, TOTAL_REVE, FINYR, DATE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @TOTAL_REVE, @FINYR, @DATE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into TableFIN2 (INST_ID, LongName, TOTAL_REVE, FINYR, DATE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, TOTALNETASSETS) values (@INST_ID, @LongName, @TOTAL_REVE, @FINYR, @DATE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @TOTALNETASSETS); Insert into Table3 (INST_ID, FirstName, MiddleName, LastName, Prefix, Suffix, Salutation, Title, Address1, Address2, City, State, Zip, Country, Phone, Fax, Email, DATE, accessLevel) values (@INST_ID, @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("@INST_ID", TextBoxINST_ID.Text);
        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTROA.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("@SCHOLARSHI", TextBoxSFEDA.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOED.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("@DATE", TextBoxDATE.Text);
        cmd.Parameters.AddWithValue("@FINYR", TextBoxDATE2.Text);
        cmd.Parameters.AddWithValue("@TOTALNETASSETS", TextBoxTNA.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.Parameters.AddWithValue("@LongName", lblSchool.Text);
        
        cmd.ExecuteNonQuery();
        con.Close();
Posted
Comments
Raje_ 2-Jul-14 10:59am    
Are you getting any exception?
Computer Wiz99 2-Jul-14 11:15am    
No. No error message and nothing like that.
Raje_ 2-Jul-14 11:19am    
Use try-catch block and check if you are getting any exception or not.
In such case i always prefer to write a Stored Procedure.

Some things to try ....

1. Try surrounding any columns that use SQL or ODBC Reserved words [^] with square brackets e.g. [DATE]. This probably isn't a problem here however you should avoid using certain words for column names, and if you feel you must, remember to use the square-bracket trick.

2. Put a break point on the line
cmd.Parameters.AddWithValue("@FirstName", TextBoxFName.Text);
and run your code.
At the break point hover over all of those text box values and make sure that they all contain some text.

If some don't have any text in them, check to see if you have allowed nulls on that database column.

3. If they do all have text in them make sure it is the appropriate type - e.g. make sure you are not trying to update an int column with "apples"

4. If that doesn't sort your problem out, try taking the update to table3 out of that multi-statement command and run it by itself. It's not clear if you are already using a Try-Catch block but any error message you get back might help determine the problem. Or try running it directly against the database to see what errors are returned.
 
Share this answer
 
From that alone, it's pretty difficult to tell, but I suspect that you have an SQL error in the final INSERT which is throwing an exception and your system is set to ignore it. It may be as simple as a misspelled column name, or a text box with too much information in it.

So do two things:
1) Use a try...catch block to give you the exception details, and either log or log and report the problem - that way you stand a chance of finding out what it is.
2) Use a transaction round the whole insert, and cancel it if you get an error - that way you don't end up with "orphaned" information in your first two tables...
 
Share this answer
 
Hi,
can you please check your table table3 passing parameters are like @Email, @DATE.
///////////////////////////////////////////////////////////////
i think the data after these two are not inserting
cmd.Parameters.AddWithValue(@DATE, TextBoxDATE.Text);
cmd.Parameters.AddWithValue(@FINYR, TextBoxDATE2.Text);

DATE is predefined variable in sql so please change it to some other name and also check DATE datatype in sql server.
//////////////////////////////////////////////////////////////

Due to this, data in your table3 will be empty.
 
Share this answer
 
v2

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