0

Im trying to pass only checked values to a database. But my problem is that even the unchecked values are being passed. My check boxes are created dynamically form a Dropdown box.

Creation of List box form a Drop Down Box

  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

And this is how i insert into database

  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

Any help would be much appreciated. Thank you

1 Answer 1

1

You need to conditionalise your logic since now it's not discriminating at all. All of the items are in the CheckBoxList1.Items, not just the selected ones; so, check for selected items only...

If (list.Selected) Then
 ' proceed with this one
End If
Sign up to request clarification or add additional context in comments.

1 Comment

I was opting for such a method but was not sure if this was the right move. Thank you for your help. It worked. Sorry but i have to wait 9 minutes to accept your answer. Will do so after 9 minutes.Once again, Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.