Results 1 to 4 of 4

Thread: Databound comboboxes repeating data

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Databound comboboxes repeating data

    I have several comboboxes on various tabs which I want to load from the database to give a simple list of student names and group names.
    The problem is, if I have 2 comboboxes for the student names, the data is repeated twice in both comboboxes. And if I have 2 comboboxes for the Groups, the data is repeated 5 times in all the comboboxes.
    The code I have is below.
    vb.net Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.         Me.Visible = True
    3.         Dim SQL As String = "SELECT stID, stName FROM tblStudent ORDER BY stName"
    4.         Dim Val As String = "stID"
    5.         Dim disp As String = "stName"
    6.         FillCombo(SQL, cboName, Val, disp, "Student")
    7.         FillCombo(SQL, cboStudentName, Val, disp, "Student")
    8.         SQL = "SELECT grID, grName FROM tblGroup ORDER BY grName"
    9.         Val = "grID"
    10.         disp = "grName"
    11.         FillCombo(SQL, cboStudentGroup, Val, disp, "Group")
    12.         FillCombo(SQL, cboRegistersGroup, Val, disp, "Group")
    13.         FillCombo(SQL, cboReportsGroup, Val, disp, "Group")
    14.         FillCombo(SQL, cboAttendanceGroup, Val, disp, "Group")
    15.         FillCombo(SQL, cboMaintGroupName, Val, disp, "Group")
    16.     End Sub
    17.  
    18.     Private Sub FillCombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal val As String, ByVal disp As String, ByVal tblName As String)
    19.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0"
    20.         dbSource = "Data Source=" & Application.StartupPath & "\Student.mdb"
    21.         Conn.ConnectionString = dbProvider & ";" & dbSource
    22.         Conn.Open()
    23.         da = New OleDb.OleDbDataAdapter(SQL, Conn)
    24.         da.Fill(ds, tblName)
    25.         Conn.Close()
    26.         CBO.DataSource = ds.Tables(tblName)
    27.         CBO.ValueMember = val
    28.         CBO.DisplayMember = disp
    29.     End Sub
    Why is it repeating the data the same number of times as there are comboboxes and how do I solve it?

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

    Re: Databound comboboxes repeating data

    Each time you call FillCombo you are filling the specified DataTable from the database. You call FillCombo twice with the table name "Student" so you are fill that DataTable twice, so you see two lots of the same data. If you want to display the same data in multiple ComboBoxes then you fill the DataTable ONCE only and then bind it multiple times, so you need to separate the code that retrieves the data from the code that binds it to the ComboBox.

    Also, if you bind a DataTable directly to multiple controls then making a selection in one will make the same selection in the others. You need to bind your DataTable to multiple BindingSources and then bind one BindingSource to each ComboBox.
    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
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Databound comboboxes repeating data

    below is my solution which works but it seems a bit cobbled together. Is there a more elegant way to do it?
    Also, as the database is changed, will names be added/removed from these comboboxes as they are added/romoved from the database?
    finally, What should I dispose of and when?

    vb.net Code:
    1. Dim Conn As New OleDb.OleDbConnection
    2.     Dim dbProvider As String
    3.     Dim dbSource As String
    4.     Dim ds As New DataSet
    5.     Dim da As OleDb.OleDbDataAdapter
    6.  
    7.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.         'Hide form while drawing to prevent flickering
    9.         Me.Visible = False
    10.  
    11.         'set up tab control
    12.         TabControl1.Alignment = TabAlignment.Left
    13.         TabControl1.SizeMode = TabSizeMode.Fixed
    14.         TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
    15.         TabControl1.ItemSize = New Size(70, 130)
    16.  
    17.         'Show form again once all drawing has been done
    18.         Me.Visible = True
    19.  
    20.         Dim SQL As String = "SELECT stID, stName FROM tblStudent ORDER BY stName"
    21.         Dim sVal As String = "stID"
    22.         Dim disp As String = "stName"
    23.         FillComboStudent(SQL, sVal, disp, "Student")
    24.         SQL = "SELECT grID, grName FROM tblGroup ORDER BY grName"
    25.         sVal = "grID"
    26.         disp = "grName"
    27.         FillComboGroup(SQL, sVal, disp, "Group")
    28.     End Sub
    29.     Private Sub FillComboStudent(ByVal SQL As String, ByVal Sval As String, ByVal disp As String, ByVal tblName As String)
    30.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0"
    31.         dbSource = "Data Source=" & Application.StartupPath & "\Student.mdb"
    32.         Conn.ConnectionString = dbProvider & ";" & dbSource
    33.         Conn.Open()
    34.         da = New OleDb.OleDbDataAdapter(SQL, Conn)
    35.         da.Fill(ds, tblName)
    36.         Conn.Close()
    37.         Dim bndStudentNames1 As New BindingSource
    38.         Dim bndStudentNames2 As New BindingSource
    39.         bndStudentNames1.DataSource = ds.Tables(tblName)
    40.         bndStudentNames2.DataSource = ds.Tables(tblName)
    41.         cboName.DataSource = bndStudentNames1
    42.         cboStudentName.DataSource = bndStudentNames2
    43.         cboName.ValueMember = Sval
    44.         cboName.DisplayMember = disp
    45.         cboStudentName.ValueMember = Sval
    46.         cboStudentName.DisplayMember = disp
    47.     End Sub
    48.  
    49.     Private Sub FillComboGroup(ByVal SQL As String, ByVal Sval As String, ByVal disp As String, ByVal tblName As String)
    50.         dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0"
    51.         dbSource = "Data Source=" & Application.StartupPath & "\Student.mdb"
    52.         Conn.ConnectionString = dbProvider & ";" & dbSource
    53.         Conn.Open()
    54.         da = New OleDb.OleDbDataAdapter(SQL, Conn)
    55.         da.Fill(ds, tblName)
    56.         Conn.Close()
    57.         Dim bndGroupNames1 As New BindingSource
    58.         Dim bndGroupNames2 As New BindingSource
    59.         Dim bndGroupNames3 As New BindingSource
    60.         Dim bndGroupNames4 As New BindingSource
    61.         Dim bndGroupNames5 As New BindingSource
    62.         bndGroupNames1.DataSource = ds.Tables(tblName)
    63.         bndGroupNames2.DataSource = ds.Tables(tblName)
    64.         bndGroupNames3.DataSource = ds.Tables(tblName)
    65.         bndGroupNames4.DataSource = ds.Tables(tblName)
    66.         bndGroupNames5.DataSource = ds.Tables(tblName)
    67.         cboStudentGroup.DataSource = bndGroupNames1
    68.         cboRegistersGroup.DataSource = bndGroupNames2
    69.         cboReportsGroup.DataSource = bndGroupNames3
    70.         cboAttendanceGroup.DataSource = bndGroupNames4
    71.         cboMaintGroupName.DataSource = bndGroupNames5
    72.  
    73.         cboStudentGroup.ValueMember = Sval
    74.         cboStudentGroup.DisplayMember = disp
    75.         cboRegistersGroup.ValueMember = Sval
    76.         cboRegistersGroup.DisplayMember = disp
    77.         cboReportsGroup.ValueMember = Sval
    78.         cboReportsGroup.DisplayMember = disp
    79.         cboAttendanceGroup.ValueMember = Sval
    80.         cboAttendanceGroup.DisplayMember = disp
    81.         cboMaintGroupName.ValueMember = Sval
    82.         cboMaintGroupName.DisplayMember = disp
    83.     End Sub

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

    Re: Databound comboboxes repeating data

    Like I said, separate the code to retrieve the data and the code to bind the data. Write a FillTable method and call it once for each DataTable that you want to populate. Write a BindComboBox method and call it for each ComboBox that you want to bind. The two have nothing directly to do with each other.
    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

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