Solved

Working with existing dataset  vb.net

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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