Issue with zero-length string as input to Decimal typed parameter.

  • Thread starter Thread starter Guadala Harry
  • Start date Start date
G

Guadala Harry

What do to about conversion to decimal type when blank is allowed?

The situation is this. In the UI there is a textbox into which users can
enter a value which is supposed to be a dollar amount. The value is optional
(i.e., leaving the textbox blank is perfectly acceptable). The problem is
that I pass the UI values down to the DAL for insertion or updation (yes -
I'm coining a new word) into the DB.

Behind the UI I was initially using this next line to pass the value of the
textbox down to the DAL method which expects a decimal typed parmeter:
Convert.ToDecimal(txtPrice.Text)

Of course that line chokes when the user leaves the textbox blank because a
zero-length string (the blank textbox Text property value) cannot be
converted to a decimal. I'm not going to convert the zero-length string
to/from zero just to satisfy the need to pass a decimal typed value around:
because zero and null are both meaningful in this application (with distinct
meanings).

So - what do I do? Do I pass txtPrice.Text as a string all the way down to
the stored procedure and let the stored procedure determine what to do about
a blank value?

Thoughts, rationalle?

Thanks!

-GH
 
Hello

object value;
if(txtPrice.Text.Length == 0)
value = DBNull.Value;
else
value = Convert.ToDecimal(txtPrice.Text);

Then pass value to the query. I assume you use parameterized queries.

Best regards,
Sherif
 
Back
Top