Solved

null values in Public Property

Posted on 2011-03-12
3
270 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

815 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

6 Experts available now in Live!

Get 1:1 Help Now