Question Populate Textboxes Based on Combobox Selection NOT Using Databinding

daniness

Well-known member
Joined
Feb 12, 2010
Messages
49
Programming Experience
Beginner
Hello Everyone,

Could someone please advise on how to go about this? I have a form, frm1 w/a combobox, cboLocations, which is being populated using a sql string. There is also a frm2 w/a series of textboxes and other comboboxes. I want it so that once a selection is made from cboLocations, the textboxes and comboboxes on frm 2 are populated with the corresponding data. Here is what I have so far:

frm1:
VB.NET:
Public Class frmLocations 
    Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;") 
    Dim depot_refnbr As Integer 
    Private Sub frmLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        conn.Open() 
        Call FillCombo() 
    End Sub 
 
    Private Sub FillCombo() 
 
        Dim sql As String = "Select distinct Site,depot_refnbr From Locations order by Site asc" 
        Dim cmd As New SqlCommand(sql, conn) 
        Dim da As New SqlDataAdapter(cmd) 
        Dim ds As New DataSet 
        Dim dt As New DataTable 
 
        da.Fill(ds, "Locations") 
        Dim dr As SqlDataReader = cmd.ExecuteReader 
        Do While dr.Read 
            cboLocations.Items.Add(dr("Site")) 
        Loop 
        dr.Close() 
        cmd.Cancel() 
        cmd.Dispose() 
 
    End Sub 
 
    Private Sub cboLocations_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLocations.SelectedIndexChanged 
 
    End Sub 
 
    Private Sub btnLocationOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLocationOk.Click 
        clsMain.Location_str_value = cboLocations.Text 
        'clsMain.Location_depot_refnbr = cboDepot. 
 
        Dim f As New frmLocationInfo 
        f.ShowDialog() 
    End Sub 
End Class

frm2:
VB.NET:
Public Class frmLocationInfo 
    Dim conn As New SqlConnection("Data Source=f03d3s-dev01; Initial Catalog=dos_track;User Id=vbuser; Password=tran3;") 
 
    Private Sub frmLocationInfo_Load(ByVal sender As System.Object, ByVal depot_refnbr As System.EventArgs) Handles MyBase.Load 
 
        conn.Open() 
        Call PopulateTextboxes() 
        Call FillDepotCombo() 
 
    End Sub 
 
    Private Sub PopulateTextboxes() 
        Dim sql As String = "Select distinct site, site_refnbr, aac, telephone_nbr, fax_nbr, email from depot INNER JOIN locations ON depot.depot_refnbr = locations.depot_refnbr" 
        Dim cmd_txtbox As New SqlCommand(sql, conn) 
        Dim da_txtbox As SqlDataAdapter = New SqlDataAdapter(cmd_txtbox) 
        Dim ds_txtbox As New DataSet 
        Dim dt As New DataTable 
 
        da_txtbox.Fill(ds_txtbox, "Locations") 
        Dim dr_txtbox As SqlDataReader = cmd_txtbox.ExecuteReader 
 
        'Read records 
        Do While dr_txtbox.Read 
            'txtLoc.Text = "Site" 
            txtLoc.Text(dr_txtbox("Site")) 
        Loop 
        dr_txtbox.Close() 
    End Sub 
 
 Private Sub FillDepotCombo() 
 
        Dim sql As String = "Select distinct depot_name, depot_refnbr From Depot order by depot_name asc" 
 
        Dim cmdCorrespDepot As New SqlCommand(sql, conn) 
 
        Dim daDepot As SqlDataAdapter = New SqlDataAdapter(cmdCorrespDepot) 
        Dim dsDepot As New DataSet 
        Dim dtDepot As New DataTable("Depot") 
 
        daDepot.Fill(dsDepot, "Depot") 
        Dim drDepot As SqlDataReader = cmdCorrespDepot.ExecuteReader 
 
        'Read records 
        Do While drDepot.Read 
            cboDepot.Items.Add(drDepot("depot_name")) 
 
        Loop 
 
        drDepot.Close() 
        cmdCorrespDepot.Cancel() 
        cmdCorrespDepot.Dispose() 
 
 End Sub 
End Class
What's happening right now is that I make a selection from cboLocations on frm1 and click on the OK button, frm2 loads, but none of the textboxes are populating and one of the comboboxes, cboDepot, shows the different data when you click on the down arrow, but it's not displaying anything it its text field when frm2 loads. Any assistance would be greatly appreciated.
 
Last edited by a moderator:
My first question is why you aren't using data-binding. Data-binding isn't suitable for all situations but for those that it is suitable for, why would you do things the hard way? My first guess is that this is homework and you were told you couldn't use it. If you are doing homework then it's nice to let us know, so that we can tailor our replies accordingly. If you're not doing homework then I can't see why you wouldn't use data-binding.
 
Hi jm,

This is actually not homework, but a project I've been given to upgrade an app from VB6 to VB.Net. I was advised not to use data binding by a senior person here. I would go to her for further advice, but she has her own projects and don't want to bother her, so that's why I'm hoping to get help here.
 
Does that person have a history in VB6? Many VB6 developers have a bad opinion of data-binding because of its implementation in VB6. VB.NET data-binding is a completely different animal. If you opt to write your own code to transfer data between a data source and the UI when you could do it using data-binding then you are re-inventing the wheel for no gain. I've seen some say that you have more control if you roll your own. That's true but what's the point of having more control if you don't need it? If you need that extra control then by all means write your own code but, if all you need is what data-binding already does, why not let data-binding do it? We already trust the Framework to do lots for us. Why baulk at trusting data-binding? Once you make the move to WPF you pretty much won't be able to avoid data-binding, as it's been extended to do a lot more than in WinForms. In this case, you're making your code way longer, and more error prone, by not using data-binding.

Anyway, with regards to your code, the first thing I notice is that you make a selection on the first form but then you never use it on the second. You do this on the first form:
VB.NET:
clsMain.Location_str_value = cboLocations.Text
yet I don't see where you're using that value to filter your data on the second form. As for the data you do get, you're throwing most of it away. You populate the ComboBox with the Site from each record but you simply discard the rest. You need to store that data in a collection of some sort so that when the user makes a selection in the ComboBox you can go to that collection and get the rest of the data for that record.
 
Also, in future, please be considerate enough to not make your code snippets so wide. It's very inconvenient for us to have to keep scrolling horizontally to be able to see the vertical scrollbars.
 
Back
Top