views:

251

answers:

2

I have a smalldatetime column in SQL that can have a null value, but when I try to insert a null value using SqlTypes.SqlDateTime.Null and LINQtoSQL it puts in 1/1/1900 instead of NULL.

What is the best method to insert a null, or am I doing it at the moment. If I am using the correct method at the moment, what should be done to prevent the 1/1/1900 being displayed on my fields in a WPF application.

The database is SQL 2008.

Thanks

(edit) Code

Public Shared Function saveNewClient(ByVal clientName As String, ByVal active As Boolean, ByVal maintRenew As SqlTypes.SqlDateTime, ByVal currOp As Int32) As Int32
Try
 Dim c As New client()
 c.crGUID = Guid.NewGuid
 c.crClientName = clientName
 c.crMaintenanceRenewal = maintRenew
 c.crActive = active
 c.crAddOp = currOp
 c.crAddDate = DateTime.Now
 c.crEditOp = currOp
 c.crEditDate = DateTime.Now

 db.clients.InsertOnSubmit(c)
 db.SubmitChanges()
 Return c.crID
Catch ex As Exception
 Return Nothing
End Try

End Function

A: 

Have you tried null rather than SqlTypes.SqlDateTime.Null? Does the database table row show null?

Rippo
It needs to be SqlTypes.SqlDateTime.Null or the LINQ will not insert/update... unless of course it is an actual date. Previously using sprocs I could submit a properly formatted string, ie '20090808 08:00:00' or just String.Empty and it would essentially ignore work as expected. However with LINQtoSQL recognising the datatype it wants something either in the Date format or a proper Sql null.
TravisPUK
+2  A: 

All you need to do is use a nullable datatype. Looking at the signature of your function it looks like you are passing in an SqlDateTime for maintRenew.

ByVal maintRenew As SqlTypes.SqlDateTime

Instead, try passing a nullable DateTime e.g.:

ByVal maintRenew As DateTime?

Rather than passing in a DBNull, in the calling function you can then pass in a standard "Nothing", (null in (C#), or DateTime as required.

JamesPickrell
Hey thanks JamesPickrell, once again you have come through with the solution that has worked perfectly.
TravisPUK