Reputation: 428
I have an sql column which holds a decimal value..lets say latitude which comes from a textbox.
Latitude decimal(20,16),
Now i want to check if textbox is empty or contains non-decimal charaters,if yes..it should insert a default value like '0' or if everything is in place..just insert the decimal.
I have tried the below :
int latoutput;
string latinput = txtLatitude.Text;
bool latresult = int.TryParse(latinput, out latoutput);
if(latresult)
{
cmd.Parameters.AddWithValue("@Latitude", Convert.ToDecimal(latinput));
}
else
{
cmd.Parameters.AddWithValue("@Latitude", null);
}
When the execute, even when the textbox contains valid decimal it goes on to the ELSE block and throws an exception that @Latitude was not provided.
Here there are two problems :
Thanks in advance.
Upvotes: 0
Views: 1553
Reputation: 21887
You're using int.TryParse
instead of decimal.TryParse
. If you enter anything with a decimal point, i.e. 123.45
then int.TryParse
will fail. Update your code to use decimal.TryParse
:
bool latresult = decimal.TryParse(latinput, out latoutput);
To answer your second question, decimal columns are nullable in a database if they are defined as nullable. I can't determine what you should be passing into your procedure without knowing what your schema looks like nor what your procedure looks like.
Upvotes: 4