Solved

null values in Public Property

Posted on 2011-03-12
3
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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