8 Replies - 10701 Views - Last Post: 19 November 2013 - 03:38 PM Rate Topic: -----

#1 bmccollum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 13

How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:24 AM

I'm developing a Windows Forms application in VB.NET (Visual Studio 2013). Numerous forms have combobox control that I'd like to dynamically populate via a call to a stored procedure that retrieves the appropriate combobox choices available for specific combobox controls on specific forms.

I've had a function I've used many times in the past when doing this sort of thing in VB6, but I've researched and researched how to accomplish the same thing in .NET with no luck in finding a solution.

So basically, a form opens and during part of the "onload" event, I want to make a call to a function to be named "PopulateComboBoxControl", where I pass-in the name of the form that I'm dealing with at present, as well as the name of the combobox control on the form that I need to populate with the appropriate drop-down selections the user can choose from. The "PopulateComboBoxControl" function would then call a stored procedure that accepts the parameters for "FormName" and "ControlName" and return a DataAdapter to be looped through to populate the desired combobox.

Any suggestions would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to Populate Windows Forms Combobox Dynamically from DB

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2555
  • View blog
  • Posts: 10,253
  • Joined: 03-December 12

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:26 AM

Data binding is what you are after
Was This Post Helpful? 0
  • +
  • -

#3 ybadragon   User is offline

  • Home Owner
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,647
  • Joined: 11-May 12

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:26 AM

Have you looked into creating a DataAdapter in VB.Net? What code do you have so far?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6561
  • View blog
  • Posts: 26,600
  • Joined: 12-December 12

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:39 AM

Quote

The "PopulateComboBoxControl" function would then call a stored procedure that accepts the parameters for "FormName" and "ControlName"

You shouldn't do this, and I hope that you don't intend to store form and control-names in the database? Surely the purpose of the form and its combobox tells you what data you need to retrieve from the database?

What happens when you create a new form and combobox? You are going to have to modify your database-data.

To be frank, this type of inter-dependency is a poor design, and could easily breakdown.
Was This Post Helpful? 0
  • +
  • -

#5 bmccollum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 13

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:40 AM

My code's at home right now, but I can get it tonight and post it here and let you know specifically where it's failing. Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#6 bmccollum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 13

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 18 November 2013 - 08:46 AM

View Postandrewsw, on 18 November 2013 - 08:39 AM, said:

Quote

The "PopulateComboBoxControl" function would then call a stored procedure that accepts the parameters for "FormName" and "ControlName"

You shouldn't do this, and I hope that you don't intend to store form and control-names in the database? Surely the purpose of the form and its combobox tells you what data you need to retrieve from the database?

What happens when you create a new form and combobox? You are going to have to modify your database-data.

To be frank, this type of inter-dependency is a poor design, and could easily breakdown.


I appreciate your insight. What I'm trying to do is to give multiple users of the software I've developing the ability to have different items appear in the same ComboBox on the various forms. For example, one user may want to adjust the records in the underlying database table to where the items available for selection in the frm.ComboBox1 control are "A", "B", & "C". Another user of the software at a completely different organization might want to have the available selections present in frm.ComboBox1 as "X", "Y", & "Z". I figured with the function that's called to populate the various controls, it would work well and the same mechanics would always be followed for purposes of populating each control with whatever records the stored procedure finds in the underlying database table.

Any more insight/info. into this is appreciated if I really should pursue this a different way. I was just trying to make the ComboBox situation as flexible as possible.
Was This Post Helpful? 0
  • +
  • -

#7 bmccollum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 13

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 19 November 2013 - 03:12 PM

Here's the code that I have working for population of items to choose from in a VB.NET ComboBox (Windows Forms).

The code below is present in a specific form, but what I'd like to do is to move the code to a module as be able to call it as a function, passing-in the appropriate form name as well as the name of the ComboBox I'm dealing with and have the function able to populate the desired ComboBox on any desired form.

The code below works fine for the specific form / ComboBox combination I've used in the example, but I'm not having any success getting the code to work when I alter it and allow a form name and combobox to be passed into a public function.

Any recommendations or suggested modifications to the code below are appreciated.

Private Sub FillcboDivisionCombo()
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim sql As String = Nothing

sql = "sp_pop_drop_downs"
cn = GetConnect()

With cmd
.Connection = cn
.CommandText = sql
.CommandType = CommandType.StoredProcedure
.Parameters.AddWithValue("@Form", "frmBandInfo")
.Parameters.AddWithValue("@Field", "cboDivision")
End With

