Switch to full style
You can find discussions, solutions and codes related to ASP.NET
Post a reply

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

Wed Nov 05, 2008 6:29 pm

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 




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

Wed Nov 05, 2008 6:32 pm

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

Post a reply
  Related Posts  to : Dropdown list in ASP.NET - How to get value selected
 Populating Dropdown..     -  
 POPULATING DROPDOWN IN MY DATABASE USING PHP     -  
 The selected attachment does not exist anymore     -  
 Run different function based on the selected Tab JTabbedPane     -  
 Reading selected data from a source file     -  
 Implementation of List     -  
 Sort a list     -  
 List all database in php     -  
 display list     -  
 List C++ implementation     -  

Topic Tags

ASP MYSQL, ASP Forms