troubleshooting Question

New ways to populate a listbox, vb.net

Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America asked on
.NET ProgrammingVisual Basic.NETLINQ Query
2 Comments2 Solutions2529 ViewsLast Modified:
I have WSCGSoftwareDataSet.xsd with one table. I am looking to see how many solutions are there to populate a listbox with data like: "Select SoftwareID, Title From tblSoftware"

The solutions 1 and 2 are working fine. Can you come up with new ways of doing like solution 3 described below. Or maybe some other solution 4 or solution 5?

Solution 1 (works fine, included for reference):
     'create a table object
       Dim dt As New DataTable

       'create two columns
        dt.Columns.Add("SoftwareID", GetType(System.Int32))
        dt.Columns.Add("Title", GetType(System.String))

        ' add select columns from a table in the dataset to dt
        For Each r In WSCGSoftwareDataSet.Tables(0).Rows
            dt.Rows.Add(New Object() {r(0), r(1)})
        Next
       
        ' add dt to listbox datasource
        ListBox1.DataSource = dt

        ' identify display and value members of the listbox
        ListBox1.DisplayMember = "Title"
        ListBox1.ValueMember = "SoftwareID"

Solution 2 (works fine, included for reference):
        Dim results = (From r In WSCGSoftwareDataSet.Tables(0).AsEnumerable() _
                      Select New With { _
                          .SoftwareID = r.Field(Of Integer)("SoftwareID"), _
                          .Title = r.Field(Of String)("Title") _
                      }).ToList()

        ListBox1.DataSource = results

        '' identify display and value members of the listbox
        ListBox1.DisplayMember = "Title"
        ListBox1.ValueMember = "SoftwareID"

Question: Can you come up with Solution 3 described below or possibly with solution 4 or 5 you may have?

Solution 3: How to add a query via code to table adapter or data adapter (whichever makes sense) and use this query ("Select SoftwareID, Title From tblSoftware") to populate the list box. BTW, tblSoftware is the only table in the dataset WSCGSoftwareDataSet.

We can either put the returned result from this  query into a datareader and then add a new datatable (dt1) in dataset called WSCGSoftwareDataSet. And then assig dt1 to listbox datasource or as 4th solution use the datareader to populate the list box.

I gues dataadapter will have no role because noting is communicated with the database. A dataadapter is on play only when there is some activities are taking place between a dataset and a dataset. Here, to populate listbox the interaction doesn't extend beyond the dataset into database.

Your comment on this will be appreciated.

This is yet another solution idea possibly to be developed by you :
Public Class MyData
    Public Property SoftwareID As Integer
    Public Property Title As String
End Class

This is also easy one (it is done):
ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

'' identify display and value members of the listbox
ListBox1.DisplayMember = "Title"
ListBox1.ValueMember = "SoftwareID"
ASKER CERTIFIED SOLUTION
it_saige
Developer
Join our community to see this answer!
Unlock 2 Answers and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros