TextBox Convert it to Decimal? ....Help!!
-
Wednesday, March 28, 2012 11:39 PM
Hi,
I have a textbox aka TXT_Weighting in which I place a value of %.
If I put in a number 0 < number < 100, it works.
However, If I put in 100, I get this error: "error converting Data Type numeric to decimal"
The Database Field has the DataType of decimal(10,2).
I have tried converting it to decimal, string, int, float in the application, but once it saves, gives the same error.
I know it is because of the database field having the type as decimal. But I cant stop the entire application, just so I can change the data type of the field.
How do I save it without getting this error? How do I control it from the application?
Regards,
Shav
Edit:
Initially, it was this:
.Parameters("@weighting").Value = Val(Me.Txt_Weighting.Text)I tried this, doesn't work.
.Parameters("@weighting").Value = Convert.ToDecimal(Txt_Weighting.Text)- Edited by Shavendra Chand Wednesday, March 28, 2012 11:43 PM
All Replies
-
Thursday, March 29, 2012 12:28 AM
Convert.ToDecimal should work. Here it does:
Dim dec As Decimal dec = Convert.ToDecimal("100")So, the conversion itself is not the problem.
EDIT: Forget what I wrote below as 2 is the number of decimal places.
As the number 100 has 3 decimal places, there's an error if you specified a precision of only 2.
(Good to not use the Val function. It's ancient and not compatible to the rest of the .Net Framework's parsing/formatting handling.)
Armin
- Edited by Armin Zingler Thursday, March 29, 2012 12:31 AM
- Marked As Answer by Shanks ZenMicrosoft Contingent Staff, Moderator Friday, March 30, 2012 5:07 AM
-
Thursday, March 29, 2012 12:33 AMYou wrote the type in the database is decimal (10,2), but what's the type of the @weighting parameter?
Armin
-
Thursday, March 29, 2012 12:47 AM
As follows:
.Parameters.Add("@weighting", SqlDbType.Decimal)
-
Thursday, March 29, 2012 1:44 AM
Doesn't the 2 indicate the number of decimal places after the decimal point?
I mean 100.00
-
Thursday, March 29, 2012 1:55 AM
Doesn't the 2 indicate the number of decimal places after the decimal point?
I mean 100.00
Yes, that's right. That's why I wrote in bold that you should ignore that answer of mine. You were probably quicker. :-)
I'm afraid, currently I don't know why it does not work.
Armin
- Edited by Armin Zingler Thursday, March 29, 2012 1:56 AM
-
Thursday, March 29, 2012 3:43 AM
Hi Armin,
I resolved it. I overlooked the Stored Procedure, The stored procedure had the value as decimal(4,2).
Updated it, works like a charm. Our code was totally okay.
Thanks for your time buddy.
Shav
-
Friday, March 30, 2012 11:12 AMShanks, it's nice of you to mark my message as the answer, but I think it isn't. :-) Shavendra found the solution on his own in his last message.
Armin

