views:

51

answers:

1

Dear All,

I have a small requirement and that is:

There are two combo boxes on a form and for populating the employee names and roles. I am populating the combo boxes as follows:

  1. I have created a class called "DbConnect" and in that there are 02 functions as:

    Public Function getEmployees() As DataTable
    Dim employeeDS As New DataSet
    Dim employeeDA As New SqlDataAdapter("prc_emp_list", conn)
    employeeDA.Fill(employeeDS, "employees")
    Return employeeDS.Tables("employees")
    End Function

    Public Function getRoles() As DataTable
    Dim roleDS As New DataSet
    Dim roleDA As New SqlDataAdapter("prc_role_list", conn)
    roleDA.Fill(roleDS, "roles")
    Return roleDS.Tables("roles")
    End Function

  2. Have designed a form with two combo boxes and am populating data into them as:

    Public Sub employees()
    accessFunction.Open()
    cboEmployees.DataSource = accessFunction.getEmployees
    cboEmployees.DisplayMember = "emp_name"
    cboEmployees.ValueMember = "login_id"
    End Sub

    Public Sub roles()
    accessFunction.Open()
    cboRoles.DataSource = accessFunction.getRoles
    cboRoles.DisplayMember = "role_name"
    cboRoles.ValueMember = "role_id"
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    employees()
    roles()
    End Sub

THE DATA IS GETTING POPULATED INTO THE COMBO BOXES CORRECTLY AND MY REQUIREMENT IS THAT WHEN I SELECT AN EMPLOYEE FROM THE FIRST COMBO, HIS CORRESPONDING ROLE SHOULD GET SELECTED/DISPLAYED IN THE SECOND COMBO.

Anyone, please help me on this requirement.

Regards,
George

A: 

You need to add a binding source and a data relationship to get this to work. Consider this walk through, it is for datagridviews but the concept is the same.

I did a quick mock up to give you an idea. Remember that "EmpTable" is the name that you assign to your datatable and "EmpColumn" is the parent column, similarly apply the same logic to the Roles table. The key change to your code is that both tables must be in the same dataset with a datarelationship.

Dim dtEmp as Datatable
Dim dtRole as Datatable

''//fill tables here

Dim ds as New Dataset()
ds.Tables.add(dtRole)
ds.Tables.add(dtEmp)

Dim dr as New DataRelation( _
 ds.Tables("EmpTable").Columns("EmpColumn"),
 ds.Tables("RoleTable").Columns("RoleColumn"))

''//create binding sources
Dim bsEmp as New BindingSource
Dim bsRole as New BindingSource
bsEmp.Datasource = ds
bsEmp.DataMember = "EmpTable"
bsRole.Datasource = bsEmp
bsRole.DataMeber = "RoleTable"

''//bind the binding sources to the appropriate comboboxes
cboEmployee.Datasource = bsEmp
cboRole.Datasource = bsRole

Good luck.

Shiftbit
Dear, i never undestood the concept. That is first i need to create a data table as "getEmployees" and populate it. Then i need to create another data table for roles and populate that as well. After that, what do i do to proceed with that. Please advice on the same and if possible, can you give me a complete example of the same. Regards - George
George Trevour Dsouza
I have shown you what do do after you have filled both tables and added them to the dataset. You must call your table "EmpTable" upon filling your table or whatever you wish to call it. There must exist an actual relationship within you database for this to work. The datarelationship parent and child columns must reflect the columns in the database.
Shiftbit