Error converting data type varchar to numeric. (CASTing)

Hi,

I got problem with SQL statement (SELECT string) generated in C# code.
Field products.product_price in DB type is decimal(18,2).
I want to pass double variable included in SELECT string (below code), but given error "Error converting data type varchar to numeric."
Tried to execute this  statement with same parameter (for example '100.555555' as price_min) in SQL editor in MS DBMS and it works.

Thanks


Double price_min = Convert.ToDouble(filtersPriceMinBox.Text) * Convert.ToDouble(curr_in_euros_rate);
 
adapterSelectStmt += " and (prod.product_price > (CAST ( '" + price_min + "' AS decimal(18, 2))))";

Open in new window

damixaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pcelbaConnect With a Mentor Commented:
The most probably reason is the conversion of your "Double price_min" to varchar value when you are building the condition: ... (CAST ( '" + price_min + "' AS decimal ...

You have to use some explicit conversion because concatenating your double value of price_min to the text string without any conversion will result to scientific notation:

CAST('100.55555e00' AS decimal(18,2))

and this is not properly recognized by SQL engine.
0
 
pcelbaCommented:
The proper conversion seems to be   price_min.ToString("f5")  (Hope you know C# better than me and correct it.)
0
 
damixaAuthor Commented:
I tried price_min.ToString("f5"). It creates good String (5 digit places after dot), but problem is still...
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Rahul Goel ITILSenior Consultant - DeloitteCommented:
price_min.ToString("###.00")
0
 
nkiggsCommented:
Try truncating any spaces to ensure there are no spaces which may not be recognised as valid.
To parse simply use vartype.Parse(input string);
in this case try:
Decimal.Parse(trim(price_min))
0
 
damixaAuthor Commented:
The problem is not about price_min String formatting
0
 
damixaAuthor Commented:
Tried with parsing:                

String str = price_min.ToString("f4");
Decimal.Parse(str.Trim());

adapterSelectStmt += " and (prod.product_price > (CAST ( '" + str + "' AS decimal(18, 2) ) ) )";

but still, same error..

On the other hand, this works:

adapterSelectStmt += " and (prod.product_price > (CAST ( '" + "1000.5555" + "' AS decimal(18, 2) ) ) )";

0
 
damixaAuthor Commented:
Found mistake....
just changed "," to "." in string
0
 
pcelbaCommented:
So, the problem was in conversion of double to string as I mentioned in the first answer (the scientific notation was just one possible reason)... The comma raised from your regional settings probably and without this information is almost impossible to answer correctly.

The best solution is to display the whole SQL command and test it in SQL Management studio.
0
All Courses

From novice to tech pro — start learning today.