Solved

Working with existing dataset  vb.net

Posted on 2014-12-29
5
190 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

10 Experts available now in Live!

Get 1:1 Help Now