Question

I'm having some problem to display product details from two tables.

I'm using VS 2010, and MS Access Database.

My database table structure are as follow :

  • Product (#Product_ID, Category_ID, Product_Name, Product_Cost, Product_Price)

  • Category (#Category_ID, Category_Name)

What I want is to display ProductCost, ProductPrice and CategoryName into Textbox when I select ProductName from Combobox. I'm able to display ProductCost & ProductPrice but fail to display CategoryName because I not sure how to make this two table link together.

The code I use to fill Combobox with ProductName is :

Public Sub fillProductCombobox(ByVal sender As Object)
    Dim da As New OleDbDataAdapter
    Dim dt As New DataTable
    Try
        conn.Open()
        da.SelectCommand = New OleDbCommand("SELECT * FROM Product", conn)
        da.Fill(dt)
        sender.DataSource = dt
        sender.DisplayMember = "Product_Name"
        sender.ValueMember = "Product_ID"

        'best method?
        frmAddSalesProduct.txtProductCost.DataBindings.Add("Text", dt, "Product_Cost")
        frmAddSalesProduct.txtPerPrice.DataBindings.Add("Text", dt, "Product_Price")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        conn.Close()
    End Try
End Sub

Then I call the function this way on form load :

fillProductCombobox(ProductComboBox)

This is my how my form look like : Form example

Please guide me how to I display CategoryName as well.

Also is that the way I use to fill Product_Cost and Product_Price the best method?

P/S : For some reason I need to have everything done dynamically

Was it helpful?

Solution

You can use the join query like

 SELECT Product_ID, p.Category_ID, Product_Name, Product_Cost, Product_Price, Category_Name
 FROM Product p
 INNER JOIN Category c ON p.Category_ID = c.Category_ID

So, you will get the category name using this query, just bind the text box as you are binding for others.

I hope it will help you. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top