Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
foreach (var item in BranchContactList)
                    {
                        var varInsertBranchContactResult = db.Database.ExecuteSqlCommand("TempspInsertBranchContact @BranchID,@FirstName,@LastName,@Designation,@Department,@PhoneNo1,@PhoneNo2,@MobileNo1,@MobileNo1,@EmailAddress1,@EmailAddress2,@CreatedBy,@CreatedUser,@BranchContactIDOut OUT",
                        new SqlParameter("BranchID", BranchID),
                        new SqlParameter("FirstName", item.FirstName.ToString()),
                        new SqlParameter("LastName", item.LastName.ToString()),
                        new SqlParameter("Designation", item.Designation.ToString()),
                        new SqlParameter("Department", item.Department.ToString()),
                        new SqlParameter("PhoneNo1", item.PhoneNo1.ToString()),
                        new SqlParameter("PhoneNo2", item.PhoneNo2.ToString()),
                        new SqlParameter("MobileNo1", item.MobileNo1.ToString()),
                        new SqlParameter("MobileNo2", item.MobileNo2.ToString()),
                        new SqlParameter("EmailAddress1", item.EmailAddress1.ToString()),
                        new SqlParameter("EmailAddress2", item.EmailAddress2.ToString()),
                        new SqlParameter("CreatedBy", UserID),
                        new SqlParameter("CreatedUser", User), outParamBranchContactID).ToString().ToList();
                        var BranchContactID = (int)outParamBranchContactID.Value;

                      

                    }



How to solve this problem in this particular scenario.

I know cmd.Parameters.Clear() is an option but i cant use it in this scenario.

Note : foreach runs for 3 times. Once successfully. For 2nd time it throws an error :
C#
Error: The SqlParameter is already contained by another SqlParameterCollection

.
.
Posted

1 solution

Most likely it's your OUT parameter outParamBranchContactID. You don't define a new parameter for this execution. Instead you use a predefined parameter which may already have been associated with another command.

Try creating a new parameter and use it in the call.
 
Share this answer
 
Comments
Abrar Kazi 16-Jan-16 4:10am    
Yes you were right. I tried removing the outParamBranchContactID n it worked fine. Thank you so much Mika you saved me a lot of time.
Wendelius 16-Jan-16 4:18am    
You're most welcome :)
Abrar Kazi 16-Jan-16 7:01am    
Could you please look into this i am very much stuck into it.

http://www.codeproject.com/Questions/1072174/How-to-notify-Onsuccess-in-Ajax-BeginForm-as-false
bahman.01 22-Nov-17 0:49am    
I don't have a SqlParameterCollection. It is only one SqlParameter that by each calling the method, a new instance is created of SqlParameter but I counter with this message. What should I do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900