-1
private Sub Btn_Show_Click(sender As Object, e As RoutedEventArgs)
       Dim dt As New DataTable()
       Dim SearchCriteria = cmbSearch.Text
       dt.Columns.Add("Title", GetType(String))
       dt.Columns.Add("Author", GetType(String))
       dt.Columns.Add("AccessionNo", GetType(String))
       dt.Columns.Add("Location", GetType(String))
       dt.Columns.Add("Status", GetType(String))
       Dim xmldoc As New XmlDocument()
       xmldoc.Load("C:\Users\Shahrukh\Documents\Visual Studio 2012\Projects\Simple search1\Simple search1\New folder\Data.xml")
       Dim nodeList As XmlNodeList = xmldoc.SelectNodes("/NewDataSet/Table")


       If SearchCriteria = "Title" Then
           For Each node As XmlNode In nodeList
               Dim dtrow As DataRow = dt.NewRow()
               If (LCase(node("title").InnerText).Contains(LCase(txtSearch.Text()))) Then
                   dtrow("Title") = node("title").InnerText
                   dtrow("Author") = node("Author").InnerText
                   dtrow("AccessionNo") = node("AccessionNo").InnerText
                   dtrow("Location") = node("location").InnerText
                   dtrow("Status") = node("status").InnerText
                   dt.Rows.Add(dtrow)
               End If
           Next
       End If

       If SearchCriteria = "Author" Then
           For Each nodeAuthor As XmlNode In nodeList
               Dim dtrow As DataRow = dt.NewRow()
               If (LCase(nodeAuthor("Author").InnerText).Contains(LCase(txtSearch.Text()))) Then
                   dtrow("title") = nodeAuthor("title").InnerText
                   dtrow("Author") = nodeAuthor("Author").InnerText
                   dtrow("AccessionNo") = nodeAuthor("AccessionNo").InnerText
                   dtrow("location") = nodeAuthor("location").InnerText
                   dtrow("status") = nodeAuthor("status").InnerText
                   dt.Rows.Add(dtrow)
               End If
           Next
       End If
       dGridResults.ItemsSource = dt.DefaultView
   End Sub

It gives an Error "Object reference not set to an instance of an object."

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shahrukh
  • 1
  • 2

1 Answers1

0

I think I see an inconsistency. You have a Table with the column "Title". In node/nodeAuthor you fetch "title" both times (so I figure that is ok). But for dtRow you write to "Title" in the first if and "title" in the second. Change that to "Title" and try again.

               dtrow("Title") = node("title").InnerText
WozzeC
  • 2,630
  • 1
  • 13
  • 12
  • still the error is on and that point to the "If (LCase(nodeAuthor("author").InnerText).Contains(LCase(txtSearch.Text()))) Then" – shahrukh Nov 05 '15 at 10:05