Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi folks,

Stuck with this error for so long.
When directly execute from sql script in SSMS, no issue but from web application i got this error '
Error converting data type varchar to bigint
' .

I use this way in application.
using (SqlConnection conn = new SqlConnection(strconn))

sqlComm.Parameters.Add(new SqlParameter("@para", strPara));


I'm thinking to change the code.

What I have tried:

I'm thinking to change the code.
Posted
Updated 30-Aug-21 8:08am
Comments
Patrice T 29-Aug-21 23:22pm    
You have a secret error in your secret code.

Based on the error message you set a string to the parameter as value and trust that the database does the conversion to bigint for you. This won't happen if the string is invalid on the database side so instead you should explicitly do the conversion at calling side.

First of all define the data type for the parameter using SqlParameterCollection.Add Method (System.Data.SqlClient), Add(String, SqlDbType) [^]

Now before setting the parameter you can use Int64.TryParse Method (System) | Microsoft Docs[^] to parse the text to proper number. Depending on the situation, you may need to remove the thousand separators or change decimal point.

After that you can set the number as the parameter value and the database doesn't need to do any conversion.
 
Share this answer
 
As to solution 1 that cover everything I would do to amend this problem. One further consideration is the mapping from the SQL to SQL. See the Microsoft Docs for the appropriate mapping.

SQL-CLR Type Mapping - ADO.NET | Microsoft Docs[^]
 
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