Databinding a list box

rlegg369

Member
Joined
Jun 6, 2006
Messages
11
Programming Experience
10+
I hope this is the correct forum for this question...
I have a SQL database with three tables.

Table one has an key index and a string. I have a combobox databound to this table and it is populated with all the strings in table one.

My second table has a key index, a string, and a foreign key pointing to the key index of table one. There can be one or more strings with the same foreign key

I have a list box that I want to databind to table two, but I only want it to display table two strings equal to the combobox selected item's index, which has the foreign key of table two equal to the index key of table one

So basically, whatever the selected index of the combobox is, I want the list box to show the string data from table two in which the foreign key is equal to the key index of the combobox selected item.

I'm sure this is possible, but I haven't been able to get it to work.
Any help would be appreciated.
 
Try This one

VB.NET:
Imports System.Data.SqlClient
    Public Sub FillList(ByVal lstList As ListBox, ByVal sSQL As String, ByVal strTable As String, ByVal strDisplayMember As String, ByVal strValueMember As String)
     
        Try
            Open_conwharton()
            Dim da As New SqlClient.SqlDataAdapter(sSQL, conwharton)
            Dim dt As New DataSet

            da.Fill(dt, strTable)
            lstList.DataSource = dt.Tables(strTable).DefaultView
            lstList.DisplayMember = strDisplayMember
            lstList.ValueMember = strValueMember

            da.Dispose()
            dt.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Close_conwharton()
        End Try
    End Sub

    Public conwharton As New SqlConnection("Server=yourserver;Database=yourDB;User ID=uID;Password=pass;Trusted_Connection=False;")

    Public Sub Open_conwharton()
        Try
            conwharton.Open()
        Catch ex As Exception
            conwharton.Close()
            conwharton.Open()
        End Try
    End Sub

    'To close connection

    Public Sub Close_conwharton()
        Try
            conwharton.Close()
        Catch ex As Exception
            conwharton.Open()
            conwharton.Close()
        End Try
    End Sub

pass the appropriate SQL query
 
Hi Tom,
I'd like to try this walkthrough, but I have a question.
It uses the pubs database. As far as I can tell, this is for SQL server 2000. I'm using SQL server 2005. I haven't been able to find the pubs database in SQL server 2005 database listings. Can I download this pubs database and use it in SQL server 2005?
 
Back
Top