0

私は C# ASP.NET が初めてです。複数の行を持つデータベースに選択した項目を追加するための CheckBoxList に問題があります。

送信ボタンでデータベースに製品情報を挿入するページが 1 つあります。このページでは、データベースから CheckBoxList をバインドしています。

チェックボックスをオンにすると、製品コードが自動的に生成されます。チェックボックス リストから 2 つのチェック ボックスを選択すると、チェックボックス リストの値が 1 つだけデータベースに挿入されます。他の選択されたチェックボックスの値または情報はデータベースに挿入されません。

私のSQLテーブルに、製品名、製品コード(自動的に生成される)製品タイトルなどを挿入します.

そのため、1 回の [送信] クリックで CheckBoxList を選択したときに、データベースに複数の行を追加したいと考えています。

私のコードは

foreach (ListItem listitem in cblsubcat.Items)
{
  if (listitem.Selected == true)
  {
    product myproduct;
    myproduct = new product();
    myproduct.ProdName = txtproductname.Text.ToString().Trim();
    GetCategoryId();
    int Subcat_id = int.Parse(cblsubcat.SelectedValue.ToString());
    int Cat_id = int.Parse(cateid.ToString());
    gen_prod_code(Cat_id, Subcat_id);
    myproduct.ProdCode = prodcode;
    myproduct.ProdTitle = txtproductname.Text.ToString().Trim();
    myproduct.CatId = cateid;
    myproduct.SubCatID = int.Parse(cblsubcat.SelectedValue.ToString());
    ProductManager.InsertProductdata(myproduct);
  }

これがDataaccesslayerのInsertProductdataコードです。

public static int InsertProductdata(product myproduct)
    {
        int result = 0;
        using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionString))
        {
            SqlCommand myCommand = new SqlCommand("spInsertProductItem", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@Prod_code", myproduct.ProdCode);
            myCommand.Parameters.AddWithValue("@Prod_name", myproduct.ProdName);                
            myCommand.Parameters.AddWithValue("@Prod_title", myproduct.ProdTitle);
            myCommand.Parameters.AddWithValue("@Prod_sdesc", myproduct.ProdSdesc);
            myCommand.Parameters.AddWithValue("@Prod_ldesc", myproduct.ProdLdesc);
            myCommand.Parameters.AddWithValue("@Prod_features", myproduct.ProdFeatures);
            myCommand.Parameters.AddWithValue("@Prod_colour", myproduct.ProdColour);
            myCommand.Parameters.AddWithValue("@Prod_size", myproduct.ProdSize);
            myCommand.Parameters.AddWithValue("@Prod_height_FI", myproduct.ProdHeightFi);
            myCommand.Parameters.AddWithValue("@Prod_height_CM", myproduct.ProdHeightCm);
            myCommand.Parameters.AddWithValue("@Prod_totqty", myproduct.ProdTotqty);
            myCommand.Parameters.AddWithValue("@Prod_available_size", myproduct.ProdAvailablesize);
            myCommand.Parameters.AddWithValue("@Prod_available_qty", myproduct.ProdAvailableqty);                
            myCommand.Parameters.AddWithValue("@Prod_price", myproduct.ProdPrice);
            myCommand.Parameters.AddWithValue("@Prod_offerprice", myproduct.ProdOfferprice);
            myCommand.Parameters.AddWithValue("@Prod_img_M", myproduct.ProdImgM);
            myCommand.Parameters.AddWithValue("@Prod_img_D", myproduct.ProdImgD);
            myCommand.Parameters.AddWithValue("@Prod_img_S", myproduct.ProdImgS);
            myCommand.Parameters.AddWithValue("@Prod_img_L", myproduct.ProdImgL);
            myCommand.Parameters.AddWithValue("@Prod_img_XL", myproduct.ProdImgXL);
            myCommand.Parameters.AddWithValue("@Prod_img_F", myproduct.ProdImgF);
            myCommand.Parameters.AddWithValue("@Prod_img_B", myproduct.ProdImgB);
            myCommand.Parameters.AddWithValue("@Prod_visibility", myproduct.ProdVisibility);
            myCommand.Parameters.AddWithValue("@Prod_is_del", myproduct.Prodisdel);
            myCommand.Parameters.AddWithValue("@Prod_createddate", myproduct.ProdCreateddate);
            myCommand.Parameters.AddWithValue("@Prod_modifieddate", myproduct.ProdModifieddate);
            myCommand.Parameters.AddWithValue("@Prod_keyword", myproduct.ProdKeyword);
            myCommand.Parameters.AddWithValue("@Prod_alttag", myproduct.ProdAlttag);
            myCommand.Parameters.AddWithValue("@Cat_id", myproduct.CatId);
            myCommand.Parameters.AddWithValue("@Subcat_id", myproduct.SubCatID);
            myCommand.Parameters.AddWithValue("@is_Featured", myproduct.IsFeatured);
            myCommand.Parameters.AddWithValue("@is_Recent", myproduct.IsRecent);
            myCommand.Parameters.AddWithValue("@is_Popular", myproduct.IsPopular);
            myCommand.Parameters.AddWithValue("@title_tag", myproduct.TitleTag);
            myCommand.Parameters.AddWithValue("@meta_description", myproduct.MetaDescription);

            DbParameter returnValue;
            returnValue = myCommand.CreateParameter();
            returnValue.Direction = ParameterDirection.ReturnValue;
            myCommand.Parameters.Add(returnValue);

            myConnection.Open();
            myCommand.ExecuteNonQuery();
            result = Convert.ToInt32(returnValue.Value);
            myConnection.Close();
        }
        return result;
    }
4

0 に答える 0