2 Replies - 1857 Views - Last Post: 16 January 2013 - 03:44 PM Rate Topic: -----

#1 Darkelf2001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-January 13

display retrieved SQL data into a CheckedListBox

Posted 16 January 2013 - 03:23 PM

New programmer here and asking your forgiveness on such a novice issue...hahaha
Have a windows form app that when i click a button, it connects to a SQL database and retrieves data from a table. I then want this data to display in a checkedlistbox.
It runs and returns no errors, but also does not display the data in my checkedlistbox

Here is the code from the button:

Private Sub btn_LoadImages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_LoadImages.Click
        Dim conn As New SqlClient.SqlConnection
        ' TODO: Modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = "integrated security=SSPI;data source=ABC;" & _
    "persist security info=False;initial catalog= website"
        Try
            conn.Open()
            MessageBox.Show("Connected to SQL Database")
        Catch ex As Exception
            MessageBox.Show("Failed to connect to data source")
        End Try
        ' Insert code to process data.'

        Dim myCMD As SqlClient.SqlCommand = New SqlClient.SqlCommand("SELECT strProducerID from dbo.tblProducer", conn)

        Dim myDataReader As SqlClient.SqlDataReader = myCMD.ExecuteReader

        If myDataReader.HasRows > 0 Then
            CheckedListBox1.DataSource = myDataReader
            CheckedListBox1.DisplayMember = "strCompanyName"
            CheckedListBox1.ValueMember = "intProducerID"
        End If

        conn.Close()

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: display retrieved SQL data into a CheckedListBox

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3221
  • View blog
  • Posts: 10,805
  • Joined: 12-December 12

Re: display retrieved SQL data into a CheckedListBox

Posted 16 January 2013 - 03:39 PM

HasRows is a Boolean (True/False), which in VB has a numerical value of -1 if True; so it is not greater than 0. Change to

If myDataReader.HasRows Then

This post has been edited by andrewsw: 16 January 2013 - 03:41 PM

Was This Post Helpful? 1
  • +
  • -

#3 Darkelf2001  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-January 13

Re: display retrieved SQL data into a CheckedListBox

Posted 16 January 2013 - 03:44 PM

Ahh....thanks a bunch. Knew it was something silly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1