Link to home
Start Free TrialLog in
Avatar of jokerxp888
jokerxp888

asked on

ListBox problem

i have 2 table
table1 : categoryID,CategoryName,Desc
table2: subCategoryID,SubCateogryName,subCategoryDesc,CategoryID
link by CategoryID

i have 2 listbox
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1

my code :

Private ds As DataSet = New DataSet

Public Sub getCategory(ByVal ds As DataSet)
        Dim strSql As String = "select * from Category"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSql, DBConn)
        da.Fill(ds, "Category")

    End Sub
    Public Sub getSubCategory(ByVal ds As DataSet)
        Dim strSql As String = "select * from SubCategory"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(strSql, DBConn)
        da.Fill(ds, "SubCategory")
    End Sub

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getCategory(ds)
        getSubCategory(ds)
        ListBox1.DataSource = ds.Tables("category")
            ListBox1.DisplayMember = ds.Tables("category").Columns(1).ToString
            ListBox1.ValueMember = ds.Tables("category").Columns(0).ToString
    End Sub


any can help me ?
Avatar of ptakja
ptakja
Flag of United States of America image

What's the problem?
Avatar of jokerxp888
jokerxp888

ASKER

i want to make it
listbox1 : display all categoryName
listbox2: display subCategoryName base on categoryName selected at the listbox1


i have error at

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            ListBox2.DataSource = ds.Tables("subCategory").Select("cat_id=1")
            ListBox2.DisplayMember = ds.Tables("subCategory").Columns(1).ToString
            ListBox2.ValueMember = ds.Tables("subCategory").Columns(0).ToString
        Catch err As Exception
            MsgBox(err.ToString)
        End Try
    End Sub
What's the error you get?
ASKER CERTIFIED SOLUTION
Avatar of ptakja
ptakja
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ptakja took a good point.

You can use the default view of the datatable

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            dim dv as Data.DataView = ds.Tables("subCategory").DefaultView

            dv.Sort = "cat_id"
            dv.RowFilter = "1"

            ListBox2.DataSource = dv
            ListBox2.DisplayMember = ds.Tables("subCategory").Columns(1).ToString
            ListBox2.ValueMember = ds.Tables("subCategory").Columns(0).ToString
        Catch err As Exception
            MsgBox(err.ToString)
        End Try
    End Sub
I would say split between ptakja & wguerram.