B
Brad Pears
I am using a function called "CreateSQLParam" which adds SQL parameters to a
collection.
The function is shown below... I add a parameter to a collection using the
following line code...
------------------------------------------------------------------------------------
dim ContractNo as varchar
dim colParms as collection
' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input)
------------------------------------------------------------------------------------
I am getting an error on the "ContractNo" field in the above line that says
"option strict on
disallows narrowing from type 'object' to type 'string' in copying the
value of ByRef
parameter "sValue" back to the matching argument"
Here is the function...
-----------------------------------------------------------------------------------------
Function CreateSQLParam(ByVal sName As String, ByRef 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
-------------------------------------------------------------------------------------------
So in looking at the function, I am passing a varchar value (ContractNo) to
sValue which has been defined as
an object and hence the error message. Short of turning "option strict OFF",
what is the best way to keep
my generic function so that I can pass whatever data type is required to the
functions sValue parameter? It
must somehow mean I need to explicitely define the type of variable coming
in isntead of using object but how
and where would I do this?
Help!!
Thanks, Brad
collection.
The function is shown below... I add a parameter to a collection using the
following line code...
------------------------------------------------------------------------------------
dim ContractNo as varchar
dim colParms as collection
' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNo, SqlDbType.VarChar,
ParameterDirection.Input)
------------------------------------------------------------------------------------
I am getting an error on the "ContractNo" field in the above line that says
"option strict on
disallows narrowing from type 'object' to type 'string' in copying the
value of ByRef
parameter "sValue" back to the matching argument"
Here is the function...
-----------------------------------------------------------------------------------------
Function CreateSQLParam(ByVal sName As String, ByRef 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
-------------------------------------------------------------------------------------------
So in looking at the function, I am passing a varchar value (ContractNo) to
sValue which has been defined as
an object and hence the error message. Short of turning "option strict OFF",
what is the best way to keep
my generic function so that I can pass whatever data type is required to the
functions sValue parameter? It
must somehow mean I need to explicitely define the type of variable coming
in isntead of using object but how
and where would I do this?
Help!!
Thanks, Brad