944,008 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 85095
  • ASP.NET RSS
Feb 10th, 2007
0

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

Expand Post »
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
ASP.NET Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
artee is offline Offline
8 posts
since Dec 2006
Feb 10th, 2007
0

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

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

that line is only useful if you are databinding in your case use
ASP.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 68
Solved Threads: 9
Junior Poster
sedgey is offline Offline
130 posts
since Jan 2007
Dec 10th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by sedgey ...
cboEmpList.DataValueField = myRead.GetValue(myRead.GetOrdinal("EId"))

that line is only useful if you are databinding in your case use
ASP.NET Syntax (Toggle Plain Text)
  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"));
Reputation Points: 8
Solved Threads: 0
Newbie Poster
bungek84 is offline Offline
17 posts
since Dec 2007
Dec 11th, 2007
0

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

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.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Dec 12th, 2007
0

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

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!
Reputation Points: 8
Solved Threads: 0
Newbie Poster
bungek84 is offline Offline
17 posts
since Dec 2007
Oct 23rd, 2011
0
Re: Dropdown list in ASP.NET - How to get value selected
in dropdownlist,i want that each field in that is pass its control different pages.how can i code to dropdown list value particularly.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
navera is offline Offline
1 posts
since Oct 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC