Solved

Alternate way of supplying datasource data, vb.net

Posted on 2014-12-29
12
119 Views
Last Modified: 2016-02-16
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

0
Comment
Question by:Mike Eghtebas
  • 6
  • 6
12 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40521796
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40522133
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40522215
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40522507
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40522570
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40522716
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40522737
Here is the link to the new question.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40522906
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40523044
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40523125
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
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40523155
Beautiful.

Thanks,

Mike
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40523157
Not a problem Mike, Glad I was able to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now