Help with error message using ASP.NET with XML


I can't figure out why I'm getting the following error message:
Object reference not set to an instance of an object.

On line:
dtDonor.Rows.Add({DonorID2, Name})
and dtReceiver.Rows.Add({ReceiverID2, Name}) when I comment out the Donor section.

I verified that C1Country.SelectedItem.Value is returning  the correct value, but still can figure out what is causing the errror.

        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
            Dim Link_ID As String

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

            ' Extract the Link_ID avalues from the current node

            LinkID2 = xnLink.SelectSingleNode("Link_ID").InnerText

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

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
Who is Participating?
In the initial page load, after you populate the datatables, put them in session

Session("dtDonor") = dtDonor

Then when you need it again when page posts back, retrieve it

dtDonor = Session("dtDonor")
vcharlesAuthor Commented:

Tables dtReceiver and dtDonor are already declared in my For Load event as follows:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack = False Then
            DonorStream.Load(Server.MapPath("~/App_Data/Donor.xml")) 'My.Resources.Donor)
            RecStream.Load(Server.MapPath("~/App_Data/Receiver.xml")) 'My.Resources.Receiver)
            LinkStream.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)
            xnLink.Load(Server.MapPath("~/App_Data/Link.xml")) 'My.Resources.Link)
            ' instanitate the datatables, and create the columns
            dtDonor = New DataTable
            dtDonor.Columns.Add("DonorID", GetType(String))
            dtDonor.Columns.Add("Name", GetType(String))

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

            dtLinkID = New DataTable
            dtLinkID.Columns.Add("Link_ID", GetType(String))

            'load C1Country
            Dim CountryOrigin As New DataSet
            C1Country.DataSource = CountryOrigin.Tables(0)
            C1Country.DataMember = "Name"
            C1Country.DataTextField = "Name"
            C1Country.DataValueField = "ReceiverID"
        End If
    End Sub
Do you get the error in initial page load or when posting back? Remember that in ASP.NET, you would need to store dtDonor and dtReceiver in session or recreate them if you want to use them when the page posts back.
vcharlesAuthor Commented:
I receive the error when I click on the search button, how do I make sure
The values set in the page load event is also available
When I run a search?
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.