Help with retreiving Text value from Dropdownlist control.

Hello,

My Dropdownlist shows BEL, how evee when I try to retreive that information using
DropDownList1.Text, I receive a value of "2" instead of BEL, any ideas what is causing this problem?

The dropdown list isloaded using the folowing method:

                 Dim DonorID As String
                DonorID = xnLink.SelectSingleNode("DonorID").InnerText

                ' Perform another similar search based upon the Donor XML file
                For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                    ' Extract the DonorID and Name values from the current node
                    Dim DonorID2 As String
                    Dim Name As String

                    DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                    Name = xnDonor.SelectSingleNode("Name").InnerText
                    dtDonor.Rows.Add({DonorID2, Name})
                Next

 DropDownList1.DataSource = dtDonor
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataValueField = "DonorID"
        DropDownList1.DataBind()

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
CodeCruiserCommented:
You have added the columns correctly in datatable right?

Try changing

DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                    Name = xnDonor.SelectSingleNode("Name").InnerText
                    dtDonor.Rows.Add({DonorID2, Name})


to

Dim drow As DataRow = dtDonor.NewRow()
drow("DonorID") = xnDonor.SelectSingleNode("DonorID").InnerText
drow("Name") = xnDonor.SelectSingleNode("Name").InnerText
dtDonor.Rows.Add(drow)
0
 
käµfm³d 👽Commented:
Have you tried using SelectedItem instead?

Dim textValue As String = DimDropDownList1.SelectedItem

Open in new window

0
 
CodeCruiserCommented:
Given that the datasource is datatable, try


Dim textValue As String = CType(DimDropDownList1.SelectedItem, DataRowView)("Name")
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
vcharlesAuthor Commented:
I tried  Dim textValue As String = DropDownList1.SelectedItem.Text
But getting error message: Object reference not set to an instance of an object.

0
 
vcharlesAuthor Commented:
It doesn't like Dim textValue As String = CType(DropDownList1.SelectedItem, DataRowView)("Name")

Error message: value of type Sustem.Web.UI.WebControls.ListItem 'Can not be converted to System.data.DataRowView'
0
 
CodeCruiserCommented:
Hmm. Try

Dim textValue As String = DropDownList1.SelectedText
0
 
vcharlesAuthor Commented:
SelectedText  is not a property of the DrowpdownList control.
0
 
CodeCruiserCommented:
Ah. Off course its not. Strange thing is that you have set Name as the DataTextField but the Text is coming back as 2? 2 is the DonorID right? And the DDL is displaying BEL as text?
0
 
vcharlesAuthor Commented:
Yes 2 is the DonorID, the DDL is displaying 2 instead of BEL.
0
 
käµfm³d 👽Commented:
What function contains the code in which you are trying to access the DropDownList's text?
0
 
vcharlesAuthor Commented:
The latest code suggested did not work, still getteing a number (2) for Dropdownlist.text instead of the actule Name (BEL). below is the Function (Click event of a button) after displaying the data in the Dropdownlist from a previous Click event.

 Protected Sub Save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Save.Click
        MsgBox(DropDownList1.Text)
        Dim MyDocLink As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Link.xml"))
        Dim LinkID = (From el In MyDocLink.Descendants("Link") Select CInt(el.<Link_ID>.Value)).ToList
        LinkID.Sort()
        Dim an As String
        an = MsgBox("Are you sure you want to Add this record?", MsgBoxStyle.YesNo, "Warning before Attempting to Save Data")
        If an = MsgBoxResult.Yes Then
            '*********************************
            Dim MyDonor As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Donor.xml"))
            Dim search As String = DropDownList1.Text
            Dim searchId As String = Nothing
            Dim dom As New XmlDocument()
            dom.Load(Server.MapPath("~/App_Data/Donor.xml"))
            Dim list As XmlNodeList = dom.SelectNodes("//Donor")
            Dim CheckForItem = (From el In MyDonor.Descendants("Donor").Elements("DonorID") Select el.Value).ToList
            If CheckForItem.Contains(Me.DropDownList1.Text.ToString) Then
                For Each node As XmlNode In list
                    If node("DonorID").InnerText.Equals(search) Then
                        searchId = node("DonorID").InnerText
                        Exit For
                    End If
                Next
            End If
            '********************************
            Dim xml As String = LinkStream.InnerXml
            Dim newline As String = Environment.NewLine
            Dim replacement As String = "<Link><Link_ID>" & (LinkID.Last() + 1) & "</Link_ID><ReceiverID>" & C1Receiver.SelectedItem.Value & "</ReceiverID><DonorID>" & searchId & "</DonorID></Link>"
            LinkStream.InnerXml = LinkStream.InnerXml.Replace("</Root>", "") & replacement & "</Root>"
            LinkStream.Save(Server.MapPath("~/App_Data/Link.xml"))
            Dim cnt As Integer = LinkStream.SelectNodes("Root/Link").Count
            MsgBox("Data was Added")
        End If
        If an = MsgBoxResult.No Then
            MsgBox("Record was not Added")
        End If

    End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.