我做了一些代码来保存数据,我做了一个代码用于复选框列表以将其保存在数据中它只使用一个复选框,当我选择两个复选框时它只是保存第一个
像这样asp.net代码
<table style="height: 172px; margin-top: 15px;margin-top:10px;">
<tr>
<td colspan="5" class="auto-style5" style="padding-left:250px; font-size:29px"><strong>Faculty Members</strong>
</td>
</tr>
<tr>
<td class="auto-style4" style="width: 171px; font-size:20px">Frist Name:
</td>
<td class="auto-style1">
<asp:TextBox ID="txt_fname" runat="server" style="width: 171px"></asp:TextBox>
</td>
<td class="auto-style4" style="width: 108px; font-size:20px; padding-left:20px;">Last Name:
</td>
<td class="auto-style1">
<asp:TextBox ID="txt_lname" runat="server" style="width: 171px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4" style="height: 34px; width: 171px; font-size:20px">Specialization:
</td>
<td class="auto-style1" style="height: 34px">
<asp:TextBox ID="TextBox1" runat="server" style="width: 171px"></asp:TextBox>
</td>
<td class="auto-style4" style="height: 34px; width: 108px; font-size:20px; padding-left:20px;">Degee:
</td>
<td class="auto-style1" style="height: 34px">
<asp:TextBox ID="txt_degree" runat="server" style="width: 171px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4" style="width: 171px; font-size:20px">Addrss:
</td>
<td class="auto-style1">
<asp:TextBox ID="txt_address" runat="server" style="width: 171px"></asp:TextBox>
</td>
<td class="auto-style4" style="width: 128px; font-size:20px; padding-left:20px;">PhoneNumber:
</td>
<td class="auto-style1">
<asp:TextBox ID="txt_phone" runat="server" style="width: 171px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style4" style="width: 171px; font-size:20px">Subject_ID:
</td>
<td class="auto-style1">
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>DataBases</asp:ListItem>
<asp:ListItem>Programming 1</asp:ListItem>
<asp:ListItem>programming 2</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td class="auto-style4" style="width: 291px; font-size:20px">Faculty Members Number:
</td>
<td class="auto-style1">
<asp:TextBox ID="txt_Snumber" runat="server" style="width: 171px"></asp:TextBox>
</td>
<td colspan="2" class="auto-style4" style="width: 108px; padding-left:50px">
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td colspan="4" style="text-align:center">
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
</table>][1]
c#code
public partial class admins : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into Staff values('" + txt_fname.Text + "','" + txt_lname.Text + "','" + txt_Snumber.Text + "','" + txt_degree.Text + "','" + txt_address.Text + "','" + txt_phone.Text + "','" + CheckBoxList1.SelectedValue + "','" + txt_Snumber.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Data is Saved";
txt_Snumber.Text = "";
txt_fname.Text = "";
txt_lname.Text = "";
txt_address.Text = "";
txt_degree.Text = "";
txt_phone.Text = "";
txt_Snumber.Text = "";
}
}
答案 0 :(得分:0)
您需要加入选中的复选框&#39;文本。
例如,
string subjects = string.Join(",", CheckBoxList1.Items.Cast<ListItem>()
.Where(item => item.Selected)
.Select(item => item.Text)
.ToList());
FYI :您希望考虑使用参数化查询来防止SQL注入攻击。