2013-12-10 22 views
0

当我检查清单时,我想多次删除记录。我的代码在下面有什么问题?为什么我只能删除1条记录?我只能删除上面的记录,意思是当我检查type2时,5,6只允许我删除type2。什么问题,为什么不能在复选框列表中选择多个选项

ASPX文件代码:

<asp:CheckBoxList ID="cblprodRemove" runat="server"> 
       <asp:ListItem Value="Type1" Enabled="false">Remove Product Color Type 1</asp:ListItem> 
       <asp:ListItem Value="Type2">Remove Product Color Type 2</asp:ListItem> 
       <asp:ListItem Value="Type3">Remove Product Color Type 3</asp:ListItem> 
       <asp:ListItem Value="Type4">Remove Product Color Type 4</asp:ListItem> 
       <asp:ListItem Value="Type5">Remove Product Color Type 5</asp:ListItem> 
       <asp:ListItem Value="Type6">Remove Product Color Type 6</asp:ListItem> 
       <asp:ListItem Value="Type7">Remove Product Color Type 7</asp:ListItem> 
       <asp:ListItem Value="Type8">Remove Product Color Type 8</asp:ListItem> 
       </asp:CheckBoxList> 

CS文件代码:

if (cblprodRemove.SelectedItem != null) 
      { 
       //Looping through the items in checkbox list 
       for (int i = 0; i < cblprodRemove.Items.Count; i++) 
       { 
        //Check if current items checkbox list is selected or not 
        if (cblprodRemove.Items[i].Selected) 
        { 
         //If selected execute the delete code 
         using (SqlConnection conDelColType = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString())) 
         { 

          string sqlDelColType = "DELETE FROM ProductStock WHERE ProductID = @pid AND ProductColorType = @protType; DELETE FROM ProductImage WHERE ProductID = @pid AND ProductColorType = @protType;"; 

          using (SqlCommand cmdDelColType = new SqlCommand(sqlDelColType, conDelColType)) 
          { 
           cmdDelColType.Parameters.AddWithValue("@pid", Id); 
           cmdDelColType.Parameters.AddWithValue("@protType", cblprodRemove.SelectedValue); 

           conDelColType.Open(); 
           cmdDelColType.ExecuteNonQuery(); 
           conDelColType.Close(); 
          } 
         } 
        } 
       } 
      } 
+0

从那里你得到'ID'为'@ pid'参数? –

+0

我声明globla变量字符串ID =“PID10107”; – Mickey

回答

0

这可能是一个容易解决。看到这行代码...?

cmdDelColType.Parameters.AddWithValue("@protType", cblprodRemove.SelectedValue); 

与....更换

cmdDelColType.Parameters.AddWithValue("@protType", cblprodRemove.Items[i].Value); 

希望工程

利奥

相关问题