Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Alternate way of supplying datasource data, vb.net

Posted on 2014-12-29
12
Medium Priority
?
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 64

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 34

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 64

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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 64

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 34

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
 
LVL 34

Author Comment

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

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 34

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 34

Author Comment

by:Mike Eghtebas
ID: 40523155
Beautiful.

Thanks,

Mike
0
 
LVL 64

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

688 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