473,387 Members | 1,573 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.

Referencing parameters collection member by Name bombs?

Hi all,
I am working on porting an application from VB6 to VB.NET 2003 and am
running into some problems. When declaring and populating the parameters for
a SQL Stored Procedure by using the SQLParameter() collection and trying to
reference a particular parameter by name rather than index I get a Type
Conversion error. But when declaring a SqlClient.SqlCommand object and then
adding the parameters to the command object parameters collection I have no
problem referencing a parameter by name. Why should there be a difference?
To illustrate here is some simple code examples.

1. Using the SQLParameter() collection...

Dim parameters As SqlParameter() = { _
New SqlParameter("@DocRef", SqlDbType.VarChar, 30), _
New SqlParameter("@EdiDocID", SqlDbType.Int, 4), _
New SqlParameter("@myDocID", SqlDbType.Int, 4) _
}

parameters("@DocRef").Value = DocRef
'parameters(0).Value = DocRef '(commented out for testing)
parameters(1).Value = EdiDocID
parameters(2).Value = myDocIdent

That example produces a type conversion error when I try to substitute the
parameter name string "@DocRef" for the index integer. However:

2. Adding parameters to the Command object...

Dim cmd As SqlClient.SqlCommand
cmd = New SqlClient.SqlCommand
cmd.Parameters.Add("@DocRef", SqlDbType.VarChar, 30)
cmd.Parameters.Add("@EdiDocID", SqlDbType.Int, 4)
cmd.Parameters.Add("@myDocID", SqlDbType.Int, 4)

cmd.Parameters("@DocRef").Value = DocRef
cmd.Parameters("@EdiDocID").Value = EdiDocID
cmd.Parameters("@myDocID").Value = myDocIdent

This example works just fine. I don't understand why there should be a
difference.

Here is the resulting problem.
When I am passing 150 parameters to populate an Orders table, having to
reference the parameters by numerical index can be cumbersome and very error
prone, especially when fields are added or removed from the table. I am
stuck with using the first method for reasons that I won't go into at this
time. Is there any way to use the parameter name to derive the index value
so I can don't have to renumber the list of parameter value assignments when
a parameter in the called SQL Procedure is removed or added somewhere in the
middle of the list?

Thanks for your input.....
Oct 6 '06 #1
1 2486
In your first example you should be using

Dim parameters as SQLParameterCollection
John Kotuby wrote:
Hi all,
I am working on porting an application from VB6 to VB.NET 2003 and am
running into some problems. When declaring and populating the parameters for
a SQL Stored Procedure by using the SQLParameter() collection and trying to
reference a particular parameter by name rather than index I get a Type
Conversion error. But when declaring a SqlClient.SqlCommand object and then
adding the parameters to the command object parameters collection I have no
problem referencing a parameter by name. Why should there be a difference?
To illustrate here is some simple code examples.

1. Using the SQLParameter() collection...

Dim parameters As SqlParameter() = { _
New SqlParameter("@DocRef", SqlDbType.VarChar, 30), _
New SqlParameter("@EdiDocID", SqlDbType.Int, 4), _
New SqlParameter("@myDocID", SqlDbType.Int, 4) _
}

parameters("@DocRef").Value = DocRef
'parameters(0).Value = DocRef '(commented out for testing)
parameters(1).Value = EdiDocID
parameters(2).Value = myDocIdent

That example produces a type conversion error when I try to substitute the
parameter name string "@DocRef" for the index integer. However:

2. Adding parameters to the Command object...

Dim cmd As SqlClient.SqlCommand
cmd = New SqlClient.SqlCommand
cmd.Parameters.Add("@DocRef", SqlDbType.VarChar, 30)
cmd.Parameters.Add("@EdiDocID", SqlDbType.Int, 4)
cmd.Parameters.Add("@myDocID", SqlDbType.Int, 4)

cmd.Parameters("@DocRef").Value = DocRef
cmd.Parameters("@EdiDocID").Value = EdiDocID
cmd.Parameters("@myDocID").Value = myDocIdent

This example works just fine. I don't understand why there should be a
difference.

Here is the resulting problem.
When I am passing 150 parameters to populate an Orders table, having to
reference the parameters by numerical index can be cumbersome and very error
prone, especially when fields are added or removed from the table. I am
stuck with using the first method for reasons that I won't go into at this
time. Is there any way to use the parameter name to derive the index value
so I can don't have to renumber the list of parameter value assignments when
a parameter in the called SQL Procedure is removed or added somewhere in the
middle of the list?

Thanks for your input.....
Oct 6 '06 #2

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

Similar topics

14
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code...
2
by: russb_69 | last post by:
Russ here. I'm new to this forum. I need help in understanding why the following code does not work. Basically, I'm defining a structure in a c# application, and using a c++ dll to modify it. ...
3
by: Jozef | last post by:
Hi Folks! I'm trying to reference a specific table using the containers collection. I'm trying to use my help file to find out more about how to accomplish this, but for some reason, when I...
9
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of...
0
by: Ryan Mitchley | last post by:
I am compiling under Visual C++ 2005 Express (with the Platform SDK installed). I have a class that is responsible for creating and repeatedly running a Timeslice() method using a thread: ...
2
by: Axel | last post by:
Hi, a question about something that seems very simple at first glance: is it possible to reference other controls of a subform in a query window without referencing through the parent form? I...
9
by: Alan | last post by:
Hmmm, I'm not too good with the syntax of referencing a subreport. I have frmInvoice which has the invoice details (e.g. ProductCode, ProductCost etc) in the subform frmInvoiceDetails. I'm trying...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
6
by: Lars Uffmann | last post by:
Hi everyone, I'm trying to execute the same function call with 1 parameter different in a switch statement of a member function. However, if a certain condition is true (and I can check that...
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: 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:
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
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?
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,...
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.