【发布时间】:2016-03-06 20:44:29
【问题描述】:
我想在 sql server 中存储复选框列表的多个选定值。当我选择一个值时,它进入数据库,但在选择的多个时,它会给上面错误。
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IvrContext"].ConnectionString);
conn.Open();
SqlCommand com1 = new SqlCommand("INSERT into Ivrdatas (days) values (@days)", conn);
string empty = "";
foreach (ListItem lst in CheckBoxList1.Items)
{
if (lst.Selected == true)
{
empty = empty + " " + lst.Value;
com1.Parameters.AddWithValue("@days", empty);
com1.ExecuteNonQuery();
}
}
conn.Close();
}
这是我的 html。
<asp:CheckBoxList ID="CheckBoxList1" runat="server" style="margin-left: 299px" AutoPostBack="True">
<asp:ListItem>Monday</asp:ListItem>
<asp:ListItem>Tuesday</asp:ListItem>
<asp:ListItem>Wednesday</asp:ListItem>
<asp:ListItem>Thursday</asp:ListItem>
<asp:ListItem>Friday</asp:ListItem>
<asp:ListItem>Saturday</asp:ListItem>
<asp:ListItem>Sunday</asp:ListItem>
</asp:CheckBoxList>
【问题讨论】:
-
有人请帮忙。提前致谢!
标签: asp.net checkboxlist