PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.

Converting an empty textbox to a null datetime field...

 
 
Brad Pears
Guest
Posts: n/a
 
      16th Jul 2007
I am using vb.net 2005 and SQL server 2000. In my table I have a date field
of type "smalldatetime". In my vb application, the user may or may not enter
a date value into the appropriate text box. I then want to pass the value of
this text box as a datetime variable to my stored procedure which inserts or
updates the row.

As I mentioned above, the textbox may be left blank - hence I would want to
pass a null to the stored proc. How do I convert from an empty string
(textbox) to a datetime field so as to pass a null value into my stored
procedure?

I know that I could just skip sending this parameter at all and let my
stored procedure do it but I am using a class and I really do not want to
place any real logic in there. I just want to pass what I have - and that
could be a null value if the text box is left empty.

Every time I attempt to set a variable defined as datetime =
cdate(txtdateField.text) or by using Convert.ToDateTime(txtdatefield.text) I
get an error that "Conversion from 'DBNull' to type 'Date' is not valid."

Does anyone have any experience with what I am describing here and if so,
how did you get around it? Basically I just want to convert an empty string
to a null datetime variable!

Thanks, Brad



 
Reply With Quote
 
 
 
 
Armin Zingler
Guest
Posts: n/a
 
      16th Jul 2007
"Brad Pears" <(E-Mail Removed)> schrieb
> I am using vb.net 2005 and SQL server 2000. In my table I have a
> date field of type "smalldatetime". In my vb application, the user
> may or may not enter a date value into the appropriate text box. I
> then want to pass the value of this text box as a datetime variable
> to my stored procedure which inserts or updates the row.
>
> As I mentioned above, the textbox may be left blank - hence I would
> want to pass a null to the stored proc. How do I convert from an
> empty string (textbox) to a datetime field so as to pass a null
> value into my stored procedure?
>
> I know that I could just skip sending this parameter at all and let
> my stored procedure do it but I am using a class and I really do not
> want to place any real logic in there. I just want to pass what I
> have - and that could be a null value if the text box is left empty.
>
> Every time I attempt to set a variable defined as datetime =
> cdate(txtdateField.text) or by using
> Convert.ToDateTime(txtdatefield.text) I get an error that
> "Conversion from 'DBNull' to type 'Date' is not valid."
>
> Does anyone have any experience with what I am describing here and
> if so, how did you get around it? Basically I just want to convert
> an empty string to a null datetime variable!



'loading:

If FieldFromDatabase Is DBNull.Value then
txtDatefield.text = string.empty
else
txtDatefield.text = FieldFromDatabase.ToString
end if


'saving (no validation included!):

if txtDateField.text.length = 0 then
'assign DBNull.Value to the destination. If you
'use Parameters with an SQLCommand object, you set
'the parameter's value to DBNull.Value
<destination> = DBNull.Value
else
<Destination> = cdate(txtDateField.text)
end if


Of course, you should put the code into procedures, or, derive your own
Textbox (eg NullableDatetimeTextbox) from the Textbox class that handles all
this.


Armin

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTime as empty String or null, How can i Do it? =?Utf-8?B?R2lkaQ==?= Microsoft C# .NET 8 27th Aug 2004 08:50 PM
converting Unix datetime count to .Net DateTime object Mark Worrall Microsoft Dot NET Framework 2 13th Feb 2004 06:52 PM
Re: converting Unix datetime count to .Net DateTime object Mark Worrall Microsoft C# .NET 1 4th Feb 2004 08:29 AM
converting Unix datetime count to .Net DateTime object Mark Worrall Microsoft Dot NET 1 4th Feb 2004 07:54 AM
Setting datetime to null/empty? jib Microsoft ASP .NET 1 28th Jan 2004 08:39 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:46 PM.