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:
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.