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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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

Thanks

Apresto
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.