Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--scenario--

i have two text box and drop down

C#
TMS.API.Reimbursement.VO_Reimbursement_SC objClaimListApproval_SC = new TMS.API.Reimbursement.VO_Reimbursement_SC();
                objClaimListApproval_SC.Hub_ID = ddl_Hub.SelectedItem.Value;//drop down
                objClaimListApproval_SC.UserID = txtUserID.Text;//text box
objClaimListApproval_SC.Claim_amount = Convert.ToDecimal(txt_Claim_Amount.Text);//text box


--properties are declared in
C#
TMS.API.Reimbursement.VO_Reimbursement_SC();


C#
public string Hub_ID
       {
           get;
           set;
       }



C#
public string UserID
       {
           get;
           set;
       }


C#
public decimal Claim_amount
       {
           get;
           set;
       }



----doubt---when i debug--and reach
C#
objClaimListApproval_SC.Claim_amount = Convert.ToDecimal(txt_Claim_Amount.Text);//text box

it goes into catch block.... i get to know that format exception was caught

Input string was not in a correct format.



---If i make the proprty string ,,,and change remove conver.todecimal i get no error
C#
objClaimListApproval_SC.Claim_amount = txt_Claim_Amount.Text;//text box

Why this error is happening ..can we use decimal property ?


I tried this:

objClaimListApproval_SC.Claim_amount = decimal.Parse(txt_Claim_Amount.Text);

//same error

---The text box may be empty or the number that user enters[like 500 or 500.00].

--currently the above explanation is like i was not entering anything...so obviously its empty

--and i was getting input string not in proper format.

--Now question is why cannot we convert the string to decimal and store it in that objClaimListApproval_SC.Claim_amount and pass the xml to stored procedure and do the manipulation

--If i pass string it works fine and in stored procedure i have to use the claim amount as
varchar[declare a variable claim amount as varchar]...and the xml that we fetched which contains claim amount we store it in the variable declared ...and do the manipulation

--I was just thinking like pass decimal value from front end....and pass the xml to stored procedure ....and in stored procedure[declare a variable claim amount as decimal].. and the xml that we fetched which contains claim amount we store it in the variable declared ...and do the manipulation

----But this doesnot work.....that is the doubt
Posted
Updated 17-Jul-13 6:09am
v4
Comments
[no name] 17-Jul-13 9:13am    
The error is telling exactly what the problem is. Whatever txt_Claim_Amount.Text is cannot be converted to a decimal value.
anurag19289 17-Jul-13 9:18am    
Then how to convert to decimal value?
Mike Meinz 17-Jul-13 9:23am    
Maybe there are spaces in the text box.

Try Convert.ToDecimal(txt_Claim_Amount.Text.Trim)

It is also possible that the text box is empty or it could contain a comma or some other character that is not compatible with the ToDecimal method. Use the Visual Studio Interactive Debugger to step through the executing code to see exactly what value is in the text box.
anurag19289 17-Jul-13 12:11pm    
same error...question is improved..

1 solution

It is doing exactly what it should: it is telling you that the data in your textbox is not a number (according to the current Locale for the PC). You job at this point is to tell teh user he typed it wrong, not to try and assume that he entered something slightly wrong and try to "guess" what he should have typed.

Personally, I would not use a textbox for a numeric input: Have a look at the NumericUpDown Control instead - it won't let the user enter an invalid value.
 
Share this answer
 
Comments
Ron Beyer 17-Jul-13 10:00am    
Its probably a correct input, I'm guessing he gets the error because he uses the currency symbol de-jour in the textbox. Instead of using a numeric up/down (try using it in data entry, they suck because it requires mouse entry), create a textbox that only accepts numeric inputs and then either remove currency symbols or use the NumberStyles.AllowCurrencySymbol when using it with decimal.TryParse.

-Ron B.
anurag19289 17-Jul-13 12:12pm    
question is improved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900