Converting TextBox input to decimal

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?

Thanks!
MadRiverLizardAsked:
Who is Participating?
 
aprestoCommented:
or another option is decimal.Parse

decimal.Parse( yourval )

the difference between parse and Convert.To...

PARSE:

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

CONVERT.TO...

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

Apresto
0
 
PoeticAudioCommented:
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.
0
 
PoeticAudioCommented:
Oops ignore that, I didn't realize that there is a Convert.ToDecimal... my fault for the confusion...
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dstanley9Commented:
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.
0
 
MadRiverLizardAuthor Commented:
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 !
0
 
MadRiverLizardAuthor Commented:
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.
0
 
aprestoCommented:
lol - happens to us all!

Thanks

Apresto
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.