user2103670 user2103670 -5 years ago 355 Question

Retrieved the checked items from a CheckedListBox VB.NET Winform

I asked this question before but suggested to provide a more details. Here is the problem:

  1. I have a CheckedListBox called
    . All items in this
    are getting from SQL Server Datasource. All items are loaded properly using the below code

    Dim queryString As String = "SELECT Facility FROM Database.dbo.Facility "
    Dim connection As New SqlConnection(connectionString)
    Dim command As New SqlCommand(queryString, connection)
    Dim dataReader As SqlDataReader = command.ExecuteReader()
    Dim source As New BindingSource
    source.DataSource = dataReader
    CheckedList_Facility.DataSource = source
    CheckedList_Facility.ValueMember = "Facility"

  2. I would like to get a list of items that are checked. For example,

    [X] AAA

    [X] BBB

    [ ] CCC

    [ ] DDD

    [X] EEE

then the list should be "AAA", "BBB", "EEE"

  1. To test if the item is retrieved correctly, I using a button call
    and when this button is pressed, a msgbox displays the items that are checked. With this code:

    Dim itemChecked As Object
    For Each itemChecked In CheckedList_Facility.CheckedItems

  2. However, I only receive this error message


Technically, this might not be an error but instead of receiving "AAA", I get this


Answer Source

Because you bound the checkedlistbox to your datareader, the checked object internally is actually a {System.Data.Common.DataRecordInternal} and not a string or any other native object. You have to access the item property within the object to get to the string you want, like so:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download