Solved

Trouble with Binding Source

Posted on 2014-12-28
3
95 Views
Last Modified: 2014-12-29
I have an application which accesses a database consisting of one table (Customers) with two fields (LastName and FirstName).  I drug the data source onto my form which automatically generated a data set, binding source, table adapter, table adapter manager, and a binding navigator. (I do not pretend to fully understand what each of these does).  This automatically generated text boxes for FirstName and LastName.

I want to load the data into an unbound listbox.

This code works:
    
        Me.CustomersBindingSource.MoveFirst()
        For i = 1 To CustomersBindingSource.Count
            ListBox1.Items.Add(Me.LastNameTextBox.Text + ", " + Me.FirstNameTextBox.Text)
            Me.CustomersBindingSource.MoveNext()
        Next

Open in new window


My listbox fills with last and first names.

But, if I change the visible property of the textboxes to False, the listbox fills with commas.  

I do not need the textboxes to be present except to make the above code work.  

Is there some way to get the data directly from the Binding Source (or Table Adapter or Binding Navigator, etc.)?

Thanks
0
Comment
Question by:randys106
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
I just tested the following code and it works as described below. Please apply the changes noted below to work in your project:
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        For Each r In WSCGSoftwareDataSet.Tables(0).Rows
            ListBox1.Items.Add(r(1))
        Next

    End Sub

Open in new window


WSCGSoftwareDataSet used here is my dataset name which is the same as WSCGSoftwareDataSet.xsd file in object browser, change it to whatever you have.

r(1) is the column index in your table. For now proceed with a single column (ex. firstname). But then use:

ListBox1.Items.Add(r(2) & ", " & r(1))

if Last Name has index 2 and First Name has index 1.

I have Button1_Click to run this code. You can put in form load if you wish.
0
 

Author Closing Comment

by:randys106
Comment Utility
Perfect solution.  Thanks so much.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
Hi Randy,

Also try this:

Adjust WSCGSoftwareDataSet, LastName, and ID to whatever you are having
       ListBox1.DataSource = WSCGSoftwareDataSet.Tables(0) 

        ListBox1.DisplayMember = "LastName"
        ListBox1.ValueMember = "ID"

Open in new window


The advantage of this will be use of:

 Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex > -1 Then
            MessageBox.Show(ListBox1.SelectedValue.ToString())
        End If
    End Sub

Open in new window


To give you the value of ID upon click on the list box.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Zoom web page in asp.net 2 34
Performance enhancement 39 25
Paging GridView 7 32
Change data in datatable 8 13
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

8 Experts available now in Live!

Get 1:1 Help Now