Solved

null values in Public Property

Posted on 2011-03-12
3
269 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:doctor069
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35117375
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

0
 
LVL 16

Accepted Solution

by:
MikeMCSD earned 500 total points
ID: 35117398

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

0
 

Author Closing Comment

by:doctor069
ID: 35117742
That did the trick. Thanks for your help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now