473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problems with data type conversions..

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

Jul 10 '07 #1
1 2952
Brad,

Firstly, I'm not sure why you're trying to do this since the contstructor
for SqlParameter is overloaded, allowing you to do something like this:
colParms.Add ( New SqlClient.SqlParamter(<any number of parameters !>) )
see this for the possible combinations you can use this link...
http://msdn2.microsoft.com/en-us/lib...parameter.aspx

Also, in .Net 2.0 there's a SqlParameterCollection so that they don't need
to be stored in a generic collection.
http://msdn2.microsoft.com/en-us/lib...ction.add.aspx

Secondly, you're parameter in your function is not only an Object, but it's
also a reference type, (ByRef). This means that it's going to try to
implicitly copy the details from an Object to a String, but the compiler
doesn't know whether this is possible.
Either change the function definition so that the parameter so that it's
"ByVal sValue As Object" (not ByRef), or change do the following:

dim ContractNoObject as Object
dim ContractNo as String
dim colParms as collection

' Add a paramter to the collection
colParms.Add(CreateSQLParam("@vcContractNo", ContractNoObject ,
SqlDbType.VarChar, ParameterDirection.Input)

ContractNo = ContractNoObject.ToString()

"Brad Pears" <br***@truenorthloghomes.comwrote in message
news:e%****************@TK2MSFTNGP06.phx.gbl...
>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

Jul 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
8
by: Thomas | last post by:
I am currently passing email messages over XML_RPC as the payload for a certain function call. On some of these messages, XML_RPC blows up on the server side and says something to the effect of: ...
10
by: vb | last post by:
Hi all, I am a newbie in C and i want to know what all pointer conversions are "legal" according to ANSI C standard. For Example, int* to char*, some_struct* to char* and so on .. According to...
2
by: Brian Henry | last post by:
is there info about data type conversions between access to ado.net? I have tried a lot of the ones that say oledb data types should work for and get a lot of "data type mismatch" errors using...
1
by: Rich | last post by:
Hello, I created a DTS package VB6 script with DTS from Sql Server 2000. In a vb.net project I add a reference to Microsoft DTSpackage object library and copy the code from the DTS script to a...
5
by: Akaketwa | last post by:
guys help me on this rather nubish question.I have created a new data type in java and i also want to create a new data type object in db2 that will store the money data object. The money...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
11
by: Brad Pears | last post by:
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... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.