Help with retreiving ID value from Dropdownlist


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
Who is Participating?
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?
vcharlesAuthor Commented:
Ok, I will try it. No I'm not adding any columns.
vcharlesAuthor Commented:
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?

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

> 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"

vcharlesAuthor Commented:
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.


vcharlesAuthor Commented:
Thank You!
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.