473,396 Members | 2,013 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,396 software developers and data experts.

Reusing parameters

I am trying to reuse my parameters to re-call my stored procedures but get
the error:

The SqlParameter with ParameterName '@UserID' is already contained by
another SqlParameterCollection.

My code is essentially:
************************************************** *************
Dim parameters As SqlParameter () = { _
New SqlParameter("@UserID",SqldbType.BigInt) }

parameters(0).value = session("UserID")

for i as integer = 0 to 1
dbReader = myDbObject.RunProcedure("GetProfileList", parameters)
StoredProfiles.DataSource=dbReader
StoredProfiles.DataValueField="ApplicantProfileID"
StoredProfiles.DataTextField= "ProfileName"
StoredProfiles.databind()
StoredProfiles.Items.Insert(0, new ListItem("Select Stored Search",""))
if not (StoredProfiles.Items.FindByText("Default")) is nothing then
StoredProfiles.Items.FindByText("Default").Selecte d = true
SelectProfile(StoredProfiles.SelectedValue)
exit for
else
Call AddNewProfileDefault()
parameters(0).value = new SqlParameter("@UserID",SqldbType.Int)
parameters(0).value = session("UserID")
DBReader.Close()
end if
next
************************************************** *************

What the above code does is look for a list of profiles. If there is no
"Default" profile, it creates one and then goes back does the
"GetProfileList" Stored Procedure again (2nd loop). But the 2nd time gets
the error.

I thought that if I created another SqlParameter (new SqlParameter) that it
would be pointing another SqlParameter but apparently not. I also tried
closing the DBReader to see if this would help but it didn't.

How can I make this work?

Thanks,

Tom

Feb 6 '07 #1
2 2032
On Mon, 5 Feb 2007 16:56:21 -0800, "tshad" <t@home.comwrote:

¤ I am trying to reuse my parameters to re-call my stored procedures but get
¤ the error:
¤
¤ The SqlParameter with ParameterName '@UserID' is already contained by
¤ another SqlParameterCollection.
¤
¤ My code is essentially:
¤ ************************************************** *************
¤ Dim parameters As SqlParameter () = { _
¤ New SqlParameter("@UserID",SqldbType.BigInt) }
¤
¤ parameters(0).value = session("UserID")
¤
¤ for i as integer = 0 to 1
¤ dbReader = myDbObject.RunProcedure("GetProfileList", parameters)
¤ StoredProfiles.DataSource=dbReader
¤ StoredProfiles.DataValueField="ApplicantProfileID"
¤ StoredProfiles.DataTextField= "ProfileName"
¤ StoredProfiles.databind()
¤ StoredProfiles.Items.Insert(0, new ListItem("Select Stored Search",""))
¤ if not (StoredProfiles.Items.FindByText("Default")) is nothing then
¤ StoredProfiles.Items.FindByText("Default").Selecte d = true
¤ SelectProfile(StoredProfiles.SelectedValue)
¤ exit for
¤ else
¤ Call AddNewProfileDefault()
¤ parameters(0).value = new SqlParameter("@UserID",SqldbType.Int)
¤ parameters(0).value = session("UserID")
¤ DBReader.Close()
¤ end if
¤ next
¤ ************************************************** *************
¤
¤ What the above code does is look for a list of profiles. If there is no
¤ "Default" profile, it creates one and then goes back does the
¤ "GetProfileList" Stored Procedure again (2nd loop). But the 2nd time gets
¤ the error.
¤
¤ I thought that if I created another SqlParameter (new SqlParameter) that it
¤ would be pointing another SqlParameter but apparently not. I also tried
¤ closing the DBReader to see if this would help but it didn't.
¤

The problem is that you've already added the parameter @UserID to the collection. If you want to
reuse the parameter then just change the value instead of adding a new SQLParameter object.
Paul
~~~~
Microsoft MVP (Visual Basic)
Feb 6 '07 #2
"Paul Clement" <Us***********************@swspectrum.comwrote in message
news:51********************************@4ax.com...
On Mon, 5 Feb 2007 16:56:21 -0800, "tshad" <t@home.comwrote:

¤ I am trying to reuse my parameters to re-call my stored procedures but
get
¤ the error:
¤
¤ The SqlParameter with ParameterName '@UserID' is already contained by
¤ another SqlParameterCollection.
¤
¤ My code is essentially:
¤ ************************************************** *************
¤ Dim parameters As SqlParameter () = { _
¤ New SqlParameter("@UserID",SqldbType.BigInt) }
¤
¤ parameters(0).value = session("UserID")
¤
¤ for i as integer = 0 to 1
¤ dbReader = myDbObject.RunProcedure("GetProfileList", parameters)
¤ StoredProfiles.DataSource=dbReader
¤ StoredProfiles.DataValueField="ApplicantProfileID"
¤ StoredProfiles.DataTextField= "ProfileName"
¤ StoredProfiles.databind()
¤ StoredProfiles.Items.Insert(0, new ListItem("Select Stored
Search",""))
¤ if not (StoredProfiles.Items.FindByText("Default")) is nothing then
¤ StoredProfiles.Items.FindByText("Default").Selecte d = true
¤ SelectProfile(StoredProfiles.SelectedValue)
¤ exit for
¤ else
¤ Call AddNewProfileDefault()
¤ parameters(0).value = new SqlParameter("@UserID",SqldbType.Int)
¤ parameters(0).value = session("UserID")
¤ DBReader.Close()
¤ end if
¤ next
¤ ************************************************** *************
¤
¤ What the above code does is look for a list of profiles. If there is no
¤ "Default" profile, it creates one and then goes back does the
¤ "GetProfileList" Stored Procedure again (2nd loop). But the 2nd time
gets
¤ the error.
¤
¤ I thought that if I created another SqlParameter (new SqlParameter) that
it
¤ would be pointing another SqlParameter but apparently not. I also tried
¤ closing the DBReader to see if this would help but it didn't.
¤

The problem is that you've already added the parameter @UserID to the
collection. If you want to
reuse the parameter then just change the value instead of adding a new
SQLParameter object.
Actually, I just found the problem. It was my error.

It was not that I was using @UserID, but that I was applying it to
parameters(0).value instead of parameters(0) - (without the .value).

After I did that, it worked fine.

Thanks,

Tom
>

Paul
~~~~
Microsoft MVP (Visual Basic)

Feb 6 '07 #3

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

Similar topics

7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
0
by: Bhavna | last post by:
Hi im still hving a problem with my Identity value that is returned after my insert query What i have done is inserted a row into a datatable and then used an dataAdapters UPDATE command to...
9
by: Alan | last post by:
Using VC++ (1998) compiler with PFE32 editor in Win2K Pro SP4. (DigitalMars CD on order. ) The program (below) instantiates a class and then deletes it. I would have thought that reusing the...
4
by: Old Wolf | last post by:
#include <stdio.h> #include <stdarg.h> Is this safe: void foo(const char *fmt, ...) { va_list ap; va_start(ap,fmt);
2
by: tshad | last post by:
I have some code that is exactly the same except for 1 name. In the "for loops" below the 2nd parameters change as they are pointing to separate string arrays in the structure: ...
4
by: Kufa | last post by:
Hi, I might post a stupid question, but i prefer give it a shot, as i tried several things without any success. In a function called with a varying number of arguments of varying types, could...
3
by: neonk | last post by:
I create a number of Global ADO Commands on Application Load. Every 30 seconds I use these to recreate Recordsets (Most of the RS are not global, so are recreated each time instead of just being...
1
by: jfbaro | last post by:
Hi, I am new to C# and SQLite and I am having problems using Paramaters.Add... I prepare the "INSERT" statement and inside a loop I set the values for each parameter and call...
2
by: qmunke | last post by:
I've inherited some code which previously went something like: for (x:y) { } which I replaced with
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:
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: 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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.