Link to home
Start Free TrialLog in
Avatar of T Hoecherl
T HoecherlFlag for United States of America

asked on

Populating VB.net checked listbox

Is there a way to dynamically populate a checked listbox from a SQL query?
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Something like this
Dim ds As New DataSet()
Dim strCheckboxlist As String = "select ID, Name from myOptionsTable"
ds = dc.FillDataSet(strCheckboxlist)
If ds.Tables(0).Rows.Count > 0 Then
	checkedListBox1.DataSource = ds.Tables(0)
	checkedListBox1.DisplayMember = "Name"

	checkedListBox1.ValueMember = "Id"
End If

Open in new window

Avatar of T Hoecherl

ASKER

I'm getting an error on the ds = dc.FillDataSet(strCheckboxlist) line.  'dc' is not declared.  What does the dc represent?
That should have been a instance of dataadapter and call fill method

Dim objConn As New SqlConnection(sConnectionString)
Dim da  =   New SqlDataAdapter("sqlquery", objConn)

Open in new window

sorry.  I don't know how to create the instance.  This is what I have right now:

        Dim ds As New DataSet()
        Dim strCheckboxlist As String = "SELECT DISTINCT SERIES FROM PCT_PROD_BATCHING ORDER BY SERIES"
        ds = dc.FillDataSet(strCheckboxlist)
        If ds.Tables(0).Rows.Count > 0 Then
            cbSeries.DataSource = ds.Tables(0)
            cbSeries.DisplayMember = "SERIES"

            cbSeries.ValueMember = "SERIES"
        End If

What else do I need?
ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Najam.  That works perfectly.

T
I'm getting unwanted blank lines in my result (see attached Word doc).  Here is a sample of my code:

        Dim cnn As SqlConnection
        Dim connectionString As String
        Dim sqlAdp As SqlDataAdapter
        Dim ds As New DataSet
        connectionString = "Data Source=THOECHERL-PC; Initial Catalog=TWO; User ID=sa; Password=Great Plains!"
        cnn = New SqlConnection(connectionString)
        sqlAdp = New SqlDataAdapter("SELECT DISTINCT SERIES FROM PCT_PROD_BATCHING WHERE SERIES <> '' ORDER BY SERIES", cnn)
        sqlAdp.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            cbSeries.DataSource = ds.Tables(0)
            cbSeries.DisplayMember = "SERIES"
            'cbSeries.ValueMember = "SERIES"
        End If
checkboxlist_image.docx
If you have blank values in db, filter them in query
I did.  Note that the query includes WHERE SERIES <> ''
Check the datatable your getting , are you getting blank rows there?
If I run the query in SSMS, there are no blank rows.
Paste your full code (relevant to checkboxlist) here
Private Sub frmFilter_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim cnn As SqlConnection
        Dim connectionString As String
        Dim sqlAdp As SqlDataAdapter
        Dim ds As New DataSet
        connectionString = "Data Source=THOECHERL-PC; Initial Catalog=TWO; User ID=sa; Password=Great Plains!"
        cnn = New SqlConnection(connectionString)
        sqlAdp = New SqlDataAdapter("SELECT DISTINCT SERIES FROM PCT_PROD_BATCHING WHERE SERIES <> '' ORDER BY SERIES", cnn)
        sqlAdp.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            cbSeries.DataSource = ds.Tables(0)
            cbSeries.DisplayMember = "SERIES"
        End If

        sqlAdp = New SqlDataAdapter("SELECT DISTINCT STYLE FROM PCT_PROD_BATCHING WHERE STYLE <> '' ORDER BY STYLE", cnn)
        sqlAdp.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            cbStyle.DataSource = ds.Tables(0)
            cbStyle.DisplayMember = "STYLE"
        End If
    End Sub
please donot put password (for your own security) while pasting code here

Try
RTRIM(LTRIM(SERIES))<> ''

Open in new window

Same result.  Here is the query:

sqlAdp = New SqlDataAdapter("SELECT DISTINCT RTRIM(LTRIM(SERIES)) AS SERIES FROM PCT_PROD_BATCHING WHERE SERIES <> '' ORDER BY SERIES", cnn)

The result is attached.

checkboxlist_image.docx
I meant
sqlAdp = New SqlDataAdapter("SELECT DISTINCT RTRIM(LTRIM(SERIES)) AS SERIES FROM PCT_PROD_BATCHING WHERE RTRIM(LTRIM(SERIES))<> '' ORDER BY SERIES", cnn)

Open in new window

Here is the query:

sqlAdp = New SqlDataAdapter("SELECT DISTINCT RTRIM(LTRIM(SERIES)) AS SERIES FROM PCT_PROD_BATCHING WHERE RTRIM(LTRIM(SERIES)) <> '' ORDER BY SERIES", cnn)

Result has not changed.
I got it, Najam.  In each section of code for the different check box lists, I was using sqlAdp and ds over again.  When I changed to sqlAdp1, sqlAdp2, etc. the problem was resolved.

Thanks.

T