Help with retreiving ID value from Dropdownlist

Posted on 2011-10-12
Last Modified: 2012-08-14

I am loading my Dropdownlist (CmbRec) using code A, buyt when I use code B and try to use ReceiverID of the first row displayed, with the following code.

I receive the following error:

But I am able to retrieve DonorID from Dropdownlist1, can’t figure out why it’s working for Dropdownlist1 and not for CmbRec. Can you please help me figure out why I can’t extract the ReceiverID value from CmbRec.
Code A:
Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents

        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")

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

            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            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})

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim ReceiverID2 As String
                Dim Name As String

                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})


        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"

        DropDownList1.DataSource = dtDonor
        DropDownList1.DataTextField = "Name"
        DropDownList1.DataValueField = "DonorID"

Code B:
Dim an As String
        an = MsgBox("Are you sure you want to Delete this record?", MsgBoxStyle.YesNo, "Warning before Attempting to Save Data")
        MsgBox(CmbRec.SelectedItem.Value) *****Error
        If an = MsgBoxResult.Yes Then
            Dim xml As String = LinkStream.InnerXml
            Dim newline As String = Environment.NewLine
            Dim replacement As String = "<Link><ReceiverID>" & CmbRec.SelectedItem.Value & "</ReceiverID><DonorID>" & DropDownList1.SelectedItem.Value & "</DonorID></Link>"
            LinkStream.InnerXml = xml.Replace(replacement, "")
            Dim cnt As Integer = LinkStream.SelectNodes("Root/Link").Count
            MsgBox("Record was deleted")
        End If
        If an = MsgBoxResult.No Then
            MsgBox("Record was not deleted")
        End If
    End Sub
Question by:vcharles
    LVL 83

    Accepted Solution

    Place a breakpoint somewhere and examine all the SelectedIndex, SelectedItem, SelectedValue etc properties of both DDLs and see what's coming up.

    Also, are you adding columns to dtDonor and dtReceiver anywhere?

    Author Comment

    Ok, I will try it. No I'm not adding any columns.

    Author Comment

    Strange outcome:
    When I include:
    In codeA, they both return the ID values, than when I include the same thing at the beginning of CodeB:
    CmbRec returns a Blank value, but DropDownList1 returns the same DonorID that was returned in the mesaagebox in Code A.
    Any ideas why CmbRec is not returning ReceiverID in Code B?

    LVL 83

    Expert Comment

    > No I'm not adding any columns.
    How does the dropdown then know which column is Name and which one is ReceiverID when you use the code

    CmbRec.DataSource = dtReceiver
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"


    Author Comment

    I am using:

                dtReceiver = New DataTable
                dtReceiver.Columns.Add("ReceiverID", GetType(String))
                dtReceiver.Columns.Add("Name", GetType(String))
                dtDonor = New DataTable
                dtDonor.Columns.Add("DonorID", GetType(String))
                dtDonor.Columns.Add("Name", GetType(String))

    I was able to fix the bug.



    Author Closing Comment

    Thank You!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now