?
Solved

Converting TextBox input to decimal

Posted on 2006-04-25
7
Medium Priority
?
3,547 Views
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?

Thanks!
0
Comment
Question by:MadRiverLizard
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Assisted Solution

by:PoeticAudio
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.
0
 
LVL 6

Assisted Solution

by:PoeticAudio
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...
0
 
LVL 23

Accepted Solution

by:
apresto earned 300 total points
ID: 16534551
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
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

by:dstanley9
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.
0
 

Author Comment

by:MadRiverLizard
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 !
0
 

Author Comment

by:MadRiverLizard
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.
0
 
LVL 23

Expert Comment

by:apresto
ID: 16535596
lol - happens to us all!

Thanks

Apresto
0

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