Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with retreiving Text value from Dropdownlist control.

Posted on 2011-10-11
11
Medium Priority
?
247 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:vcharles
  • 5
  • 4
  • 2
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36949290
Have you tried using SelectedItem instead?

Dim textValue As String = DimDropDownList1.SelectedItem

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36949411
Given that the datasource is datatable, try


Dim textValue As String = CType(DimDropDownList1.SelectedItem, DataRowView)("Name")
0
 

Author Comment

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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:vcharles
ID: 36949484
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36949530
Hmm. Try

Dim textValue As String = DropDownList1.SelectedText
0
 

Author Comment

by:vcharles
ID: 36949564
SelectedText  is not a property of the DrowpdownList control.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36949662
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
 

Author Comment

by:vcharles
ID: 36949686
Yes 2 is the DonorID, the DDL is displaying 2 instead of BEL.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 36949734
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36949781
What function contains the code in which you are trying to access the DropDownList's text?
0
 

Author Comment

by:vcharles
ID: 36949879
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question