Link to home
Start Free TrialLog in
Avatar of doctor069
doctor069Flag for Canada

asked on

null values in Public Property

Hi -

I have a public property (see code below) that could possibly be null  when I go to write it to a db via insert I get an error even though my db table allows it to be null. If there is a value it works fine.

I thing I have to check for null and return it as db.null but nothing I do seems to work...

Thanks in advance
Public Class Info
    Private _TestId As String
    Public Property TestId() As String
        Get
            Return _TestId
        End Get
        Set(ByVal value As String)
            _TestId = value
        End Set
    End Property
End Class

'sql - i wont bore you with to many details here - it works if there is a value
  myCommand.Parameters.AddWithValue("@TestId", p3.TestId)


'--------------------------------------------------------------------
'here is what it tried
  myCommand.Parameters.AddWithValue("@TestId", isNull(p3.TestId))

 Private Function isNull(ByVal Field As Object) As Object
        If String.IsNullOrEmpty(Field.ToString) = True Then
            Return DBNull.Value
            MsgBox("nothing")
        Else
            Return Field
        End If
    End Function

--------------------------------------------------------------------

 Private Function isNull(ByVal Field As Object) As Object
        If Field Is Nothing Then
            Return DBNull.Value
            MsgBox("nothing")
        Else
            Return Field
        End If
    End Function

Open in new window

Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

What is the exact error you get? Have you tried simply not adding the parameter if the value is null? i.e.
If Not String.IsNullOrEmpty(p3.TestId) Then
     myCommand.Parameters.AddWithValue("@TestId", p3.TestId)
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctor069

ASKER

That did the trick. Thanks for your help