473,387 Members | 3,801 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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

' 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
11 1721
Brad,

First as you get no answers, you can be sure nobody knows the answer, so
please don't repeat.

Two questions to you, why you are using by ref, I have the idea that you are
interpreting that wrong secondly why do you think that when you tell that
you want to pass an object that it will pass everything else too as by
instance values?

Cor
"Brad Pears" <br***@truenorthloghomes.comschreef in bericht
news:%2******************@TK2MSFTNGP03.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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

' 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
Brad Pears wrote:
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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"
But it's "String" in VB.NET, not "varchar"...

Andrew
Jul 11 '07 #3
Brad Pears wrote:
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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

' 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
As you are passing the reference by reference, the data type of the
reference has to match exactly. However, if you don't plan to change the
reference from within the method, there is no reason to pass the
reference by reference. Just remove the ByRef keyword.

You can remove the ByVal keywords also, that is the default way that
parameters are passed. Stick with the default, unless you actually need
to pass a parameter by reference.

--
Göran Andersson
_____
http://www.guffa.com
Jul 11 '07 #4
Sorry about that. I just wanted to rephrase part of my question so I deleted
the original, changed the content and reposted...

I am using by ref because "sValue" could be any type of datatype - and that
is why this function is using type "object"... The passed value could be of
decimal type, string type, boolean type, datetime type etc.. etc..

Otherwise I would have to check what the datatype of the object is before
and then I would have to call a fucntion that was customized for that
particular data type. You can see why I would not want to do that. I want
to use just this one function as a generic function to do the work
regardless of what type is passed in...

When you pass an object it does pass everything about that object - that is
the whole idea of being able to pass an object. For isntance, if you pass a
"collection" object, you are passing all the items in the collection as
well... So whatever function or procedure is then passed collection will be
able to use items in that collection...

Brad

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...
Brad,

First as you get no answers, you can be sure nobody knows the answer, so
please don't repeat.

Two questions to you, why you are using by ref, I have the idea that you
are interpreting that wrong secondly why do you think that when you tell
that you want to pass an object that it will pass everything else too as
by instance values?

Cor
"Brad Pears" <br***@truenorthloghomes.comschreef in bericht
news:%2******************@TK2MSFTNGP03.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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

' 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 #5
Sorry Andrew, that was a typo... I didn;t copy/paste this code - I just
entered it... Yes, it is defined as string in VB. I have been working with
SQL server so much that I accidentally tryped in varchar there instead...

Brad
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
Brad Pears wrote:
>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 vcContractNo as varchar
dim colParms as collection
vcContractNo = "07-00001"

But it's "String" in VB.NET, not "varchar"...

Andrew

Jul 11 '07 #6
"Brad Pears" <br***@truenorthloghomes.comschrieb
Sorry about that. I just wanted to rephrase part of my question so I
deleted the original, changed the content and reposted...

I am using by ref because "sValue" could be any type of datatype -
and that is why this function is using type "object"... The passed
value could be of decimal type, string type, boolean type, datetime
type etc.. etc..

Otherwise I would have to check what the datatype of the object is
before and then I would have to call a fucntion that was customized
for that particular data type. You can see why I would not want to
do that. I want to use just this one function as a generic function
to do the work regardless of what type is passed in...
You don't have to use ByRef to be able to declare it As Object.

Use ByRef /only/ if you want to pass something back to the calling
procedure.
When you pass an object it does pass everything about that object -
that is the whole idea of being able to pass an object. For
isntance, if you pass a "collection" object, you are passing all the
items in the collection as well... So whatever function or procedure
is then passed collection will be able to use items in that
collection...
We have to distinguish between reference types and value types:

If you pass a value type ByVal, a complete copy of the object, that means a
copy of all the fields in the object is put on the stack.

If you pass a reference type ByVal, a copy of the reference to the object is
put on the stack. The object is not copied. In the called procedure you have
a reference to the same object as the caller has.

If you pass a value type ByRef, a reference to the object is passed.

If you pass a reference type ByRef, a reference to the passed variable
(holding the reference to the object) is passed.
In addition, there are boxed value types: If you assign a value type to an
"As Object" variable, the variable will contain a reference to the boxed
object (which is still a value type). Boxing is done implicitly.
Armin

Jul 11 '07 #7
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()

Jul 12 '07 #8
Goran,

I agree with you almost everything, however is it not that you are talking
about C#. I tried it and it does not work. Although my IDE makes from it
nicely the correct VB code even in VBNet 2003.

(-:

Cor
>
You can remove the ByVal keywords also, that is the default way that
parameters are passed. Stick with the default, unless you actually need to
pass a parameter by reference.

--

Jul 13 '07 #9
Had to be

:-)

Cor
"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:Ow**************@TK2MSFTNGP04.phx.gbl...
Goran,

I agree with you almost everything, however is it not that you are talking
about C#. I tried it and it does not work. Although my IDE makes from it
nicely the correct VB code even in VBNet 2003.

(-:

Cor
>>
You can remove the ByVal keywords also, that is the default way that
parameters are passed. Stick with the default, unless you actually need
to pass a parameter by reference.

--


Jul 13 '07 #10
Cor Ligthert [MVP] wrote:
Goran,

I agree with you almost everything, however is it not that you are talking
about C#.
No, obviously it's not, as there are no ByRef and ByVal keywords in C#.
I tried it and it does not work.
Exactly what did not work?
Although my IDE makes from it
nicely the correct VB code even in VBNet 2003.

(-:

Cor
>You can remove the ByVal keywords also, that is the default way that
parameters are passed. Stick with the default, unless you actually need to
pass a parameter by reference.

--


--
Göran Andersson
_____
http://www.guffa.com
Jul 13 '07 #11
"Cor Ligthert [MVP]" <no************@planet.nlschrieb
Brad,

In addition to the as forever very good explanation from Armin.
I see it coming: One day, we will have a Skype call. :-) (..why not?)
Armin
Jul 13 '07 #12

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...
1
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.