Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 7:24 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 4 posts ] 
Author Question
 Question subject: Dropdown list in ASP.NET - How to get value selected
PostPosted: Wed Nov 05, 2008 6:29 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:47 pm
Posts: 281
Has thanked: 0 time
Have thanks: 1 time

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.
Code:
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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Dropdown list in ASP.NET - How to get value selected
PostPosted: Wed Nov 05, 2008 6:32 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:53 pm
Posts: 229
Has thanked: 0 time
Have thanks: 0 time
Create a DataSet object and return it in a method and bind that to your
combobox




Instead of using a datareader and SqlCommand use a SqldataAdpater

Code:
Dim adapter as New SqlDataAdapter(strSql, connect)
Dim dsResult As System.Data.DataSet
adapter.Fill(dsResult)


Code:
cboEmpList.DataSource = dsResult.Tables(0)
cboEmpList.DisplayMember = "employeeName"
cboEmpList.ValueMember = "eid"


This is what will get the value to be the employee id based of the employee
name that shows up in the cbolistemp.....hope this was clear for you...you dont
need to the SqlCommand Object if you dont want it a SqlDataAdapter will work


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Dropdown list in ASP.NET - How to get value selected
PostPosted: Thu Sep 23, 2010 2:50 pm 
Offline
Newbie
User avatar

Joined: Tue Sep 07, 2010 9:18 am
Posts: 30
Has thanked: 0 time
Have thanks: 1 time
Hey thank you so much for this post.
It will really help while creating drop down list in ASP.NET :)

_________________
Robinetterie industrielle


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Dropdown list in ASP.NET - How to get value selected
PostPosted: Fri Sep 24, 2010 9:31 am 
Offline
Newbie
User avatar

Joined: Fri Sep 24, 2010 9:25 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
congratulation to you!! :i_love:

_________________
http://www.dooneyandbourkeoutlet.org


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 4 posts ] 
Quick reply


  

 Similar topics
 reset the list counter
 nested ordered list
 Remove ordered list numbers
 Add border around ordered list elements
 creating non ordered list using UL tag.
 ordered list with roman numbers
 ordered list from specific number
 Radio Button list validation
 methods with variable arguments list
 can u check tis code to perform union & intersection of list

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team