By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"Guadala Harry" <GM**@NoSpam.com> wrote in message
news:OO*************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.