Solved

Error converting data type varchar to numeric. (CASTing)

Posted on 2009-07-03
9
1,046 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:damixa
9 Comments
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 24771388
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
 
LVL 41

Expert Comment

by:pcelba
ID: 24771427
The proper conversion seems to be   price_min.ToString("f5")  (Hope you know C# better than me and correct it.)
0
 

Author Comment

by:damixa
ID: 24771475
I tried price_min.ToString("f5"). It creates good String (5 digit places after dot), but problem is still...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 24771498
price_min.ToString("###.00")
0
 
LVL 2

Expert Comment

by:nkiggs
ID: 24771500
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
 

Author Comment

by:damixa
ID: 24771519
The problem is not about price_min String formatting
0
 

Author Comment

by:damixa
ID: 24771610
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
 

Author Comment

by:damixa
ID: 24771801
Found mistake....
just changed "," to "." in string
0
 
LVL 41

Expert Comment

by:pcelba
ID: 24771838
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now