Help with error message using  ASP.NET with XML

Posted on 2011-10-12
Last Modified: 2012-05-12

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"
Question by:vcharles

    Author Comment


    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
    LVL 83

    Expert Comment

    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.

    Author Comment

    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?
    LVL 83

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    779 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