Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This error come up "Error converting data type nvarchar to bigint." when the user clicks the Submit button. I have two other web forms that connect to the same database and didn't get this error. Here is the error code:

HTML
 Error converting data type nvarchar to bigint.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to bigint.

Source Error:


Line 275:                cmd.Parameters.AddWithValue("@LongName", lblSchool.Text);
Line 276:
Line 277:                cmd.ExecuteNonQuery();
Line 278:            }
Line 279:            else if (temp == 1)



Stack Trace:


[SqlException (0x80131904): Error converting data type nvarchar to bigint.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1787814
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5341674
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1693
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +275
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1421
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +208
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +163
   FINAPP.FINProFormD.ButtonSubmit_Click(Object sender, EventArgs e) in C:\Users\khopkins\Documents\visual studio 2010\Projects\FINAPP\FINAPP\FINProFormD.aspx.cs:277
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9627994
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724


Here is the C# code:

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

            const string cmdStr = "Select count(*) from Table99 where User_ID= @User_ID AND FINYEAR = 2015";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
            userExist.Parameters.AddWithValue("@FINYEAR", TextBoxDATE2.Text);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 0)
            {

                SqlCommand cmd = new SqlCommand("Insert into Table99 (User_ID, TOTAL_REVE, DATE, FINYEAR, INSTRUCTIO, RESEARCH, ACADEMIC_S, NET_AID, AUXILIARY_, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT, EXPENDABLE, TOTALNETASSETS, LongName, SUBMITTED) values (@USer_ID, @TOTAL_REVE, @DATE, @FINYEAR, @INSTRUCTIO, @RESEARCH, @ACADEMIC_S, @NET_AID, @AUXILIARY_, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT, @EXPENDABLE, @TOTALNETASSETS, @LongName, @SUBMITTED);", con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
                cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTRR.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
                cmd.Parameters.AddWithValue("@RESEARCH", TextBoxResPs.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcadSSSIS.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@NET_AID", TextBoxNGAS.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxAOE.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@FINYEAR", TextBoxDATE2.Text);
                cmd.Parameters.AddWithValue("@SUBMITTED", lblFalse.Text);
                cmd.Parameters.AddWithValue("@DATE", TextBoxDATE.Text);
                cmd.Parameters.AddWithValue("@TOTALNETASSETS", TextBoxTNA.Text.Replace(",", ""));
                cmd.Parameters.AddWithValue("@LongName", lblSchool.Text);

                cmd.ExecuteNonQuery();


Did I forget something in my code or did something wrong?
Posted
Updated 9-Apr-21 1:08am
Comments
Afzaal Ahmad Zeeshan 6-May-15 11:20am    
Your database is expecting data in bigint form (number) where as you are passing nvarchar (character) data.
Sergey Alexandrovich Kryukov 6-May-15 11:24am    
5! ;-)
—SA
Afzaal Ahmad Zeeshan 6-May-15 11:29am    
Hehe, thank you sir! :D
Computer Wiz99 6-May-15 11:36am    
Yes, I got that part but how am I passing that data when the other forms don't? They are all the same except each form asks for different information. This form is shorter but needs some of the same data where the other forms are longer needing more data. The data goes to the same database. So if the other two forms don't have this error, why does this one?
Afzaal Ahmad Zeeshan 6-May-15 11:46am    
The problem is that your form submits the correct data. The problem is type mismatch with the data type on your database server and your application. Change either type. You can change the type of required data on database also. That might be because you might be casting the data to the correct type, or you might also have same type on both ends.

1 solution

So if the other two forms don't have this error, why does this one?

Because the error is in one of the textbox contents, not in the form.
Either the values you are loading into the textboxes is going into the wrong textbox / column, it's bad numeric data, or you user has typed invalid data.

Personally - and given these are text boxes it's always a good idea - I'd start my method by validating and converting the textbox inputs with int.TryParse, DateTime.TryParse, and so forth, report any errors, and pass the validated values through to SQL instead of the textbox content directly only if they are all valid.
 
Share this answer
 

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