[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Converting a TextBox value to a Decimal

Posted on 2007-11-23
5
Medium Priority
?
1,629 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:digital_slavery
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:digital_slavery
ID: 20341632
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 total points
ID: 20341636
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20341638
does you textbox has some character different from digits and point sign?
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20342333
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
 
LVL 1

Author Closing Comment

by:digital_slavery
ID: 31410840
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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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