Last post Mar 30, 2009 04:01 AM by Paul Linton
Aug 08, 2008 12:13 PM|cutekids525|LINK
I am trying to assign values to parameters of a stored proc from my class file.
I got the parameters by using the following code :
Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
SqlParameter cmdParameters = discoverCmdParameters(practiceConnectionString, "Register_Insert");
string hashKey = practiceConnectionString + ":" + spname;
cachedParameters = (SqlParameter)paramCache[hashKey];
if (cachedParameters == null)
cachedParameters = (SqlParameter)(paramCache[hashKey] = discoverCmdParameters(practiceConnectionString, spname));
I am trying to attach these parameters to SqlCommand object by using following code :
string connString = ConfigurationManager.ConnectionStrings["practiceConnectionString"].ConnectionString;
SqlConnection cn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(storedprocName,cn);
foreach (SqlParameter p in cmdParameters)
At this point I am getting error as : The SqlParameter is already contained by another SqlParameterCollection
Can anyone help me how to solve this problem.
Aug 08, 2008 04:26 PM|rjcox|LINK
You need to create new SqlParameter instances for each SQL execution.
Aug 22, 2008 03:49 AM|MSW7|LINK
Try adding a clear after execution;
Mar 29, 2009 10:38 PM|crtjr|LINK
I wish some of you would know exactly what you're talking about or try some of these suggestions before you post them. Clearing the parameters collection does NOT help. I have the same problem and that was the first thing I tried.
Mar 29, 2009 10:49 PM|crtjr|LINK
Unfortunately what I did was recreate the parameter collection every time... it's a good thing that I do not expect this piece of code to run constantly or else the work would be too much. XML data type will be the way to go from now on.
Mar 30, 2009 03:48 AM|PDillon|LINK
Thanks, adding the clear statement worked for me.
Mar 30, 2009 04:01 AM|Paul Linton|LINK
I don't know exactly what I am talking about and I certainly have not attempted to mirror your system from the brief information you have given. If you don't like the suggestion I am about to make then I am perfectly happy to refund all your money (some
people offer double your money back but I am not that generous).
You might want to try cloning before you add the parameter to the new collection
PS - You might get better free, volunteer, unpaid, gratis assistance from a SqlServer or database forum.