Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Working with existing dataset  vb.net

Posted on 2014-12-29
5
Medium Priority
?
202 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
[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
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
Ali HND earned 1400 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 600 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 45

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

604 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