나는 두 개의 체크 박스 목록 인 CandidateName과 Payment와 dropdownlist를 가지고있다. 사용자는 체크 박스 목록에서 여러 항목을 선택하고 드롭 다운 목록에서 하나를 선택할 수 있습니다. chechboxlists 및 dropdownlist의 해당 데이터를 Panel에 배치합니다. 여기에 gridview가있는 UpdatePanel이 있습니다. Add Button을 클릭하여 Panel을 열면 모든 데이터가 제대로 채워집니다. 디버그시 "for 루프"를 통해 여러 개의 선택된 체크 박스 목록을 저장하려고하면 체크 된 데이터가 쉼표로 누적되어 루프에 저장되지 않고 데이터베이스에 저장됩니다. 대신, "if (cblPayment1.Items [j] .Selected == true)"를 실행 한 후 다른 쿼리 문으로 건너 뜁니다. 도와주세요.선택한 여러 체크 박스 목록을 데이터베이스 asp.net에 저장하는 방법 C#
protected void btnAddNew_Click(object sender, EventArgs e)
{
try
{
string strcbl1 = string.Empty;
string strcbl2 = string.Empty;
compCon = new SqlConnection(strConnectionString);
compCon.Open();
for (int i = 0; i < cblCandidateName1.Items.Count-1; i++)
{
if (cblCandidateName1.Items[i].Selected ==true)
{
strcbl1 = strcbl1 + cblCandidateName1.Items[i].Value.ToString() + ",";
}
}
for (int j = 0; j < cblPayment1.Items.Count - 1; j++)
{
if (cblPayment1.Items[j].Selected == true)
{
strcbl2 += cblPayment1.Items[j].Value.ToString() + ",";
}
}
string InsertSchedule = "INSERT INTO ScheduleEmail(CANDIDATE_ID, PAYMENT_ID, TEMPLATE_ID)VALUES(@strCID, @strPID, @strCourseID)";
SqlCommand InsertScheduleCommand = new SqlCommand(InsertSchedule, compCon);
InsertScheduleCommand.Connection = compCon;
InsertScheduleCommand.Parameters.AddWithValue(@"strCID", strcbl1);
InsertScheduleCommand.Parameters.AddWithValue(@"strPID", strcbl2);
InsertScheduleCommand.Parameters.AddWithValue(@"strCourseID", strCourseID);
InsertScheduleCommand.ExecuteNonQuery();
compCon.Close();
}
catch (Exception ex)
{
ShowPopUpMsg("Data is failed to save in table. Please contact your system administrator \n" + ex.ToString());
}
finally
{
DispalyGridViewScheduleEmail();
compCon.Close();
}
}
이것은 제 HTML 코드입니다. 당신의 for
블록의
<!--Panel to add new record--><asp:Panel ID="panelAddNew" runat="server"
style="display:none; background-color:gray;" ForeColor="Black" Width="500" Height="550">
<asp:Panel ID="panelAddNewTitle" runat="server"
style="cursor:move;font-family:Tahoma;
padding:2px;" HorizontalAlign="Center" BackColor="Blue" ForeColor="White" Height="25">
<b>Add New</b></asp:Panel>
<table width="100%" style="padding:5px">
<tr>
<td colspan="3">
<asp:Label ID="lblStatus1" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td><b>Select Candidate Name(s)</b></td>
<td><b>:</b></td>
<td><asp:CheckBoxList ID="cblCandidateName1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="table">
</asp:CheckBoxList>
</td>
</tr>
<tr>
<td><b>Select Payments</b></td>
<td><b>:</b></td>
<td>
<asp:CheckBoxList ID="cblPayment1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table"></asp:CheckBoxList>
</td>
</tr>
<tr>
<td><b>Course Name</b></td>
<td><b>:</b></td>
<td> <asp:DropDownList ID="ddlCourseName" runat="server"> </asp:DropDownList>
</td>
</tr>
</table>
<br />
<div align="center">
<asp:Button ID="btnAddNew2" runat="server" Width="70" Text="Add" OnClick="btnAddNew_Click" ValidationGroup="add"/>
<asp:Button ID="btnCancel1" runat="server" Width="70" Text="Cancel" CausesValidation="false" OnClick="Cancel_Click" ValidationGroup="add"/>
</div>
</asp:Panel>
<!--End of Panel to add new record-->
빼기 1을 제거하고 디버깅했습니다. 그것은 여전히 "if"문 안에 들어 가지 않습니다. 도와 줘서 고마워. – jkhaung
'cblPayment1.Items [j] .Selected'가 실제로 true인지 확인 했습니까? – ekad
사실입니다. 체크 박스 목록 2 개를 선택했습니다. – jkhaung