Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have one combo box with the list of Employee Name from Access DB.And Two Text boxes.I want that when i select a name from combo box ,the first text box show the Employee ID and the the second box show his salary and the text box show salary is in numerical form not in text form.

My code is given below

VB
Imports System.Data.OleDb
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\new.accdb;")
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT EmpName FROM tbl", con)

        'ONE COMBO BOX TO SHOW DATA FROM ACCESS LIST
        Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(cmd)
        Dim myDataSet As DataSet = New DataSet()
        myDA.Fill(myDataSet, "new")

        For Each dRow As DataRow In myDataSet.Tables("new").Rows
            ComboBox1.Items.Add(dRow.Item("EmpName"))
        Next
        'TWO TEXT BOXES TO PICK DATA FROM ACCESS LIST ON SELECTION OF COMBO BOX LIST
        If ComboBox1.SelectedIndex > -1 Then
            Dim dv As DataView = CType(ComboBox1.DataSource, DataView)
            For Each drv As DataRowView In dv
                If drv.Item("EmpName") = CType(ComboBox1.SelectedItem, DataRowView).Item("EmpName") Then
                    TextBox1.Text = drv.Item("EmpID").ToString()
                    TextBox2.Text = drv.Item("Salry").ToString()
                    Exit For
                End If
            Next

        End If
    End Sub
End Class
Posted
Comments
[no name] 2-Jun-14 14:21pm    
Since this is a form load event, it is highly unlikely that the selected index for your combobox is going to be anything other than -1 as the user would not have been given the opportunity to make any selection yet.

1 solution

This code needs to be in the ComboBox event SelectedIndexChanged.
Then get the index int i=ComboBox1.SelectedIndex; and look up the relevant row from the DataSet.
Also you can store the EmpIds in a global list and then use that to look up the Employee by Id.


VB
'TWO TEXT BOXES TO PICK DATA FROM ACCESS LIST ON SELECTION OF COMBO BOX LIST
        If ComboBox1.SelectedIndex > -1 Then
            Dim dv As DataView = CType(ComboBox1.DataSource, DataView)
            For Each drv As DataRowView In dv
                If drv.Item("EmpName") = CType(ComboBox1.SelectedItem, DataRowView).Item("EmpName") Then
                    TextBox1.Text = drv.Item("EmpID").ToString()
                    TextBox2.Text = drv.Item("Salry").ToString()
                    Exit For
                End If
            Next
 
        End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900