Populating VB.net checked listbox

Is there a way to dynamically populate a checked listbox from a SQL query?
T HoecherlDeveloperAsked:
Who is Participating?
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

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

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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?
0
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

0

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

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

Open in new window

0
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
0
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

0
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.
0
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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.