Try
cn.Open()
dr = cmd.ExecuteReader()
While dr.Read
cboBandDivision.Items.Add(dr.Item("Entry"))
End While
dr.Close()
cn.Close()

Catch ex As Exception
MessageBox.Show("Can not open connection ! ")
End Try
End Sub

Here's the code that I have working for population of items to choose from in a VB.NET ComboBox (Windows Forms).

The code below is present in a specific form, but what I'd like to do is to move the code to a module as be able to call it as a function, passing-in the appropriate form name as well as the name of the ComboBox I'm dealing with and have the function able to populate the desired ComboBox on any desired form.

The code below works fine for the specific form / ComboBox combination I've used in the example, but I'm not having any success getting the code to work when I alter it and allow a form name and combobox to be passed into a public function.

Any recommendations or suggested modifications to the code below are appreciated.

Private Sub FillcboDivisionCombo()
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim sql As String = Nothing

        sql = "sp_pop_drop_downs"
        cn = GetConnect()

        With cmd
            .Connection = cn
            .CommandText = sql
            .CommandType = CommandType.StoredProcedure
            .Parameters.AddWithValue("@Form", "frmBandInfo")
            .Parameters.AddWithValue("@Field", "cboDivision")
        End With

        Try
            cn.Open()
            dr = cmd.ExecuteReader()
            While dr.Read
                cboBandDivision.Items.Add(dr.Item("Entry"))
            End While
            dr.Close()
            cn.Close()

        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End Sub



Here's the code that I have working for population of items to choose from in a VB.NET ComboBox (Windows Forms).

The code below is present in a specific form, but what I'd like to do is to move the code to a module as be able to call it as a function, passing-in the appropriate form name as well as the name of the ComboBox I'm dealing with and have the function able to populate the desired ComboBox on any desired form.

The code below works fine for the specific form / ComboBox combination I've used in the example, but I'm not having any success getting the code to work when I alter it and allow a form name and combobox to be passed into a public function.

Any recommendations or suggested modifications to the code below are appreciated.

Private Sub FillcboDivisionCombo()
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim sql As String = Nothing

        sql = "sp_pop_drop_downs"
        cn = GetConnect()

        With cmd
            .Connection = cn
            .CommandText = sql
            .CommandType = CommandType.StoredProcedure
            .Parameters.AddWithValue("@Form", "frmBandInfo")
            .Parameters.AddWithValue("@Field", "cboDivision")
        End With

        Try
            cn.Open()
            dr = cmd.ExecuteReader()
            While dr.Read
                cboBandDivision.Items.Add(dr.Item("Entry"))
            End While
            dr.Close()
            cn.Close()

        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End Sub


Was This Post Helpful? 0
  • +
  • -

#8 bmccollum   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-November 13

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 19 November 2013 - 03:28 PM

I figured it out!

Public Sub PopulateControlsComboBox(frm As Form, cboCtrl As Windows.Forms.ComboBox, strSQL As String, strFieldName As String, Optional ByVal strFieldID As String = vbNullString)
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim sql As String = Nothing
        Dim cn As New SqlConnection

        sql = "sp_pop_drop_downs"
        cn = GetConnect()

        With cmd
            .Connection = cn
            .CommandText = sql
            .CommandType = CommandType.StoredProcedure
            .Parameters.AddWithValue("@Form", frm.Name)
            .Parameters.AddWithValue("@Field", cboCtrl.Name)
        End With

        Try
            cn.Open()
            dr = cmd.ExecuteReader()
            While dr.Read
                cboCtrl.Items.Add(Trim(dr.Item("Entry")))
            End While
            dr.Close()
            cn.Close()

        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End Sub


Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6561
  • View blog
  • Posts: 26,600
  • Joined: 12-December 12

Re: How to Populate Windows Forms Combobox Dynamically from DB

Posted 19 November 2013 - 03:38 PM

There is already a discrepancy because you are supplying the value "cboDivision" but the combobox you actually populate is named cboBandDivision.

You are retrieving Division information from the database so, in my opinion, there must be something about the role - the use and purpose - of the form that determines what information needs to be retrieved, other than the form and combobox names.

Sorry, I can't help you with your current approach. Maybe someone else can ;)




The most common bridge between a database and a form is a Class, and class-instances. This is, in essence, the philosophy behind the Entity Framework and other ORM frameworks. Forms just provide the user-interface, as they should, and form and control-names should remain immaterial to the design-process.

This post has been edited by andrewsw: 19 November 2013 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1