|
-
Jul 12th, 2007, 01:25 AM
#1
Thread Starter
Just Married
[2005] Save DBNULL
Hi all
How to save dbnull in the sql server database
basically i have datetype column in table and on the form i have chacked dtpicker
if the dtpicker is chacked then it save date other wise it save DBNull
Last edited by shakti5385; Jul 12th, 2007 at 08:04 AM.
-
Jul 12th, 2007, 08:06 AM
#2
Thread Starter
Just Married
Re: [2005] Save DBNULL
any idea
It Saving 1 Jan 1900 in the database when the dtpicker is not checked!!
-
Jul 12th, 2007, 08:13 AM
#3
Re: [2005] Save DBNULL
I use the method discussed in this thread:
http://www.vbforums.com/showthread.p...79#post2881879
Just change the # to single qoutes ( ' )
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 12th, 2007, 08:27 AM
#4
Re: [2005] Save DBNULL
YEs... that's what databases do.... a bit odd that it's 1 Jan 1900 and not 31 Dec 1899 though.... but that's how NULL dates are stored in most databases.
-tg
-
Jul 12th, 2007, 08:28 AM
#5
Re: [2005] Save DBNULL
Why? I can save NULL to a datetime field in a database with out any problems?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jul 12th, 2007, 09:01 AM
#6
Member
Re: [2005] Save DBNULL
If you want to do it through VB.Net try this
Code:
MyDataTable.Rows(i)("YourDateFieldName") = DBNull.Value
If you want to do it through SQL try this :
YourDateFieldName= NULL
-
Jul 12th, 2007, 09:14 AM
#7
Re: [2005] Save DBNULL
If you use a parameterized query you can set the parameter value to DBNull.Value
This is my ExecuteQuery Function
it accepts Parameters in the form of @param,Value
If Value is empty it sets the parameter value to DBNull.Value
Code:
Public Function ExecuteQuery(ByVal SQL As String, ByVal ParamArray Parameters As String()) As Boolean
Dim con As New SqlClient.SqlConnection(ConnString)
Dim oCmd As New SqlClient.SqlCommand(SQL, con)
If Not Parameters Is Nothing Then
Dim split As String()
For Each p As String In Parameters
Dim delimeter As Char() = {","c}
split = p.Split(delimeter, 2)
If split(1) = String.Empty Then
oCmd.Parameters.AddWithValue(split(0), DBNull.Value)
Else
oCmd.Parameters.AddWithValue(split(0), split(1))
End If
Next
End If
Try
con.Open()
If oCmd.ExecuteNonQuery() > 0 Then
ExecuteQuery = True
Else
ExecuteQuery = False
End If
Catch ex As Exception
ErrorMessage = ex.Message
ExecuteQuery = False
Finally
If Not oCmd Is Nothing Then
oCmd.Dispose()
End If
End Try
Return ExecuteQuery
End Function 'Parameter Query
-
Jul 12th, 2007, 09:32 AM
#8
Re: [2005] Save DBNULL
Of course, your column must be declared as nullable in the first place.
-
Jul 12th, 2007, 10:46 AM
#9
Re: [2005] Save DBNULL
I didn't say you COULDN'T save a NULL value.... in every database I've ever stuffed a NULL value into a DateTime field ends up being 12/31/1899 0:00:00.000
it's led to some problems during data extractions.
-tg
-
Jul 12th, 2007, 10:48 AM
#10
Re: [2005] Save DBNULL
I don't seem to get that back. When I post a null to the database the field is null. On return I test for NULL and process accordingly.
Sometimes the Programmer
Sometimes the DBA
Mazz1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|