Trouble clearing SqlParameters from my Command object

  • Thread starter Elliot M. Rodriguez
  • Start date
E

Elliot M. Rodriguez

(note - sorry for the crosspost - I sent to .vb thinking i was sending to
..adonet).

I implemented a very small, basic data access layer for my web application.
It works just fine, except for this one bug.

One of my methods returns an abstracted dataset. To accomodate X number of
input parameters, I created a function signature that accepts a ParamArray
of SqlParameters as well as the name of the stored proc. In the body of the
function I loop through the param array and append each object to the
Parameters collection of my Command object.

The problem comes up when I try to append a SqlParameter object that I have
already used in a previous call, that is also an argument for the next
stored procedure. For example, I have:

dim objParam1 as New SqlParameter("@userid", intUserid)
dim objDS as DataSet
objDS = objDAL.GetDataSet("storedprocname", objParam1)

' do some stuff with the dataset, now try a new query, different proc, same
parameter
objDS.clear()
objDS = objDAL.GetDataSet("anotherprocname", objParam1)

The error returned is something like "There is already a parameter named
objParam1 in the SqlParametersCollcetion.

Prior to adding each parameter, I check its count and, if greater than 0,
remove each item before appending the new params. In debugging the count is
always 0, so how can there be an SqlParameter object in its collection?

' create our command object with the passed in ProcedureName.

' and sqlparameter object.

Dim objCommand As New SqlCommand(ProcedureName, Me.Connection)

Dim sqlParam As SqlParameter

objCommand.CommandType = CommandType.StoredProcedure

Me.LocalCommand = objCommand

If objCommand.Parameters.Count > 0 Then

' in case any parameters are hanging around, clear them out

For Each sqlParam In objCommand.Parameters

objCommand.Parameters.Remove(sqlParam)

Next

End If

objCommand.Parameters.Clear()

' add the parameters passed in from the ParamArray

' into the command's parameters collection.

For Each sqlParam In DataParameters

sqlParam = objCommand.Parameters.Add(sqlParam) ' ERROR THROWN HERE

Next

What element am I missing please? This has me bending my brain pretty badly.
Thank you in advance.
 
W

William Ryan

For Each sqlParam In DataParameters--Try changing this to
objCommand.Parameters. It looks like DataParameters is a different
reference and I don't see any code that clears or removes the params from it

sqlParam = objCommand.Parameters.Add(sqlParam) ' ERROR THROWN HERE
 
E

Elliot M. Rodriguez

William:

Thank you for your reply.

I dont follow what you mean. Upon each call the value of DataParameters
should change. If I change the line to what you indicated, how will my
function know which parameter object to add to it?
 
D

David Browne

Elliot M. Rodriguez said:
(note - sorry for the crosspost - I sent to .vb thinking i was sending to
.adonet).

I implemented a very small, basic data access layer for my web application.
It works just fine, except for this one bug.

One of my methods returns an abstracted dataset. To accomodate X number of
input parameters, I created a function signature that accepts a ParamArray
of SqlParameters as well as the name of the stored proc. In the body of the
function I loop through the param array and append each object to the
Parameters collection of my Command object.

The problem comes up when I try to append a SqlParameter object that I have
already used in a previous call, that is also an argument for the next
stored procedure. For example, I have:

Why are you trying to reuse SqlParameter's.

SqlCommand, and SqlParameter are lightweight objects which should be used
once and then discarded. If you need a similar parameter later, just create
a new one.

David
 
E

Elliot M. Rodriguez

David:

Thanks for your reply.
Its a clarity issue more than anything. And besides, if I can reuse it, why
not?
 
W

William Ryan

I misread the code...sorry about that.. Let me look at it for a minute. I
think though that instead of a Param Array, you could add your parameters to
an ArrayList and iteratively walk through it adding your params and passing
in the arraylist to the handler function. Let me look at it for a few
mintues...I'll be right back.;

Bill
 
W

William Ryan

Where are you passing in the Command Object. It looks like GetDataSet has a
local command object in it. Is that right? I can't see the call, but from
the function calls, it looks like it probably does. That collection, inside
the class objDAL has a command object and in the first pass, you give it a
parameter named @Userid. Then, in the same instance, you call another
function passing in @UserID.

So in GetDataSet....make sure the paraters are clear out there...not outside
of the instance. If there isn't a command object in the class, then I way
off base, but I suspect there is and this is the problem..


Let me know.

Bill
 
D

David Browne

Elliot M. Rodriguez said:
David:

Thanks for your reply.
Its a clarity issue more than anything. And besides, if I can reuse it, why
not?

It's just that if you do you're on your own for figuring out the
dependencies.
For instance the Command has a reference to the connection, and the
parameter has a reference to the ParameterCollection, which is a field on
the Command. So you have to figure out the correct sequence to detach these
objects and reuse them.

David
 
E

Elliot M. Rodriguez

william:

I noticed that after I stared at your post for a while. But then I got an
unusual error after clearing the collection:
For now I've decided instead to use the other parameters at David's
suggestion, and that is working. It takes away from my encapsulation of my
DAL, but I need to get at least a prototype going on my project.

Thanks guys
 
W

William Ryan

Good deal. I agree with his point too. Just out of curiousity, what
exception was being thrown?
 
E

Elliot M. Rodriguez

Oops. I though I posted it. Off the top of my head, it was something like
"collection may be modified". It was thrown as soon as I attempted to call
the remove method as I looped through the parameter collection.

for each objParam in Me.LocalCommand.Parameters
Me.LocalCommand.Parameters.Remove(objParam) 'thrown here
next objParam
 
D

David Browne

Elliot M. Rodriguez said:
Oops. I though I posted it. Off the top of my head, it was something like
"collection may be modified". It was thrown as soon as I attempted to call
the remove method as I looped through the parameter collection.

for each objParam in Me.LocalCommand.Parameters
Me.LocalCommand.Parameters.Remove(objParam) 'thrown here
next objParam

In general you cannot modify a collection while enumerating its members.
Do something like:

do until Me.LocalCommand.Parameters.Count = 0
Me.LocalCommand.Parameters.Remove(0)
loop

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top