简体   繁体   中英

listbox contents empty when using inner join

I am trying to populate a listbox with contents from 2 tables. When I try to "select all" from either table, I get results in my listbox. When I try to use the innerjoin query, the listbox is empty. No errors, just an empty box. The fields to join are customers.custID=sales.cust_id . What have I done wrong?

cust_cn.ConnectionString = "Data Source=localhost;Initial Catalog=tires;Integrated Security=True"
    cust_cn.Open()
    'Dim selectStatement As String = "SELECT * FROM customers as C inner join sales as S on c.custID=s.cust_ID "
    'Dim b As New System.Text.StringBuilder()
    'Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    'Dim myDataSet As New DataSet()
    'adpt.Fill(myDataSet)
    'Dim myDataTable As DataTable = myDataSet.Tables(0)
    'Dim tempRow As DataRow
    'For Each tempRow In myDataTable.Rows
    'lstCustSales.Items.Add(tempRow(tempRow("c.first") & tempRow("c.last") & "TIRE ID: " & tempRow("s.tire_id") & "QTY: " & tempRow("s.qty") & tempRow("s.notes")))
    'Next


    'Dim selectStatement As String = "select * from customers"
    'Dim b As New System.Text.StringBuilder()
    'Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    'Dim myDataSet As New DataSet()
    'adpt.Fill(myDataSet, "customer_data")
    'Dim myDataTable As DataTable = myDataSet.Tables(0)
    'Dim tempRow As DataRow
    'For Each tempRow In myDataTable.Rows
    '    lstCustSales.Items.Add(tempRow("custphone") & "   " & tempRow("last") & tempRow("first") & " " & tempRow("address") & "   " & tempRow("zip") & temprow("custid"))
    'Next

    Dim selectStatement As String = "select * from sales"
    Dim b As New System.Text.StringBuilder()
    Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    Dim myDataSet As New DataSet()
    adpt.Fill(myDataSet, "sales_data")
    Dim myDataTable As DataTable = myDataSet.Tables(0)
    Dim tempRow As DataRow
    For Each tempRow In myDataTable.Rows
        lstCustSales.Items.Add(tempRow("date") & "   " & tempRow("cust_id") & tempRow("tire_id") & " " & tempRow("qty") & "   " & tempRow("total") & tempRow("notes"))
    Next

when I go to management studio and type this:

SELECT * FROM customers as C inner join sales as S on c.custID=s.cust_ID where c.custphone=7146511734

I get the full results from both tables, limited to my phone# (correct/intended results)

I haven't played with VB datasets for a very long time, there is a difference between how a client side dataset resolves each of the column names, versus how it is done in management studio. Essentially in Management Studio I am assuming that it is purely a string output, and no referencing to the column name is required. This could cause an issue if there are fields in both tables with the same name.

I suggest being explicit in your select statement about the fields you actually want retrieved from the query, instead of using " SELECT * ". This is good practice anyway as returning additional fields is a waste of resources, especially if there are BLOB fields

SELECT c.first, c.last, s.tire_id, s.qty, s.notes

Actually looking at your code again - you have a tempRow, looking for a column named tempRow(" ... ")

lstCustSales.Items.Add(tempRow(tempRow("c.first") & tempRow("c.last") & "TIRE ID: " & tempRow("s.tire_id") & "QTY: " & tempRow("s.qty") & tempRow("s.notes")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM