2013-02-12 67 views
0

只檢查值複選框的列表我試圖只傳遞檢查值到數據庫。但我的問題是,即使未經檢查的值也正在通過。我的複選框是從下拉框中動態創建的。
如何從asp.net使用vb

創建列表框形成一個下拉框

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 

任何幫助將非常感激。謝謝

回答

1

你需要對你的邏輯進行限制,因爲現在它根本沒有歧視。所有的項目都在CheckBoxList1.Items,不只是選定的項目;所以,只檢查選定的項目...

If (list.Selected) Then 
' proceed with this one 
End If 
+0

我選擇了這樣的方法,但不知道這是否是正確的舉措。感謝您的幫助。有效。對不起,但我必須等待9分鐘才能接受你的答案。 9分鐘後會這樣做。再次,謝謝 – freaky 2013-02-12 10:39:47