Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
first of all sorry about my english
Based on a condition i need to insert a row . i have to insert the row three times
i am having three checkboxes. and i am generating random integer for my subscription id. if the check box is true i am adding parameters to it but it is generating error

error : the variable name '@subid' has already been declared. variable names must be unique within a query batch or stored procedures

Code :


C#
string insertsubscriptionquery = " INSERT INTO Subscription(SubscriptionId,TypeOfMagazine) values (@subid,@TypeOfMagazine)";
SqlCommand subcmd = new SqlCommand(insertsubscriptionquery,connstring);
if (checkbox1.IsChecked == true)
                {
                    subcmd.Parameters.AddWithValue("@subid", subscripid1);
                    subcmd.Parameters.AddWithValue("@TypeOfMagazine", magtype1);
                     subcmd.ExecuteNonQuery();
                     
                }
                if (checkbox2.IsChecked == true)
                {
                    subcmd.Parameters.AddWithValue("@subid", subscripid2);
                    subcmd.Parameters.AddWithValue("@TypeOfMagazine", magtype2);
                    subcmd.ExecuteNonQuery();
                    
                }
                if (checkbox3.IsChecked == true)
                {
                    subcmd.Parameters.AddWithValue("@subid", subscipid3);
                    subcmd.Parameters.AddWithValue("@TypeOfMagazine", magtype3);
                    subcmd.ExecuteNonQuery();
                    noofrows++;
                }


subscription id is a primary key

How to insert all values at the same time
Posted

The problem is that when you execute the first one, you set the parameters. When you then try to execute the second, the command exists and has the parameters set already if checkbox1 is checked.

I would do it slightly differently:
C#
private string insertsubscriptionquery = " INSERT INTO Subscription(SubscriptionId,TypeOfMagazine) values (@subid,@TypeOfMagazine)";
private static void InsertIfRequired(bool required, object id, object magazineType)
   {
   using (SqlCommand subcmd = new SqlCommand(insertsubscriptionquery,connstring))
      {
      if (required)
         {
         subcmd.Parameters.AddWithValue("@subid", id);
         subcmd.Parameters.AddWithValue("@TypeOfMagazine", magazineType);
         subcmd.ExecuteNonQuery();
         }
      }
   }

...
                InsertIfRequired(checkbox1.IsChecked, subscripid1, magtype1);
                InsertIfRequired(checkbox2.IsChecked, subscripid2, magtype2);
                InsertIfRequired(checkbox3.IsChecked, subscripid3, magtype3);
...

You can replace the object parameters with the appropriate type.
 
Share this answer
 
Comments
Costica U 9-Mar-11 7:43am    
I like your solution. My 5
Change
C#
if (checkbox2.IsChecked == true)
{
    if(subcmd.Parameters.Contains("@subid"))
    {
       subcmd.Parameters["@subid"].Value=subscripid2;
    }
    else
    {
      subcmd.Parameters.AddWithValue("@subid", subscripid2);
    }
    if(subcmd.Parameters.Contains("@TypeOfMagazine"))
    {
       subcmd.Parameters["@TypeOfMagazine"].Value=magtype2;
    }
    else
    {
      subcmd.Parameters.AddWithValue("@TypeOfMagazine", magtype2);
    }
    subcmd.ExecuteNonQuery();
}

Same for if (checkbox3.IsChecked == true)

Also you increment noofrows only if checkbox3.IsChecked == true, is this wanted?.
 
Share this answer
 
Comments
sairam.bhat 10-Mar-11 5:43am    
good answer sir

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