Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi guys..
In my application i getting exception

The SqlParameter is already contained by another SqlParameterCollection...

C#
public string InsertRetailer(Business.Retailers objRetailer)
        {
            string retInsertRet = string.Empty;
            try
            {
              
                using (SqlConnection conn = Connection.OpenConnection())
               {
                   
                   SqlCommand cmd = new SqlCommand();
                   cmd.CommandText = "INSERT_RETAILER";
                   cmd.CommandType = System.Data.CommandType.StoredProcedure;
                   cmd.Connection = conn;
                   SqlParameter[] sqlParams=new SqlParameter [8];

                   SqlParameter RetailerCode = new SqlParameter("@RetailerCode", objRetailer.RetailerCode);
                   RetailerCode.Direction = System.Data.ParameterDirection.Input;
                   RetailerCode.DbType = System.Data.DbType.String;
                   sqlParams[0] = RetailerCode;

                   SqlParameter RetailerName = new SqlParameter("@RetailerName", objRetailer.RetailerName);
                   RetailerName.Direction = System.Data.ParameterDirection.Input;
                   RetailerName.DbType = System.Data.DbType.String;
                   sqlParams[1] = RetailerName;

                   SqlParameter EVD_No = new SqlParameter("EVD_No", objRetailer.EVD_No);
                   EVD_No.Direction = System.Data.ParameterDirection.Input;
                   EVD_No.DbType = System.Data.DbType.String;
                   sqlParams[2] = EVD_No;

                   SqlParameter Address = new SqlParameter("@Address", objRetailer.Address);
                   Address.Direction = System.Data.ParameterDirection.Input;
                   Address.DbType = System.Data.DbType.String;
                   sqlParams[3] = Address;

                   SqlParameter OpeningBalance = new SqlParameter("@OpeningBalance", objRetailer.OpeningBalance);
                   OpeningBalance.Direction = System.Data.ParameterDirection.Input;
                   OpeningBalance.DbType = System.Data.DbType.Currency;
                   sqlParams[4] = OpeningBalance;

                   SqlParameter WorkingHours = new SqlParameter("@WorkingHours", objRetailer.WorkingHours);
                   WorkingHours.Direction = System.Data.ParameterDirection.Input;
                   WorkingHours.DbType = System.Data.DbType.Double;
                   sqlParams[5] = WorkingHours;

                   SqlParameter ContactPerson_Name = new SqlParameter("@ContactPerson_Name", objRetailer.ContactPerson_Name);
                   ContactPerson_Name.Direction = System.Data.ParameterDirection.Input;
                   ContactPerson_Name.DbType = System.Data.DbType.String;
                   sqlParams[6] = ContactPerson_Name;

                   SqlParameter ContactPerson_Phone = new SqlParameter("@ContactPerson_Phone", objRetailer.ContactPerson_Phone);
                   ContactPerson_Phone.Direction = System.Data.ParameterDirection.Input;
                   ContactPerson_Phone.DbType = System.Data.DbType.Double;
                   sqlParams[7] = ContactPerson_Phone;


C#
cmd.Parameters.AddRange(sqlParams);
                   cmd.ExecuteNonQuery();
                   retInsertRet = Msg.Value.ToString();



need your help
Thnxx in advance
Posted
Updated 8-Oct-13 1:41am
v2
Comments
Azee 8-Oct-13 7:43am    
Which line throws the Exception?
indrajeet jadhav 8-Oct-13 7:47am    
cmd.Parameters.AddRange(sqlParams);
Azee 8-Oct-13 7:58am    
Is there a possibility that you are using the sqlParams more than 1 for multiple commands?
indrajeet jadhav 8-Oct-13 8:15am    
No..
thatraja 8-Oct-13 8:05am    
what's the exception message?

For the given below line you are missing the "@" symbol. Don't know if this is the exact reason, but Microsoft sometime throws strange error messages for simple errors!!!! :)

C#
SqlParameter EVD_No = new SqlParameter("EVD_No", objRetailer.EVD_No);
 
Share this answer
 
Try to get rid of your array and add your SqlParameters one by one ; this will, for sure, be much more debuggable.
Unless there's some code you do not show, the use of the array does not provide any added value here ; it just makes the debugging harder.
 
Share this answer
 

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