parameter.add question - How do you handle null or empty values?

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

Can you tell me how to handle processing when parameter.add adds a parameter in which the value is null or empty?

In my code I add this parameter:

VB.NET:
  objCommand.Parameters.Add(New OleDbParameter("@parameter7", pParameter7))

When this line executes I get an error that states @parameter has no default value.

VB.NET:
            objCommand.ExecuteNonQuery()

Ok, well at least I thought I had it covered in here but I must be missing something:

VB.NET:
  Public Function ExecuteInsertChangeDelete(ByVal pSqlQuery As String,
                                              Optional ByVal pParameter1 As String = Nothing,
                                              Optional ByVal pParameter2 As String = Nothing,
                                              Optional ByVal pParameter3 As String = Nothing,
                                              Optional ByVal pParameter4 As String = Nothing,
                                              Optional ByVal pParameter5 As String = Nothing,
                                              Optional ByVal pParameter6 As String = Nothing,
                                              Optional ByVal pParameter7 As String = Nothing,
                                              Optional ByVal pParameter8 As String = Nothing,
                                              Optional ByVal pParameter9 As String = Nothing,
                                              Optional ByVal pParameter10 As String = Nothing
                                              ) As Boolean

I thought that String = Nothing gives them a default value.

As a quick fix so I can import the data I made sure if any parameter was null I would pass it a single space character but is that the best way to handle that scenario?

Thanks.

Truly,
Emad
 
Last edited:
Nothing is used in VB code to represent null. To represent null in data going to or coming from a database, you use DBNull.Value.

Also, rather than this:
VB.NET:
objCommand.Parameters.Add(New OleDbParameter("@parameter7", pParameter7))
do this:
VB.NET:
objCommand.Parameters.AddWithValue("@parameter7", pParameter7)
This will handle your Nothing to DBNull conversion:
VB.NET:
objCommand.Parameters.AddWithValue("@parameter7", If(CObj(pParameter7), DBNull.Value))
 
Back
Top