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 回答