Results 1 to 2 of 2

Thread: Rsolved: Problem with Datagrid using XML

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Rsolved: Problem with Datagrid using XML

    I converted this from a C# project that I found online so I am not 100% sure that I did it right but it looks right to me. Here is the code I have:

    VB Code:
    1. Public Sub InitializeGrid()
    2.         Dim dg As DataGrid = TryCast(FindName("datagrid1"), DataGrid)
    3.         Dim doc As XDocument = XDocument.Load("StoresList2.xml")
    4.         Dim StoreData = From info In doc.Descendants("Store") _
    5.             Select New Stores With { _
    6.                 .Name = Convert.ToString(info.Element("Name").Value), _
    7.                 .Number = Convert.ToString(info.Element("Number").Value), _
    8.                 .Phone = Convert.ToString(info.Element("Phone").Value)}
    9.         dg.ItemsSource = StoreData
    10.     End Sub

    My Class looks like this:

    VB Code:
    1. Public Class Stores
    2.     Private Property _Number As String
    3.     Public Property Number() As String
    4.         Get
    5.             Return _Number
    6.         End Get
    7.         Set(ByVal value As String)
    8.             _Number = value
    9.         End Set
    10.     End Property
    11.  
    12.     Private _Name As String
    13.     Public Property Name() As String
    14.         Get
    15.             Return _Name
    16.         End Get
    17.         Set(ByVal value As String)
    18.             _Name = value
    19.         End Set
    20.     End Property
    21.  
    22.     Private _Phone As String
    23.     Public Property Phone As String
    24.         Get
    25.             Return _Phone
    26.         End Get
    27.         Set(ByVal value As String)
    28.             _Phone = value
    29.         End Set
    30.     End Property
    31.  
    32. End Class

    And then my XML file looks like this:
    XML Code:
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <company>
    3.   <store>
    4.     <Number>100</Number>
    5.     <Name>Cho Cho Puff Inc</Name>
    6.     <Phone>269-555-1212</Phone>
    7.   </store>
    8.   <store>
    9.     <Number>101</Number>
    10.     <Name>Cheerios Inc</Name>
    11.     <Phone>616-555-5555</Phone>
    12.   </store>
    13.  </company>

    The error that I am getting is this:

    System.NullReferenceException was unhandled by user code
    Message=Object reference not set to an instance of an object.


    on this line dg.ItemsSource = StoreData in the InitializeGrid sub



    I have stepped through the code and the data is being read from the file so I am not sure what the problem is. Any ideas?
    Last edited by Elfish; Aug 18th, 2010 at 06:33 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    28

    Re: Problem with Datagrid using XML

    Got it. There were two issues. Below is the modified sub with the corrected code.
    vb Code:
    1. Dim doc As XDocument = XDocument.Load("StoresList.xml")
    2.         Dim StoreData = From info In doc.Descendants("store") _
    3.             Select New clsStores With { _
    4.                 .Name = Convert.ToString(info.Element("Name").Value), _
    5.                 .Number = Convert.ToString(info.Element("Number").Value), _
    6.                 .Phone = Convert.ToString(info.Element("Phone").Value)}
    7.         Try
    8.             DataGrid1.ItemsSource = StoreData
    9.         Catch ex As Exception
    10.             MessageBox.Show(String.Format("{0} {1}", ex.Message, ex.StackTrace))
    11.         End Try

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width