Parameters.Add

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am new to .Net. I have a windows form in which I am using a Stored
Procedure to get the data. I am successful in retrieving the data, however
after closing the form and reopening the form I landed in a problem . The
error is too many parameters.What I did was I included a line

SqlCmd.Parameters.Clear before Parameters.Add - line and I fixed the issue.
Can any explain why this is happening and how I can control it?

thanks
Vb
 
The Parameters collection is exactly that, a collection. If the item is
already in there, you just need to set the value.

if(cmd.Parameters.Count > 0){
cmd.Parameters[0].Value = //Whatever

}
else{
cmd.Parameters.Add["Wahtever", SqlDbType.int];
cmd.Parameters["Whatever"].Value = //Whatever
}
 
Thanks for the response,

What I am doing is that having a global Parameters object and use the same
object for every Database call. Do I have to clear the parameters all the
time or is there a better way.

Thanks
Vb



W.G. Ryan eMVP said:
The Parameters collection is exactly that, a collection. If the item is
already in there, you just need to set the value.

if(cmd.Parameters.Count > 0){
cmd.Parameters[0].Value = //Whatever

}
else{
cmd.Parameters.Add["Wahtever", SqlDbType.int];
cmd.Parameters["Whatever"].Value = //Whatever
}

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
vb said:
I am new to .Net. I have a windows form in which I am using a Stored
Procedure to get the data. I am successful in retrieving the data, however
after closing the form and reopening the form I landed in a problem . The
error is too many parameters.What I did was I included a line

SqlCmd.Parameters.Clear before Parameters.Add - line and I fixed the issue.
Can any explain why this is happening and how I can control it?

thanks
Vb
 
If it's global, the last parameters used will always be there. You need to
clear them before reusing the object.

Jeff
vb said:
Thanks for the response,

What I am doing is that having a global Parameters object and use the same
object for every Database call. Do I have to clear the parameters all the
time or is there a better way.

Thanks
Vb



W.G. Ryan eMVP said:
The Parameters collection is exactly that, a collection. If the item is
already in there, you just need to set the value.

if(cmd.Parameters.Count > 0){
cmd.Parameters[0].Value = //Whatever

}
else{
cmd.Parameters.Add["Wahtever", SqlDbType.int];
cmd.Parameters["Whatever"].Value = //Whatever
}

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
vb said:
I am new to .Net. I have a windows form in which I am using a Stored
Procedure to get the data. I am successful in retrieving the data, however
after closing the form and reopening the form I landed in a problem . The
error is too many parameters.What I did was I included a line

SqlCmd.Parameters.Clear before Parameters.Add - line and I fixed the issue.
Can any explain why this is happening and how I can control it?

thanks
Vb
 
Back
Top