Converting TextBox input to decimal

Posted on 2006-04-25
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
    LVL 6

    Assisted Solution

    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.
    LVL 6

    Assisted Solution

    Oops ignore that, I didn't realize that there is a Convert.ToDecimal... my fault for the confusion...
    LVL 23

    Accepted Solution

    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

    LVL 25

    Assisted Solution

    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

    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

    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

    lol - happens to us all!



    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now