Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

Alternate way of supplying datasource data, vb.net

I have WSCGSoftwareDataSet.xsd with one table. I am using the following code to fill a listbox.

Question: Is there any easier ways without making a dt? Directly using WSCGSoftwareDataSet.Tables(0) or querying it?

In particular, I am interested in something like:

Dim names As New List(of string)

and possibly LINQ solution. If I am asking for too much, just name the possible solutions for now.

     '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"

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Mike;

Try this code in place of what you posted and see if it does what you need.

Dim results = (From r in WSCGSoftwareDataSet.Tables(0).AsEnumerable() _
			  Select New With { _
			      .SoftwareID = r.Field(Of String)("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"

Open in new window

Avatar of Mike Eghtebas

ASKER

Hi Fernando,

I got the following error:User generated imageBTW, are there any other solution other than what I have and LINQ you are proposing?

Thanks,

Mike
Sorry Mike I misread the original post. Please change the following line in my post

.SoftwareID = r.Field(Of String)("SoftwareID"), _

to this

.SoftwareID = r.Field(Of Integer)("SoftwareID"), _
Yes, this fixed it. When I run it (this is sort of additional question if I may), as shown below,

Q1: Why do I get message boxes 2 and 3 (maked with in red font) after I click number 1?

After number 1, I just expect no messages where I can just click on 2 to get message box marked 5.

Q2: are there any other solution other than what I have and LINQ you are proposing?

Thanks,

Mike
Hi Mike;

To your question, "Q1: Why do I get message boxes 2 and 3 (maked with in red font) after I click number 1?", not sure, can you post the code that displays the message box and maybe a screenshot.

to your question, "Q2: are there any other solution other than what I have and LINQ you are proposing?", thinking off the top of my head, you could iterate through the rows of the WSCGSoftwareDataSet.Tables(0).Rows and assign the two columns to a custom class which has two properties and after initializing the class with its values assign it to a List(Of CustomClass) and then assign the list to the data source of the ListBox.

Did the Linq query populate the ListBox correctly?
Oh I am sorry, I meant to post it but I forgot. Here it is:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex > -1 Then
            MessageBox.Show(ListBox1.SelectedValue.ToString())
        End If
    End Sub

Open in new window


re:> Did the Linq query populate the ListBox correctly?
Yes it did. Thanks.

BTW, the alternate solution you referred to in your last post, is what I have in my original post in this thread.

I will post a new question on how to add a query via code to table adapter or data adapter 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) to WSCGSoftwareDataSet before assigning dt1 to listbox datasource. This will be solution 3. I do not know how this is going to work. My guess is the query goes to tableadapter and then dt1 gets populated.

Dataadapter will have no role because noting is communicated with the database. All operation is internal to the application. Your comment on this will be appreciated.

And for solution 4, I think there no other solution. Or, is there?

I will post a link here.

Mike
Here is the link to the new question.
To the question, "Q1: Why do I get message boxes 2 and 3 (maked with in red font) after I click number 1?", to the part of the question, "Why do I get message boxes 2 and 3 (maked with in red font) ", do you mean that the MessageBox pops up two or three times during the interaction with the ListBox? This MessageBox might be displayed once for each item that is loaded into the data source if I remember correctly. May not be what you want unless it is there for debugging. Why the font is red I do not know because nothing in the code you posted should have done that. If you can take a screenshot of that it may give me some reason why it is being displayed like that. To the other part of that question, "after I click number 1", do you mean after selecting the first item in the ListBox? In able to find out why I would need to see all the event handlers that defines Handles ListBox1, please post.

To your statement, "BTW, the alternate solution you referred to in your last post, is what I have in my original post in this thread.", not exactly, in your original post you use a DataTable object to store the information in which it is a heavy weight object as where I said to use a custom class such as the following.

Public Class MyData
    Public Property SoftwareID As Integer
    Public Property Title As String
End Class

The above class only needs as much memory as one Integer and the length of the String where the DataTable is allot more memory which is needed to do the same.

Here is another solution that should work and is more efficient then the solution that I previously posted or the solution you originally posted and that is to use the original table in WSCGSoftwareDataSet.Tables(0) this way you are not creating a new table or executing a Linq query. Something like the following.

ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

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

Open in new window

re:> MessageBox pops up two or three times during the interaction with the ListBox?

After the form opens, upon click (#1) a the button next to the listbox (where your code is included with), the two unexpected message boxes pop up (these are @#+2 and #3). After that, When I select an item from the list box (#4), message #5 shows up (message #5 is expected and it works ok).

I do not know why the images I had include last time doesn't show up. It must have been very frustrating for you trying to make sense of what I am talking about. I go ahead and submit it here:User generated image
Here is the link discussed before.

https://www.experts-exchange.com/questions/28588127/New-ways-to-populate-a-listbox-vb-net.html

Yes, I see how simple your alternate solution is.

Please include your solution below:
Public Class MyData
    Public Property SoftwareID As Integer
    Public Property Title As String
End Class

in the new post.

Thanks,

Mike
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
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
Beautiful.

Thanks,

Mike
Not a problem Mike, Glad I was able to help.