Solved

Error converting data type varchar to numeric. (CASTing)

Posted on 2009-07-03
9
1,048 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 42

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 42

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 42

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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