User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 391,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,623 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 10719 | Replies: 4
Reply
Join Date: Dec 2006
Posts: 8
Reputation: artee is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
artee artee is offline Offline
Newbie Poster

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

  #1  
Feb 9th, 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
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Location: Japan
Posts: 70
Reputation: sedgey is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
sedgey sedgey is offline Offline
Junior Poster in Training

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

  #2  
Feb 10th, 2007
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")))
Last edited by sedgey : Feb 10th, 2007 at 3:34 pm.
sedgey: so little daylight, too much caffeine
MCSD MCAD MCSE
http://www.thefriendlybuilder.co.uk,
http://www.sheetmusicdirect.com
Reply With Quote  
Join Date: Dec 2007
Posts: 15
Reputation: bungek84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bungek84 bungek84 is offline Offline
Newbie Poster

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

  #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
 
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  
Join Date: Sep 2007
Posts: 1,054
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

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

  #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  
Join Date: Dec 2007
Posts: 15
Reputation: bungek84 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bungek84 bungek84 is offline Offline
Newbie Poster

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC