472,958 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

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

Jul 16 '07 #1
1 31165
"Brad Pears" <br***@truenorthloghomes.comschrieb
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

Jul 16 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
8
by: Gidi | last post by:
hello, i have a textbox that represnts a date and i want to pare it to a DateTime, but sometimes this textBox can be empty string and i want to send it to the DataBase as null or as empty Date, how...
5
by: ad | last post by:
I have a datetime filed, name Birthday. I use aTexBox.Text= ((DateTime)dr).ToShortDateString(); but it result in an .InvalidCastException: How can I datetime field in a TextBox?
1
by: Thierry | last post by:
Hi, I've got the next problem: I have a datetime field in my database (sql-server) and I want the option in it that this field can be empty. But if I leave that datetime field empty I get...
0
by: Neil Young | last post by:
Hello group, I'm referring to a "rather old thread" (April 21st 2005). Because I also ran into problems with ASP.NET 2.0 formview and DBNull, but found a way around, I would like to share my...
2
by: GossTremontTech | last post by:
Hi, We have a webservice that generates an xml string that has a empty/null datetime field (<OurDate></Ourdate>). When we use ReadXML read in the serialized string we get the error: "string...
3
by: Dabbler | last post by:
How do I display a datetime field that hasn't been assigned a value as blank instead of the default minimum value? I need to do this in both display and edit modes. Thanks!
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
0
by: M | last post by:
I have textboxs bound to numerical fields or datetime fields. these fields are "allow Null". I find I cannot delete the data if the field had data before. If I delete the text of the textbox, the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.