Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am try to convert string to a decimal. but after execution it say intput string not in correct format while updating.

i am providing 12567 as input in textbox. I have add it with sqldbtype.decimal at backend.

here is conde snip:
C#
cmd.Parameters.Add("@Amount ",SqlDbType.Decimal).Value = Convert.ToDecimal (obentity.Amount);

this line give error.

user is free to enter string or decimal value to textbox

thanks in advnace
Posted
Comments
The Doer 28-Mar-12 1:07am    
can u show your textBox source file..

First, convert it before you try to access any SQL - if the user has entered a silly amount, you should tell him instead of continuing.
C#
string s = "1234.56";
double d;
if (double.TryParse(s, out d))
    {
    ...
    }


Second, you shouldn't use Parameters.Add - it has been depreciated for a long time, and superseded by AddWithValue.
C#
double d;
if (double.TryParse(obentity.Amount, out d))
    {
    cmd.Parameters.AddWithValue("@Amount ", d);
    ...
    }
This assumes that obentity.Amount is a string, containing a should-be-a-double value.
 
Share this answer
 
Comments
balongi 28-Mar-12 1:29am    
what should statement come in first if block. and what is the purpose of out?
OriginalGriff 28-Mar-12 3:06am    
"what should statement come in first if block" - please try to say this a different way, I am not sure what you are asking.
"what is the purpose of out" - out is required by the TryParse method: it tells it that the parameter may or may not have a value on entry, but that the variable itself can be changed by the method - in indicates a REFERENCE parameter rather than a VALUE parameter. (For more details, google "out c#" and look at the MSDN entry)
balongi 28-Mar-12 3:12am    
i am try to ask you that:
if (double.TryParse(s, out d))
{
...
} what code come in this block
OriginalGriff 28-Mar-12 3:36am    
What ever you should do if the user entered a valid number.
TryParse returns true if the conversion was ok, and false if there was a problem. So if it returns false, inform your user instead of trying to submit anything to SQL.
if the user enters the numeric value then its showing right result. If user enters the string values like- "a","adasd","asdjfksl". Then its not able to convert to decimal.
So better you put some (javascript/serverSide) validations to the textbox so that user may enter only numeric values(23,34.343,43322etc) .



All the Best.
 
Share this answer
 
Comments
balongi 28-Mar-12 1:35am    
use entering only the numeric value not a string. user can enter 23, or 23.5

but then it giving same error. i want if user enter 23, it automatically add 23.0 to in it. if enter decimal value value need as it.

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