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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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
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.)
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.)