ASP.NET 4.0 & SQL 2008
I would like to ask for help in my coding, I don't know what's wrong or what am I missing.
What I want to do is when I select an item in dropdownlist
, it will give me the ID (ddlSchool.selectedvalue)
and the value of the text (ddlSchool.text)
.
Below is the code that populates the dropdownlist. I'm getting the value of ID and Text from the database, it works perfectly fine; When I debug it, it gives the correct value of the ID (newItem.Value = .dr("fnorglevelid"))
and the correct value of Text (newItem.Text = .dr("fcorgcode").ToString()
Protected Sub populateDDLDepartment()
If Not IsPostBack Then
Dim newItem As New ListItem()
newItem.Text = "Select a Department..."
newItem.Value = "0"
ddlSchool.Items.Add(newItem)
Try
With connSchool
.conn = New SqlConnection(.strCNN_ADMtoGRAD) : .conn.Open()
.strSQL = "select fnorglevelid, rtrim(fcorganization) as 'fcorgcode'"& _
"from hris_organization " & _
"where flschool = 1 " & _
"order by fcorgcode asc"
.cmd = New SqlCommand(.strSQL, .conn)
.dr = .cmd.ExecuteReader()
While .dr.Read
newItem = New ListItem()
newItem.Text = .dr("fcorgcode").ToString()
newItem.Value = .dr("fnorglevelid")
ddlSchool.Items.Add(newItem)
End While
End With
Finally
With connSchool
.conn.Close()
.conn = Nothing
.strSQL = vbNullString
.strSQL = Nothing
.cmd.Dispose()
.cmd = Nothing
.dr = Nothing
End With
End Try
End If
End Sub
But when I select an item, the value of ddlSchool.text becomes the same as the value of ddlSchool.selectedValue. I put it in the message box first:
Protected Sub ddlSchool_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSchool.SelectedIndexChanged
MsgBox("Text:" & ddlSchool.Text & " ID:" & ddlSchool.SelectedValue)
End Sub
What am I missing? What do I have to do? It seems that selectedValue
is the same as Text.