473,756 Members | 3,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Cr eateSQLParam("@ vcContractNo", ContractNo, SqlDbType.VarCh ar,
ParameterDirect ion.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.Sql DbType, ByVal varDir As ParameterDirect ion) As
SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter
objParam = New SqlClient.SqlPa rameter()
objParam.Parame terName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Value = sValue
objParam.SqlDbT ype = varType
objParam.Direct ion = 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 1748
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***@truenort hloghomes.comsc hreef in bericht
news:%2******** **********@TK2M SFTNGP03.phx.gb l...
>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(Cr eateSQLParam("@ vcContractNo", ContractNo,
SqlDbType.VarCh ar, ParameterDirect ion.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.Sql DbType, ByVal varDir As
ParameterDirect ion) As SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter
objParam = New SqlClient.SqlPa rameter()
objParam.Parame terName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Value = sValue
objParam.SqlDbT ype = varType
objParam.Direct ion = 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(Cr eateSQLParam("@ vcContractNo", ContractNo, SqlDbType.VarCh ar,
ParameterDirect ion.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.Sql DbType, ByVal varDir As ParameterDirect ion) As
SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter
objParam = New SqlClient.SqlPa rameter()
objParam.Parame terName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Value = sValue
objParam.SqlDbT ype = varType
objParam.Direct ion = 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******** ******@TK2MSFTN GP04.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***@truenort hloghomes.comsc hreef in bericht
news:%2******** **********@TK2M SFTNGP03.phx.gb l...
>>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(C reateSQLParam(" @vcContractNo", ContractNo,
SqlDbType.VarC har, ParameterDirect ion.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.Sql DbType, ByVal varDir As
ParameterDirec tion) As SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter
objParam = New SqlClient.SqlPa rameter()
objParam.Param eterName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Val ue = sValue
objParam.SqlDb Type = varType
objParam.Direc tion = varDir
CreateSQLPar am = 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.inv alidwrote in message
news:u9******** ******@TK2MSFTN GP03.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***@truenort hloghomes.comsc hrieb
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.SqlPa ramter(<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 SqlParameterCol lection 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 ContractNoObjec t as Object
dim ContractNo as String
dim colParms as collection

' Add a paramter to the collection
colParms.Add(Cr eateSQLParam("@ vcContractNo", ContractNoObjec t ,
SqlDbType.VarCh ar, ParameterDirect ion.Input)

ContractNo = ContractNoObjec t.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.nlschre ef in bericht
news:Ow******** ******@TK2MSFTN GP04.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

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

Similar topics

21
4529
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
3092
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: exceptions.UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 1599: unexpected code byte Using the native Python codec for doing conversions gives me a similar error ('utf8' codec can't decode byte 0x93 in position 1328:...
10
2229
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 me, since any pointer can be cast to void* and void * can be cast to any other pointer so this implies that any pointer can be cast to any other pointer. Is that so?
2
2314
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 them.. is there a sheet somewhere or a refrence for them? all i really need to know is Access ADO.NET Numeric Integer/numeric? Memo VarWChar? Text VarWChar? Date/Time ?...
1
3862
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 module in the vb.net project. The following lines of code are a few of the lines with syntax issues and the description of the issue (below that is the sub (DTSrun) that these lines came from). My request is if someone could explain how to fix...
5
7828
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 implementation takes 2 arguments and returns. I hava read abt user defined structured data types but i seem not to understand their work. anyone pliz help. my db is \ DB2 administration tools level:
5
2311
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 System.CodeDom.CodeCastExpression) __problem typecast #1 Desc:i do needed checks but data/commands in XML is dynamic and i don't wanna fix C# code again and again... Sample:foreach (object some in somearray) (some.GetType())some.someaction();
2
3275
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
1
2962
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... ------------------------------------------------------------------------------------ dim ContractNo as varchar dim colParms as collection
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.