Reputation: 45
I'm using SQL Server 2005 and Visual Studio 2008. Working with VB.NET
language.
Colums which I set as Decimal in SQL store values using dots, like "10.5"
When I try to insert a value in the same format, there is no problem. but when I try to insert it like "10,5"
then I have "Error converting data type nvarchar to numeric".
What bugs me is, I need to multiply 2 decimal values which I get from textboxes and insert the result in SQL. I tried Decimal.Parse and some other functions work in that way but I always get the same result. (10.5) * 2 = 210
Somehow the code eliminates the dot and it sees like the value is 105. When I use comma instead of dot, the code works correctly but it cannot insert and says "Error converting data type nvarchar to numeric".
I'm out of options.
Upvotes: 0
Views: 912
Reputation: 112482
Specify the culture you are using
d = Decimal.Parse(s, New CultureInfo("de-DE"))
Or
d = Decimal.Parse(s, CultureInfo.CurrentCulture)
In order to use the culture of your operating system.
Note: The database is not storing the decimal with .
or ,
. It stores it using some binary representation if the column is declared as a decimal
column. Never store a number in a varchar
column. The same is true for a Decimal
variable in VB. It stores the number using a binary representation. The problem of the dot .
or the comma ,
as a decimal separator (i.e. the problem of the right culture) arises only when converting from and to String
.
Take care of passing the parameters to your SQL command by using parameters instead of using string concatenation. That way you don't have to be worried about culture related issues when inserting number and date values into a SQL command. It also prevents you from SQL injection attacks and strings containing apostrophes '
represent no problem either.
Upvotes: 1
Reputation: 555
10,5
isn't decimal for computers.
NEVER save decimal numbers with the colon; it is used to separate functions/values.
And about the 10.5*2 = 210
, it is probably a problem parsing your dec
value. I don't code vb.net
, but it may be simply dropping the .
to calculate it with an int
Upvotes: 0