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

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
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.

Fernando SotoRetiredCommented:
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

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Fernando,

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

Thanks,

Mike
0
Fernando SotoRetiredCommented:
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"), _
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
Fernando SotoRetiredCommented:
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?
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Here is the link to the new question.
0
Fernando SotoRetiredCommented:
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

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
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:missingImage
Here is the link discussed before.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28588127.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
0
Fernando SotoRetiredCommented:
Hi Mike;

OK, this is the issue with the extra message box's coming up when loading the ListBox1. Loading, deleting Items from the ListBox control will cause the SelectedIndexChanged event to be triggered and the reason causing the message box's to appear. You can stop this from happening by changing the SelectionMode before loading and back again after loading like the following code shows.

ListBox1.SelectionMode = SelectionMode.None

ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 
'' identify display and value members of the listbox
ListBox1.DisplayMember = "Title"
ListBox1.ValueMember = "SoftwareID"

ListBox1.SelectionMode = SelectionMode.One

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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Beautiful.

Thanks,

Mike
0
Fernando SotoRetiredCommented:
Not a problem Mike, Glad I was able to help.
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.