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

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 8
Reputation: artee is an unknown quantity at this point 
Solved Threads: 0
artee artee is offline Offline
Newbie Poster

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

 
0
  #1
Feb 10th, 2007
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
  1. Dim strConn As String
  2. strConn = ConfigurationSettings.AppSettings("connectionString")
  3. Dim myConn As New SqlConnection(strConn)
  4. Try
  5. Dim strSQL As String
  6. cboEmpList.Items.Clear()
  7. strSQL = "select distinct EId, LName, FName, Mi " & _
  8. " from EmpQualMain " & _
  9. " where BrCode = '" & gBranchCode & "'" & _
  10. " order by Lname, FName"
  11. Dim myCommand As New SqlCommand(strSQL, myConn)
  12. myConn.Open()
  13. Dim myRead As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.Default)
  14. While myRead.Read
  15. cboEmpList.Items.Add(myRead.GetValue(myRead.GetOrdinal("LName")) + ", " + myRead.GetValue(myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
  16. cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))
  17. End While
  18. myRead.Close()
  19. Catch exc1 As Exception
  20. Response.Write(exc1.Message)
  21. Finally
  22. If Not (myConn Is Nothing) Then
  23. If myConn.State = System.Data.ConnectionState.Open Then myConn.Close()
  24. End If
  25. End Try
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 130
Reputation: sedgey is on a distinguished road 
Solved Threads: 8
sedgey's Avatar
sedgey sedgey is offline Offline
Junior Poster

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

 
0
  #2
Feb 10th, 2007
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))

that line is only useful if you are databinding in your case use
  1.  
  2. cboEmpList.Items.Add(New ListItem(myRead.GetValue(myRead.GetOrdinal("EId")), myRead.GetOrdinal("FName")) + " " + myRead.GetValue(myRead.GetOrdinal("Mi")))
Last edited by sedgey; Feb 10th, 2007 at 4:34 pm.
David Ridgway: so little daylight, too much caffeine
MCSD MCAD MCSE
http://web2asp.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: bungek84 is an unknown quantity at this point 
Solved Threads: 0
bungek84 bungek84 is offline Offline
Newbie Poster

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

 
0
  #3
Dec 10th, 2007
Originally Posted by sedgey View Post
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))

that line is only useful if you are databinding in your case use
  1.  
  2. 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"));
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

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

 
0
  #4
Dec 11th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: bungek84 is an unknown quantity at this point 
Solved Threads: 0
bungek84 bungek84 is offline Offline
Newbie Poster

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

 
0
  #5
Dec 12th, 2007
Originally Posted by SheSaidImaPregy View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC