loading an empty value into a date object using vb.net

D

David Lozzi

Howdy,

I am trying to accomplish the following:

dim DateSend as date

if txtDateSend.text = "" then
DateSend = dbnull.value
else
DateSend = txtDateSend.text
end if

however, the script screams about "Value of type 'System.DBNull' cannot be
converted to 'Date'." How do I send a Null back? This variable, DateSend, is
then sent to a stored procedure and written back to the database. I read
other posts on google groups stating users switched the variables from date
to string, then did a cast within the proc. Is this really the best
solution? Come on, Microsoft must have a way of loading an empty value into
a date object.

thanks!!

David Lozzi
 
T

Teemu Keiski

DateTime is a value type (Date maps to DateTime with no time set), so it
cannot be set to null or dbnull (only reference types can be set to null
reference and even them cannot be set to dbnull.value, except object type
itself).

You can have DateTime.MinValue to indicate that the value has not been set
or is empty, and before setting the value to the parameter, check the
DateTime being MinValue and based on that pass DBNull straight to the proc
(or use default values in procs so you'd need to apss only parameters having
a value).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top