B
Brad Pears
I have the following code that adds SQLParameter objects to an
SQLParameterCollection object (well at least that's what I am trying to
do!!)
When the line of code runs that adds the parameter (colParams.Add), the
actual function call (CreateSQLParam) executes with no errors but the actual
".add" fails with an "Object reference not set to an instance of an object"
error. At first I thought it was maybe because I did not use the "New"
keyword when dimming colParams but there is no constructor for an
SQLParameterCollection object so that was not the problem. The funny thing
was, this code worked EXACTLY as it is listed below EXCEPT I was using a
standard collection object previously instead of the SQLParameterCollection
object. As soon as I converted it to the SQLParamaterCollection object, I
started getting this error. I can not see where I am going awry but it must
be something quite simple!!!
Here is the code...
Dim colParams As SqlClient.SqlParameterCollection
' Set up the parameters
colParams.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input))
Function CreateSQLParam(ByVal sName As String, ByVal sValue As Object, ByVal
varType As System.Data.SqlDbType, ByVal varDir As ParameterDirection) As
SqlClient.SqlParameter
Dim objParam As SqlClient.SqlParameter
objParam = New SqlClient.SqlParameter()
objParam.ParameterName = sName
If IsNothing(sValue) Then sValue = System.DBNull.Value
objParam.Value = sValue
objParam.SqlDbType = varType
objParam.Direction = varDir
CreateSQLParam = objParam
End Function
SQLParameterCollection object (well at least that's what I am trying to
do!!)
When the line of code runs that adds the parameter (colParams.Add), the
actual function call (CreateSQLParam) executes with no errors but the actual
".add" fails with an "Object reference not set to an instance of an object"
error. At first I thought it was maybe because I did not use the "New"
keyword when dimming colParams but there is no constructor for an
SQLParameterCollection object so that was not the problem. The funny thing
was, this code worked EXACTLY as it is listed below EXCEPT I was using a
standard collection object previously instead of the SQLParameterCollection
object. As soon as I converted it to the SQLParamaterCollection object, I
started getting this error. I can not see where I am going awry but it must
be something quite simple!!!
Here is the code...
Dim colParams As SqlClient.SqlParameterCollection
' Set up the parameters
colParams.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input))
Function CreateSQLParam(ByVal sName As String, ByVal sValue As Object, ByVal
varType As System.Data.SqlDbType, ByVal varDir As ParameterDirection) As
SqlClient.SqlParameter
Dim objParam As SqlClient.SqlParameter
objParam = New SqlClient.SqlParameter()
objParam.ParameterName = sName
If IsNothing(sValue) Then sValue = System.DBNull.Value
objParam.Value = sValue
objParam.SqlDbType = varType
objParam.Direction = varDir
CreateSQLParam = objParam
End Function