Skip to content Skip to sidebar Skip to footer

How Can I Create A Multi Value For A Column?

I have two tables TBLCustomers and TBLGroupCustomers. I want to insert multi value in a third table that CREATE TABLE [dbo].[TBLG_Groups] ( [GrId] INT IDENTITY (1,

Solution 1:

You need to define the parameters once, before the foreach loop, and then just set their values on each iteration - something like this:

using (SqlCommand cmd1 = new SqlCommand("spG_Groups", conn))
{
     cmd1.CommandType = CommandType.StoredProcedure;

     // define the parameters *ONCE*
     cmd1.Parameters.Add("@CName", SqlDbType.NVarChar, 450);
     cmd1.Parameters.Add("@CEmail", SqlDbType.VarChar, 250);
     cmd1.Parameters.Add("@GName", SqlDbType.NVarChar, 70);

     List<String> YrStrList1 = new List<string>(); 

     foreach (ListItem li in chGp.Items)
     {
         if (li.Selected)
         {
              YrStrList1.Add(li.Value);

              // just set the *VALUES* of the parameters
              cmd1.Parameters["@CName"].Value = txtCName.Value;
              cmd1.Parameters["@CEmail"].Value = txtemail.Value;
              cmd1.Parameters["@GName"].Value = YrStrList1.ToString();

              cmd1.ExecuteNonQuery();
         }
     }
}

Post a Comment for "How Can I Create A Multi Value For A Column?"