dbTalk Databases Forums  

DateTime Field, Insert a Null value.

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss DateTime Field, Insert a Null value. in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
tclancey
 
Posts: n/a

Default DateTime Field, Insert a Null value. - 04-10-2007 , 06:16 AM






Hi. I'm having some problems getting a Null value into an SQL DateTime
field. I'm all googled out, but I did find a bit of code using the
parameters that looked as if it might do the job, but unfortunately any
nullDate saved in the column is stored as Now. My SQL column is set to
allow nulls. Can ayone show me where I'm going wrong?

Cheers,
Tull.

If DbType = "MSSQL" Then
sql &= ", @User19, @User20);"
Dim con As New SqlConnection(MsSqlConnString)
Dim com As New SqlCommand(sql, con)
com.Parameters.Add(New SqlParameter("@User19", SqlDbType.DateTime))
com.Parameters.Add(New SqlParameter("@User20", SqlDbType.DateTime))
Dim nullDate As SqlDateTime = SqlDateTime.Null
If dpkUser19.Checked = True Then com.Parameters("@User19").Value =
SqlDate(dpkUser19.Value.ToShortDateString) Else
com.Parameters("@User19").Value = nullDate
If dpkUser20.Checked = True Then com.Parameters("@User20").Value =
SqlDate(dpkUser20.Value.ToShortDateString) Else
com.Parameters("@User20").Value = nullDate
con.Open()
com.ExecuteNonQuery()
com.Dispose()
Dim lid As New SqlCommand("SELECT @@IDENTITY", con)
LastID = CLng(lid.ExecuteScalar)
lid.Dispose()
con.Dispose()
End If



Reply With Quote
  #2  
Old   
tclancey
 
Posts: n/a

Default Re: DateTime Field, Insert a Null value. - 04-10-2007 , 06:37 AM






Don't worry, found the problem. I was running code to ensure all controls
on the form were trimmed before saving the data. It seems trying to trim a
DateTime picker control sets off the text changed code, which automatically
checks the control!


"tclancey" <tull (AT) idcodeware (DOT) co.uk> wrote

Quote:
Hi. I'm having some problems getting a Null value into an SQL DateTime
field. I'm all googled out, but I did find a bit of code using the
parameters that looked as if it might do the job, but unfortunately any
nullDate saved in the column is stored as Now. My SQL column is set to
allow nulls. Can ayone show me where I'm going wrong?

Cheers,
Tull.

If DbType = "MSSQL" Then
sql &= ", @User19, @User20);"
Dim con As New SqlConnection(MsSqlConnString)
Dim com As New SqlCommand(sql, con)
com.Parameters.Add(New SqlParameter("@User19", SqlDbType.DateTime))
com.Parameters.Add(New SqlParameter("@User20", SqlDbType.DateTime))
Dim nullDate As SqlDateTime = SqlDateTime.Null
If dpkUser19.Checked = True Then com.Parameters("@User19").Value =
SqlDate(dpkUser19.Value.ToShortDateString) Else
com.Parameters("@User19").Value = nullDate
If dpkUser20.Checked = True Then com.Parameters("@User20").Value =
SqlDate(dpkUser20.Value.ToShortDateString) Else
com.Parameters("@User20").Value = nullDate
con.Open()
com.ExecuteNonQuery()
com.Dispose()
Dim lid As New SqlCommand("SELECT @@IDENTITY", con)
LastID = CLng(lid.ExecuteScalar)
lid.Dispose()
con.Dispose()
End If




Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.