1,105,242 Community Members

Dropdown list in ASP.NET - How to get value selected

Member Avatar
artee
Newbie Poster
8 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am sure this must be very simple, but unforunately I can't figure it out.
I have the following code which populates the dropdown lists from the database. This works fine. Now, when the user selects a particular employee name from the list, I want to get the Employee Id of the selected employee. i.e. I need to get the "Eid" value. I can do this if the EId is loaded in the dropdown list, but in this case only the employee name is displayed. Please let me know howI can get the Eid value even though it is not loaded in the dropdown list.
I would really appreciate your help
Thanks
- Artee

Please see code below

Dim strConn As String
  strConn = ConfigurationSettings.AppSettings("connectionString")
  Dim myConn As New SqlConnection(strConn)
  Try
   Dim strSQL As String
   cboEmpList.Items.Clear()
   strSQL = "select distinct EId, LName, FName, Mi " & _
            " from EmpQualMain " & _
            " where BrCode = '" & gBranchCode & "'" & _
            " order by Lname, FName"
   Dim myCommand As New SqlCommand(strSQL, myConn)
   myConn.Open()
   Dim myRead As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.Default)
   While myRead.Read
       cboEmpList.Items.Add(myRead.GetValue(myRead.GetOrdinal("LName")) + ", " + myRead.GetValue(myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
       cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))
   End While
   myRead.Close()
   Catch exc1 As Exception
       Response.Write(exc1.Message)
   Finally
       If Not (myConn Is Nothing) Then
           If myConn.State = System.Data.ConnectionState.Open Then myConn.Close()
       End If
   End Try
Member Avatar
sedgey
Junior Poster
130 posts since Jan 2007
Reputation Points: 58 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))

that line is only useful if you are databinding in your case use

cboEmpList.Items.Add(New ListItem(myRead.GetValue(myRead.GetOrdinal("EId")), myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
Member Avatar
bungek84
Newbie Poster
17 posts since Dec 2007
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))

that line is only useful if you are databinding in your case use

cboEmpList.Items.Add(New ListItem(myRead.GetValue(myRead.GetOrdinal("EId")), myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))

how this code look like if in dropdown list don't have listitem?
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"));

Member Avatar
SheSaidImaPregy
Veteran Poster
1,080 posts since Sep 2007
Reputation Points: 28 [?]
Q&As Helped to Solve: 69 [?]
Skill Endorsements: 16 [?]
 
0
 

Isn't it just easier to bind your employee id as the Value of the Dropdownlist item, and their name as the Text of the Dropdownlist item.

You can receive the name of the dropdownlist by:
dropdownlistname.SelectedItem.Text

And the value of the dropdownlist selected item by:
dropdownlistname.SelectedItem.Value

So a simple:
Dim strEid As String = dropdownlistname.SelectedItem.Value
should do the trick as long as the item's value is the Eid.

Member Avatar
bungek84
Newbie Poster
17 posts since Dec 2007
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Isn't it just easier to bind your employee id as the Value of the Dropdownlist item, and their name as the Text of the Dropdownlist item.

You can receive the name of the dropdownlist by:
dropdownlistname.SelectedItem.Text

And the value of the dropdownlist selected item by:
dropdownlistname.SelectedItem.Value

So a simple:
Dim strEid As String = dropdownlistname.SelectedItem.Value
should do the trick as long as the item's value is the Eid.

thank alot SheSaidImaPregy..i really appreciate ur help!
:P

Member Avatar
navera
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

in dropdownlist,i want that each field in that is pass its control different pages.how can i code to dropdown list value particularly.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article