Display corresponding details from database when Listbox item is selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remya1000
    New Member
    • Apr 2007
    • 115

    Display corresponding details from database when Listbox item is selected

    i'm using VB.NET.

    i have a table names "EMP". this table contain employee details like Index, empID, First name, Last name, address, phone number etc.

    while page loads, i need to display Last name First name of all employees in a listbox. and if i select any employee name from listbox and press "EDIT" button, then i need to display the employee details for that particular employee what we selected in listbox.

    right now what i did is like when an emp is selected in listbox, i will check the last name and first name in database and display details. but if two employees were there having same first name and last name, then error will occurs and the corresponding employees detail won't be displayed. but i need only first name and last name in listbox.

    in that table Index and EMPID were non Dupicated numbers. so any way is there to give index for each employee name in listbox and when an employee name is selected, then according to that index search in database and get corresponding employee details.

    if any one have any idea how to do this please let me know. and if you can provide an example then that will be great help for me.

    thanks in advance.
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    If you will use this Sql query , you will get unique FirstName and LastName.

    e.g.

    Select FirstName + ' ' + LastName,...... ....... from Emp.

    Use this query to fill the ListBox






    Originally posted by remya1000
    i'm using VB.NET.

    i have a table names "EMP". this table contain employee details like Index, empID, First name, Last name, address, phone number etc.

    while page loads, i need to display Last name First name of all employees in a listbox. and if i select any employee name from listbox and press "EDIT" button, then i need to display the employee details for that particular employee what we selected in listbox.

    right now what i did is like when an emp is selected in listbox, i will check the last name and first name in database and display details. but if two employees were there having same first name and last name, then error will occurs and the corresponding employees detail won't be displayed. but i need only first name and last name in listbox.

    in that table Index and EMPID were non Dupicated numbers. so any way is there to give index for each employee name in listbox and when an employee name is selected, then according to that index search in database and get corresponding employee details.

    if any one have any idea how to do this please let me know. and if you can provide an example then that will be great help for me.

    thanks in advance.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      For your SQL query:
      (assuming that your column names are correct)
      Code:
      SELECT ([Last Name]+', '+[First Name]) as MyDisplay, * FROM EMP ORDER BY [Last Name], [First Name]
      This will give you an extra column called "MyDisplay" with your names already formated into "lastname, firstname" and sorted alphabetically.

      Now for your listbox. After retrieving a DataTable using that query (I'm assuming you know how) you set it as the datasource for your listbox.
      Set the listbox's DisplayMember=" MyDisplay" (So that your special column is what is displayed)
      and set the ValueMember="em pID" (Or some other unique value)

      Now you should be able to use the SelectedValue property of your listbox to get the "empID" value. (It may come back as some object-type and you will have to examine it with .GetType().Name to see what the object type is so you can use it).

      Once you have your empID you will have the unique value you need to populate your other controls with the record for that user.

      Note, you may want to make that special column also contain like "(empID)" at the end so you know which "Smith, John" you are looking at.

      Comment

      Working...