Populating VB.net checked listbox

Is there a way to dynamically populate a checked listbox from a SQL query?
T HoecherlDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
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

T HoecherlDeveloperAuthor Commented:
I'm getting an error on the ds = dc.FillDataSet(strCheckboxlist) line.  'dc' is not declared.  What does the dc represent?
Najam UddinCommented:
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

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

T HoecherlDeveloperAuthor Commented:
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?
Najam UddinCommented:
	Dim cnn As SqlConnection
	Dim connectionString As String
	Dim sqlAdp As SqlDataAdapter
	Dim ds As New DataSet
	connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=userid; Password=password"
	cnn = New SqlConnection(connectionString)
	sqlAdp = New SqlDataAdapter("select ID, Name from myOptionsTable", cnn)
    sqlAdp.Fill(ds)
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
T HoecherlDeveloperAuthor Commented:
Thank you Najam.  That works perfectly.

T
T HoecherlDeveloperAuthor Commented:
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
Najam UddinCommented:
If you have blank values in db, filter them in query
T HoecherlDeveloperAuthor Commented:
I did.  Note that the query includes WHERE SERIES <> ''
Najam UddinCommented:
Check the datatable your getting , are you getting blank rows there?
T HoecherlDeveloperAuthor Commented:
If I run the query in SSMS, there are no blank rows.
Najam UddinCommented:
Paste your full code (relevant to checkboxlist) here
T HoecherlDeveloperAuthor Commented:
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
Najam UddinCommented:
please donot put password (for your own security) while pasting code here

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

Open in new window

T HoecherlDeveloperAuthor Commented:
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
Najam UddinCommented:
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

T HoecherlDeveloperAuthor Commented:
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.
T HoecherlDeveloperAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.