Results 1 to 5 of 5

Thread: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pulled from first

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    46

    Resolved [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pulled from first

    Ok title sounds confusing, so let me post my code first:

    Code:
    Private Sub ASPCustSelection_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    
            
    
            ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:" & "\;Extended Properties='text;HDR=Yes'"
            CommandText = "select * from aspcustomers.txt"
    
            conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
            Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)
    
            conn.Open()
    
            da = New OleDbDataAdapter(CommandText, conn)
            ds = New DataSet
            ' fill dataset
            da.Fill(ds, "ASPCustomers")
    
            For Count = 0 To ds.Tables(0).Rows.Count - 1
                ComboBox1.Items.Add(ds.Tables(0).Rows(Count)(0))
    
               
            Next
    This loads the contents of a CSV file into a dataset, formatted as such:

    2281jufr97,Allied Technology
    5246ydxk84,ASC DET1
    and then fills a combo box with the "Company name" pulled from that list. What I need to do however, is pull the company CODE, the number/letter combination, and then pass that as a variable to another function later on.

    I thought I had it when i put this code in as a test:
    Code:
    TextBox1.Text = (ds.Tables(0).columns(ComboBox1.SelectedIndex).ToString)

    but after the second value it would crash, saying it couldn't find column "2".

    How can i use the selected index from the combo box to pull the matching company code?


    EDIT: I fixed it..I needed the following code in my "selection changed" event:
    TextBox1.Text = (ds.Tables(0).Rows(ComboBox1.SelectedIndex)).Item(1)
    This works
    Last edited by evanrich; Dec 7th, 2009 at 08:06 PM. Reason: fixed my own issue

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu

    You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this:
    vb Code:
    1. Me.ComboBox1.DisplayMember = "NameOfColumnToDisplay"
    2. Me.ComboBox1.ValueMember = "NameOfIDColumn"
    3. Me.ComboBox1.DataSource = ds.Tables("ASPCustomers")
    Now, when the user makes a selection from the ComboBox, you get the corresponding ID from the control's SelectedValue property.

    I've not really used WPF so I'm not sure how it works there but I'd imagine that it's similar. By the way, this site has a forum dedicated to WPF so you should ask such questions there. I've already asked the mods to move this thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2009
    Posts
    46

    Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu

    Quote Originally Posted by jmcilhinney View Post
    You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this:
    vb Code:
    1. Me.ComboBox1.DisplayMember = "NameOfColumnToDisplay"
    2. Me.ComboBox1.ValueMember = "NameOfIDColumn"
    3. Me.ComboBox1.DataSource = ds.Tables("ASPCustomers")
    Now, when the user makes a selection from the ComboBox, you get the corresponding ID from the control's SelectedValue property.

    I've not really used WPF so I'm not sure how it works there but I'd imagine that it's similar. By the way, this site has a forum dedicated to WPF so you should ask such questions there. I've already asked the mods to move this thread.
    thanks for the info. I'm not manually adding lines, the code I pasted as a fix will scan each line of the csv file into a dataset and then read the contents into a combo box. if theres 20 lines in the csv, it'll make 20 row. It's dynamic. i changed the order of my code around a bit, but it still works.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu

    Quote Originally Posted by evanrich View Post
    I'm not manually adding lines
    Yes, you are. That's exactly what this is:
    Code:
            For Count = 0 To ds.Tables(0).Rows.Count - 1
                ComboBox1.Items.Add(ds.Tables(0).Rows(Count)(0))
    
               
            Next
    You're looping through the rows in the DataTable and manually extracting a value from the row and inserting it into the ComboBox. If you just use data-binding then all that is done for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu

    Moved to WPF.

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