• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

Help with error message using ASP.NET with XML

Hello,

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.

Code:
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))

        ' 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})
            Next

            ' 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})
            Next

            ' Extract the Link_ID avalues from the current node

            LinkID2 = xnLink.SelectSingleNode("Link_ID").InnerText
            dtLinkID.Rows.Add({LinkID2})
        Next

        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
        CmbRec.DataBind()
0
vcharles
Asked:
vcharles
  • 2
  • 2
1 Solution
 
vcharlesAuthor Commented:
Hi,

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
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            C1Country.DataSource = CountryOrigin.Tables(0)
            C1Country.DataMember = "Name"
            C1Country.DataTextField = "Name"
            C1Country.DataValueField = "ReceiverID"
            C1Country.DataBind()
        End If
    End Sub
0
 
CodeCruiserCommented:
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.
0
 
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?
Thanks,
Victor
0
 
CodeCruiserCommented:
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")
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now