我在表单上设置了10个复选框。每个复选框在数据库表中都有自己的列。
当用户单击某个复选框时,必须将所选复选框的值插入数据库表的列中。知道我该怎么做吗?
我已经尝试过此代码,但是它不起作用。仅第一个复选框值插入其自己的列,其他列不受影响。
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection cnm = new SqlConnection(connStr);
cnm.Open();
if (CheckBox1.Checked)
{
SqlCommand cmd = new SqlCommand("INSERT INTO services(artist) VALUES (@artist)", cnm);
cmd.Parameters.AddWithValue("@artist", CheckBox1.Checked);
cmd.ExecuteReader();
}
else if (CheckBox2.Checked)
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO services(apprepair) VALUES (@apprepair)", cnm);
cmd1.Parameters.AddWithValue("@apprepair", CheckBox2.Checked);
cmd1.ExecuteReader();
}
}
这是asp.net标记:
<tr>
<td style="font-family:Muli, sans-serif;">
<div class="form-check">
<asp:CheckBox ID="CheckBox1" runat="server"/>
<label class="form-check-label" style="color:rgb(255,15,15);font-size:20px;" formmethod="post">Artist and Fine Arts </label>
</div>
</td>
<td>
<div class="form-check">
<asp:CheckBox ID="CheckBox2" runat="server"/>
<label class="form-check-label" style="color:rgb(36,0,255);font-family:Muli, sans-serif;font-size:20px;">Appliance Repair</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="form-check">
<asp:CheckBox ID="CheckBox3" runat="server"/>
<label class="form-check-label" runat="server" style="color:rgb(255,0,0);font-family:Muli, sans-serif;font-size:20px;"> Service and Repairs</label>
</div>
</td>
<td>
<div class="form-check">
<asp:CheckBox ID="CheckBox4" runat="server"/>
<label class="form-check-label" runat="server" style="color:rgb(20,0,255);font-family:Muli, sans-serif;font-size:20px;">Carpenter </label>
</div>
</td>
</tr>
<tr>
<td>
<div class="form-check">
<asp:CheckBox ID="CheckBox5" runat="server"/>
<label class="form-check-label" style="color:rgb(255,0,0);font-family:Muli, sans-serif;font-size:20px;">Electrical and Electronics </label>
</div>
</td>
<td>
<div class="form-check">
<asp:CheckBox ID="CheckBox6" runat="server"/>
<label class="form-check-label" style="color:rgb(51,0,255);font-family:Muli, sans-serif;font-size:20px;">I.T Solutions </label>
</div>
</td>
</tr>
....依此类推........
答案 0 :(得分:1)
将字段名称设置为您的复选框的标签,例如,将“艺术家”设置为CheckBox1的标签,然后将其写入按钮,单击:
fq
答案 1 :(得分:0)
问题不在于SQL,而在于您的C#代码。
if (CheckBox1.Checked)
{
SqlCommand cmd = new SqlCommand("INSERT INTO services(artist) VALUES (@artist)", cnm);
cmd.Parameters.AddWithValue("@artist", CheckBox1.Checked);
cmd.ExecuteReader();
}
**else if (CheckBox2.Checked)-- Issue is Here**
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO services(apprepair) VALUES (@apprepair)", cnm);
cmd1.Parameters.AddWithValue("@apprepair", CheckBox2.Checked);
cmd1.ExecuteReader();
}
在第二个If中,您正在使用Else if
,因此,如果选中了第一个复选框,则不会执行其余条件。相反,您只能使用IF
而不是ELSE IF
OR
您可以直接使用以下方法。
SqlCommand cmd = new SqlCommand("INSERT INTO services(artist) VALUES (@artist)", cnm);
cmd.Parameters.AddWithValue("@artist", CheckBox1.Checked);
cmd.ExecuteNonQuery(); -- Since Insert Statement Wont have any Retun values, Use ExecuteNonQuery
SqlCommand cmd1 = new SqlCommand("INSERT INTO services(apprepair) VALUES (@apprepair)", cnm);
cmd1.Parameters.AddWithValue("@apprepair", CheckBox2.Checked);
cmd.ExecuteNonQuery();