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
Solved

Working with existing dataset  vb.net

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

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 34

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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