Results 1 to 3 of 3

Thread: [RESOLVED] How to select a column from a dataset based on the value in another column?

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    46

    Resolved [RESOLVED] How to select a column from a dataset based on the value in another column?

    I'm writing a help application for the Customer support team at my company to help agents ask better questions when customers report problems. In it, I've got a SQL database that contains all the products, topics, and questions. The layout is similar to the following:

    Products Table:
    Product_ID Product Name:

    Topics Table:
    Topic_ID Product_ID Topic

    Questions Table:
    Question_ID Topic_ID Question


    In my App (VB.NET), I've got the following code that populates the Products Table upon program launch:
    Code:
    Public Sub PopulateProductsList()
    
        Dim DBCon As New SqlClient.SqlConnection
        Dim ds As New DataSet
        Dim da As SqlClient.SqlDataAdapter
        Dim sql As String
    
            'establish a connection to the Help conetent database
            DBCon.ConnectionString = My.Settings.DBString
            DBCon.Open()
    
            sql = "SELECT * FROM Products"
            da = New SqlClient.SqlDataAdapter(sql, DBCon)
    
            da.Fill(ds, "Products")
    
            lstProducts.DataContext = ds
            lstProducts.DisplayMemberPath = "ProductName"
            lstProducts.Items.SortDescriptions.Add(New ComponentModel.SortDescription("ProductName", ComponentModel.ListSortDirection.Ascending))
      
    
    
            DBCon.Close()
    
    
        End Sub
    In the SelectionChanged handler, I need to pass the product Id from the first dataset in so that it will then fill a listbox with the Topics based on the selected Product Id. I tried doing something similar to this:
    Code:
    txtItem.Text = (ds.Tables(0).Rows(lstProducts.SelectedIndex)("Product_ID")).ToString
    but this doesn't work.

    I don't know if I'm making myself clear, but I basically need to do the equivalent to the following SQL statement: "SELECT PRODUCT_ID FROM Products WHERE PRODUCT_Name = LstProducts.SelectedValue"

    Hope someone knows what I'm talking about and can help. Thanks.

  2. #2
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    Re: How to select a column from a dataset based on the value in another column?

    Maybe the use of Linq?
    Code:
    Dim Query = FROM Q in ds WHERE (Q.ProductName = lstProducts.SelectedValue)
    
    If isnothing(Query) = false then
    Return Query(0).Product_ID (??? field you're looking for)
    end if
    Better to regret things you did, than those you didn't
    International Intelligence

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    46

    Re: How to select a column from a dataset based on the value in another column?

    Quote Originally Posted by hassa046 View Post
    Maybe the use of Linq?
    Code:
    Dim Query = FROM Q in ds WHERE (Q.ProductName = lstProducts.SelectedValue)
    
    If isnothing(Query) = false then
    Return Query(0).Product_ID (??? field you're looking for)
    end if
    Thanks for the reply. I actually just found something that worked for what I wanted, DataRowView...I used the following (the textitem.text is just a text box to visually show me the results to see if its working):

    Code:
     Dim drv As DataRowView = lstProducts.SelectedItem
            Dim ProductID As Integer = drv.Item("Product_ID")
            txtItem.Text = ProductID.ToString
            PopulateTopicsList(ProductID)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width