We help IT Professionals succeed at work.
Get Started

I need help with my VB code trying to fill a checkedlistbox and check items based on a value in SQL

Last Modified: 2017-11-01
I am trying to read in all columns in a SQL table to a VB checklistbox item and then check the ones that have a "checked" value of 1 in SQL.
Everything is working except it throws an exception when I get to the line where it is supposed to check one.
I am a pretty basic skilled programmer.
Any help is appreciated.
The line below that throws the exception is CheckedListBox1.SetItemCheckState(Index, CheckState.Checked)
Here is my code:
  Dim connectionString As String
        Dim cnn As SqlClient.SqlConnection
        Dim cmd As SqlClient.SqlCommand
        Dim sql As String
        Dim reader As SqlClient.SqlDataReader
        Dim i As Integer = 0
        Dim td As DateTime = DateTime.Now
        Dim Newdate = td.ToString("yyyy-MM-dd")

        connectionString = "Data Source=SQLSERVER\SQLEXPRESS;Initial Catalog=DBName;Persist Security Info=True;User ID=FakeID;Password=FakePassword"
        sql = "select CONVERT(VARCHAR(10), AddTime, 120),PatientMRN,PatientName,Checked,Completed from CarePlanOCR where CONVERT(VARCHAR(10), DateColumn, 120) = '" + Newdate + "'" + " order by CONVERT(VARCHAR(10), AddTime, 120)"
        cnn = New SqlClient.SqlConnection(connectionString)
            cmd = New SqlClient.SqlCommand(sql, cnn)
            reader = cmd.ExecuteReader()
            While reader.Read()
                Dim checkedstate As Integer = reader.Item(3)
                'Add based on checked state
                If checkedstate = 1 Then

                    Dim Index As Integer = CheckedListBox1.Items.Add(reader.Item(0) + " - " + reader.Item(1) + " - " + reader.Item(2))
                    CheckedListBox1.SetItemCheckState(Index, CheckState.Checked)

                    Dim index As Integer = CheckedListBox1.Items.Add(reader.Item(0) + " - " + reader.Item(1) + " - " + reader.Item(2))

                End If

            End While

        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try

Open in new window

Watch Question
Distinguished Expert 2017
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE