T Hoecherl
asked on
Populating VB.net checked listbox
Is there a way to dynamically populate a checked listbox from a SQL query?
ASKER
I'm getting an error on the ds = dc.FillDataSet(strCheckbox list) 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)
ASKER
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(strCheckbox list)
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?
Dim ds As New DataSet()
Dim strCheckboxlist As String = "SELECT DISTINCT SERIES FROM PCT_PROD_BATCHING ORDER BY SERIES"
ds = dc.FillDataSet(strCheckbox
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Najam. That works perfectly.
T
T
ASKER
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(connectionSt ring)
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
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(connectionSt
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
ASKER
I did. Note that the query includes WHERE SERIES <> ''
Check the datatable your getting , are you getting blank rows there?
ASKER
If I run the query in SSMS, there are no blank rows.
Paste your full code (relevant to checkboxlist) here
ASKER
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(connectionSt ring)
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
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(connectionSt
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
Try
RTRIM(LTRIM(SERIES))<> ''
ASKER
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
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)
ASKER
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.
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.
ASKER
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
Thanks.
T
Open in new window