0

チェックされた値のみをデータベースに渡そうとしています。しかし、私の問題は、チェックされていない値でさえも渡されているということです。チェックボックスは、ドロップダウンボックスから動的に作成されます。

ドロップダウンボックスからのリストボックスの作成

  Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim uname As String
    Dim sqlConnection As New SqlConnection(connectionString)
    sqlConnection.Open()
    Dim exe1 As String = "select USERNAME from dummy_tbl_first AS L INNER JOIN            Dummy_tbl_second AS A ON L.USER_ID= A.USER_ID INNER JOIN Dummy_tbl3 AS G ON G.GROUP_ID=A.GROUP_ID WHERE G.GROUP_NAME='" + DropDownList1.Text + "' ORDER BY USERNAME "
    Dim thisCommand As New SqlCommand(exe1, sqlConnection)
    thisCommand.ExecuteNonQuery()
    Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
    CheckBoxList1.Items.Clear()
    While (thisReader.Read())
        uname = thisReader.GetValue(0)
        CheckBoxList1.Items.Add(uname)
    End While
    thisCommand.Dispose()
    sqlConnection.Close()
    Button1.Enabled = False
End Sub

そして、これは私がデータベースに挿入する方法です

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim id As Integer = Request.QueryString("sr_id")
    Dim first_name37 As String = CType(Session.Item("FIRST_NAME"), String)
    Session("FIRST_NAME") = first_name37
    Dim update_id As String = Request.QueryString("sr_id")

    For Each list As ListItem In CheckBoxList1.Items
        Response.Write(list.Selected)
        Dim da As Date = Date.Now()
        Dim sqlConnection As New SqlConnection(connectionString)
        sqlConnection.Open()
        Dim exe1 As String = "INSERT INTO TEST_TBL VALUES('" & id & " ','" + first_name37 + "','" + list.Value + "','" + da + "')"
        Dim thisCommand As New SqlCommand(exe1, sqlConnection)
        thisCommand.ExecuteNonQuery()
        Dim exeupdate As String = "UPDATE TEST_TBL2 SET STATUS='ASSIGNED' WHERE CR_ID='" + update_id + "'"
        Dim thisCommandUpdate As New SqlCommand(exeupdate, sqlConnection)
        thisCommandUpdate.ExecuteNonQuery()
        sqlConnection.Close()
    Next
    CheckBoxList1.Items.Clear()
    Button1.Enabled = False
End Sub

どんな助けでも大歓迎です。ありがとうございました

4

1 に答える 1

1

今ではまったく区別されていないので、ロジックを条件付けする必要があります。CheckBoxList1.Items選択したアイテムだけでなく、すべてのアイテムがにあります。したがって、選択したアイテムのみを確認してください...

If (list.Selected) Then
 ' proceed with this one
End If
于 2013-02-12T10:36:36.737 に答える