Converting a TextBox value to a Decimal

This is driving me nuts! I have had a sniplet of code that has been working for awhile but now it has stopped working and I can not figure out why. All I am trying to do is convert a TextBox value into a decimal so I can store it in my db. pretty simple right? I have the TextBox pre populated with a 0 so that a value can be passed if the user is not going to add any hours for this product. I feel like an idiot right now cuz I nothing I do seems to solve the problem.
decimal manHours;
            string mh = manHoursTextBox.Text;
            manHours = Convert.ToDecimal(mh);
 
 
It started throwing an error if no value was in the textbox, so I tried this:
 
if (string.IsNullOrEmpty(manHoursTextBox.Text) == false)
        {
            Session["mh"] = Decimal.Parse(manHoursTextBox.Text);
        }

Open in new window

LVL 1
digital_slaveryAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
you can use:
decimal.TryParse(mh, out manHours);

the TryParse never generates an exception and returns true if conversion was successful, false if not.
0
 
digital_slaveryAuthor Commented:
Also it does not matter what value I enter into the TextBox it still throws the same error:

Failed to convert parameter value from a String to a Decimal.
0
 
Jaime OlivaresSoftware ArchitectCommented:
does you textbox has some character different from digits and point sign?
0
 
Göran AnderssonCommented:
The error doesn't occur in the code that you are showing. The error message comes from the database, so somewhere you are putting a string value in a parameter that the database can not convert to a Decimal value.

Show what you are doing with the value up to where you send the query to the database.
0
 
digital_slaveryAuthor Commented:
Sorry guys, it was a late night and I overlooked the 2 other columns that were set as money which are also decimal values, which is where the problem really was, I was not passing a value from those 2. Note to self: money is also a decimal!
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.