Converting TextBox input to decimal

Posted on 2006-04-25
Medium Priority
Last Modified: 2012-05-05
I have a stored procedure that expects a decimal value. I am getting that value from a TextBox's Text property. The Text will only be two digits e.g. 11, or 25. I need to convert this Text to decimal type for use in a stored procedure that expects a decimal(3,2) type. I have tried adding "0." or "." to the beginning of the TextBox's Text property and then converting that string to decimal without success. I have also converted the Text to an int and divided it by 100, without success either. Can you tell me an easier way to do this, and if the Decimal types (C# and SqlType) are not the same?

Question by:MadRiverLizard
  • 2
  • 2
  • 2
  • +1

Assisted Solution

PoeticAudio earned 300 total points
ID: 16534449
decimal dec = (decimal)Convert.ToDouble(textBox1.Text);

There is no error handling above though, for example if the user types in an invalid value then things will get hairy, so you might throw it in a try-catch and to what you need to do if there is an error.

Assisted Solution

PoeticAudio earned 300 total points
ID: 16534465
Oops ignore that, I didn't realize that there is a Convert.ToDecimal... my fault for the confusion...
LVL 23

Accepted Solution

apresto earned 300 total points
ID: 16534551
or another option is decimal.Parse

decimal.Parse( yourval )

the difference between parse and Convert.To...


You have to convert your value into a string before you can parse it - using ToString()


You Dont need to convert it to string first therefore saving you that extra nanosecond on processing time :o)

NOTE : Most types have a parse method - int.parse, short.parse, bool.parse, but remember, the values have to be a stirng representation of what you want to parse -you cant do this for example:     bool.Parse( "Pancake" ), or int.Parse("Hello World")

Good luck

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 25

Assisted Solution

dstanley9 earned 150 total points
ID: 16534587
Also, no, the native decimal type and SqlDecimal are not the same type.  You can implicitly convert, though:

SqlDecimal sd = new SqlDecimal(1.01);
decimal d = 2.12M;

SqlDecimal sd1 = (SqlDecimal)d;

decimal d1 = (decimal)sd;

You can also pares directly to a SqlDecimal:

SqlDecimal sd = SqlDeicmal.Parse("1.02");

Make sure you use a try/catch blick to check for malformed strings.

Author Comment

ID: 16535271
I have not had any success with the above tips. I have checked the code that supplies the values to the stored procedure and the values are formatted as: 0.12 as c# type decimal. The stored procedure type is decimal, the database column is (Sql2005) Decimal(3,2). After insertion into the database, the column in question (TxnFee) is always 0.00.

I need the value in the databse to be a percentage, e.g. (.06). Is there an easier way to convert a TextBox string of 2 numbers to a type that will insert into the database as .NN? I am not against changing any of the types along the process from form, c# code, SqlCommand, StoredProc, SqlDatabse type.

Thanks again for your help !

Author Comment

ID: 16535590
Found my problem:

I did not have the decimal's scale or precision set in the Stored Procedure ( Decimal(3,2)).

Thanks for the inspiration. I'll split points to all.
LVL 23

Expert Comment

ID: 16535596
lol - happens to us all!



Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

850 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