Solved

null values in Public Property

Posted on 2011-03-12
3
268 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
Comment Utility
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
Comment Utility

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
Comment Utility
That did the trick. Thanks for your help
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now