Important alert: (current site time 7/15/2013 8:51:00 AM EDT)
 

article

Accepting Nullable Dates

Email
Submitted on: 1/4/2005 6:38:23 PM
By: Pamela Pennington 
Level: Intermediate
User Rating: By 6 Users
Compatibility: VB.NET
Views: 13583
 
     This article explains how to save a null date value to a SQL Server table. After working on this for 2 days and finding a solution I thought it would be nice to share this! It is not obvious.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				The .NET datetime variable data type does not accept null as a value. If you are using SQL Server, you can get around this by making your field variables SqlDateTime data type. The SqlDateTime data type implements the nullable option. 
To illustrate how to use this, 
Create a web form (VB.NET) that contains two textboxes called "txtaltstartdate" and "txtaltenddate". They can be databound to the corresponding fields in the SQL Server database.
Also add a "Save" button.
View the code
At the top of your class, put
Imports System.Data.SqlTypes
In the code behind for the "Save" button, you would place the following code:
Dim objaltstartdate As TextBox
Dim objaltenddate As TextBox
Dim sqldatenull As SqlDateTime
sqldatenull = SqlDateTime.Null
objaltstartdate = CType(e.Item.FindControl("txtaltstartdate"), TextBox)
objaltenddate = CType(e.Item.FindControl("txtaltenddate"), TextBox)
' Then, open your database connection and 
' assuming you have a stored procedure to handle 
' updates it would be processed as follows, 
' passing the value:
Dim objSQLConnection As New SqlConnection
Dim objSQLCommand As New SqlCommand
Dim strConnectString As String
strConnectString = ConfigurationSettings.AppSettings("gstrConnection")
objSQLConnection = New SqlConnection(strConnectString)
objSQLConnection.Open()
objSQLCommand = New SqlCommand("sp_update", objSQLConnection)
objSQLCommand.CommandType = CommandType.StoredProcedure
' One tricky thing is you have to use the 
' SqlDateTime.Parse option to take the string 
' value in the text box and convert it to 
' sqlDatetime.
If objaltstartdate.Text <> "" Then
objSQLCommand.Parameters.Add(New SqlParameter("@pAlternateEventStartDate", SqlDbType.DateTime))
objSQLCommand.Parameters("@pAlternateEventStartDate").Value = SqlDateTime.Parse(objaltstartdate.Text)
else
objSQLCommand.Parameters.Add(New SqlParameter("@pAlternateEventStartDate", SqlDbType.DateTime))
objSQLCommand.Parameters("@pAlternateEventStartDate").Value = sqldatenull
endif
If objaltenddate.Text <> "" Then
 objSQLCommand.Parameters.Add(New SqlParameter("@pAlternateEventEndDate", SqlDbType.DateTime))
 objSQLCommand.Parameters("@pAlternateEventEndDate").Value = SqlDateTime.Parse(objaltenddate.Text)
else
 objSQLCommand.Parameters.Add(New SqlParameter("@pAlternateEventEndDate", SqlDbType.DateTime))
 objSQLCommand.Parameters("@pAlternateEventEndDate").Value = sqldatenull
endif
objSQLCommand.ExecuteNonQuery()
objSQLCommand.Dispose()
objSQLConnection.Close()
objSQLConnection.Dispose()
objSQLCommand = Nothing
objSQLConnection = Nothing


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/5/2005 9:57:26 AMTrey Smith

Thanks for the tip!
(If this comment was disrespectful, please report it.)

 
2/9/2005 6:50:00 AM

Hi, I read your solution about storing a null date into an SQL table.

But using VB6 as front-end, I'm just not able to store a null date into a date-field in MS-Access.

Could you please help ?

Thanks.
(If this comment was disrespectful, please report it.)

 
2/9/2005 11:14:39 AMPamela Pennington

Hello, Below is a procedure I was using in my VB5 apps to set a date to null in an ACCESS database:
=====================================
Public Sub DeleteDate(strTableName As String, tmpstrWhere As String, strFieldToDeleteTag As String)

Dim tmpdb As Database
Dim tmpUpdStr As String

Set tmpdb = OpenDatabase(gsdatabase)

tmpUpdStr = "UPDATE " & strTableName & " SET " & strFieldToDeleteTag & " = NULL WHERE " & tmpstrWhere

tmpdb.Execute tmpUpdStr
DoEvents

tmpdb.Close

End Sub
=====================================
Hope this helps!
Pam
(If this comment was disrespectful, please report it.)

 
4/17/2006 9:05:34 PMOceanic

Thank you for sharing as I have been look for this... Thank you
(If this comment was disrespectful, please report it.)

 
10/5/2007 4:26:21 PMJohn

Actually the correct way to do it is this:
In .Net code check the value and see if it's Null. If so then don't pass the value to the stored procedure.

In the stored procedure Set the defalut value to Null.

This way if in .Net it's null don't do anyting else send the value. The stored prodedure will handle the rest.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.