Solved

Working with existing dataset  vb.net

Posted on 2014-12-29
5
192 Views
Last Modified: 2014-12-29
The dataset in a project (WSCGSoftwareDataSet) has only one table with a few columns. To upload SoftwareID and Title fields from this table into a list box (indexes 0 and 1):
       '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


The data displays correctly in the list box, but...

Question: Why, upon click on the listbox, the following line doesn't display the value of SoftwareID?

It displays the column caption "SoftwareID" not the corresponding value of it.

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        MessageBox.Show(ListBox1.ValueMember.ToString())
    End Sub

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Ali HND earned 350 total points
ID: 40521628
Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If ListBox1.SelectedIndex <> -1 Then
            MessageBox.Show(ListBox1.SelectedValue.ToString())
        End If 
End Sub 

Open in new window

0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 150 total points
ID: 40521689
>>Why, upon click on the listbox, the following line doesn't display the value of SoftwareID?
>>MessageBox.Show(ListBox1.ValueMember.ToString())

Simply said, it doesn't show the value of SoftwareID because you don't tell the message box to display that, you tell it to display something else namely the ValueMember of the Listbox which is the string "SoftwareID"
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40521696
Thanks Ali,

It fixed the problem. Is there any easier way without making a dt? Directly using WSCGSoftwareDataSet.Tables(0) or querying it? I will add a new question for it shortly.

Mike
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40521708
Just what Ali suggested.  (I just pointed out why your original code didn't work as you expected).
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40521717
Thanks Andy. Your post made me realize the reason why. I hadn't paid any attention to it.

BTW, the link for my new question is: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28587819.html

Mike
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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