A
Alex Stevens
Hi All,
I'm working on my Data access class which has an .AddParamter Function.
(Code is at the end of the message).
One of the parameters of this Function is the value which should entered
into an SQLClient.SQLParameter object.
This is declared as Object because I could be accepting dates, string, ints
into the parameter.
However, I wish to be able check if the value supplied is Nothing and set it
to System.DBNull.Value, so that it is passed to the SQL server correctly as
a Null.
When I pass an int (set to Nothing) into the Value Parameter and check the
varValue object like this in the command window:
varValue = Nothing 'This evaluates to True
varValue Is Nothing 'This evaluates to False.
But I can't use "If varValue = Nothing" in the function the compiler won't
allow it - "Use the IS operator to test for object identity"
But if I use "If varValue Is Nothing" in the function it is incorrect for an
integer.
How can I get my code to correctly recognise an integer passed into an
object and test "varValue = Nothing" and recognise a object passed in and
text "varValue Is Nothing"?????
Many Thanks
Alex
***** CODE START*****
Public Function AddParameter(ByVal strParamName As String, _
ByVal sqlParamType As System.Data.SqlDbType, _
Optional ByVal sqlParamDir As System.Data.ParameterDirection =
ParameterDirection.Input, _
Optional ByVal intSize As Integer = 0, _
Optional ByVal varValue As Object = Nothing, _
Optional ByVal ConvertNothingToNull As Boolean = False) As
SqlClient.SqlParameter
Dim sqlParam As New SqlClient.SqlParameter
If varValue Is Nothing And ConvertNothingToNull Then
End If
With sqlParam
.ParameterName = strParamName
.SqlDbType = sqlParamType
.Direction = sqlParamDir
.Size = intSize
.Value = varValue
End With
m_cmdCommandSQL.Parameters.Add(sqlParam)
Return sqlParam
End Function
***** CODE END*****
I'm working on my Data access class which has an .AddParamter Function.
(Code is at the end of the message).
One of the parameters of this Function is the value which should entered
into an SQLClient.SQLParameter object.
This is declared as Object because I could be accepting dates, string, ints
into the parameter.
However, I wish to be able check if the value supplied is Nothing and set it
to System.DBNull.Value, so that it is passed to the SQL server correctly as
a Null.
When I pass an int (set to Nothing) into the Value Parameter and check the
varValue object like this in the command window:
varValue = Nothing 'This evaluates to True
varValue Is Nothing 'This evaluates to False.
But I can't use "If varValue = Nothing" in the function the compiler won't
allow it - "Use the IS operator to test for object identity"
But if I use "If varValue Is Nothing" in the function it is incorrect for an
integer.
How can I get my code to correctly recognise an integer passed into an
object and test "varValue = Nothing" and recognise a object passed in and
text "varValue Is Nothing"?????
Many Thanks
Alex
***** CODE START*****
Public Function AddParameter(ByVal strParamName As String, _
ByVal sqlParamType As System.Data.SqlDbType, _
Optional ByVal sqlParamDir As System.Data.ParameterDirection =
ParameterDirection.Input, _
Optional ByVal intSize As Integer = 0, _
Optional ByVal varValue As Object = Nothing, _
Optional ByVal ConvertNothingToNull As Boolean = False) As
SqlClient.SqlParameter
Dim sqlParam As New SqlClient.SqlParameter
If varValue Is Nothing And ConvertNothingToNull Then
End If
With sqlParam
.ParameterName = strParamName
.SqlDbType = sqlParamType
.Direction = sqlParamDir
.Size = intSize
.Value = varValue
End With
m_cmdCommandSQL.Parameters.Add(sqlParam)
Return sqlParam
End Function
***** CODE END*****