Click here to Skip to main content
16,006,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using below code for searching but its properly work on only title criteria and when i select the another criteria like author, publisher etc its give an error "Object reference not set to an instance of an object."


code:
VB
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
Posted
Updated 4-Nov-15 9:24am
v2
Comments
Richard Deeming 6-Nov-15 13:51pm    
If you want to update your question, use the "Improve question" button. Don't post the update as a new question.

Your reposted copy of this question has been removed.

1 solution

You aren't consistent in the UPPER/lower case of the strings you use to index into dtrow and they don't always match the column names you specified above.
VB
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  ' fixed
                   dtrow("Author") = nodeAuthor("Author").InnerText
                   dtrow("AccessionNo") = nodeAuthor("AccessionNo").InnerText
                   dtrow("Location") = nodeAuthor("location").InnerText  ' fixed
                   dtrow("Status") = nodeAuthor("status").InnerText  ' fixed
                   dt.Rows.Add(dtrow)
               End If
           Next
       End If
       dGridResults.ItemsSource = dt.DefaultView
   End Sub

You should use named constants instead of in-line strings to avoid this. The constants define the string value and the compiler will enforce your using the correct casing/spelling of the names.
 
Share this answer
 
Comments
Member 12112060 5-Nov-15 1:02am    
that code you have post that's right code for it?
i used that code but it also give an same error
pls help me to solve that error
Member 12112060 5-Nov-15 1:34am    
still that error is come please provide me proper way to solve that.
thanks
shahurukh
Matt T Heffron 5-Nov-15 12:56pm    
I can't be sure that my correction is for your exact problem, but it was a reasonable guess, since it was an apparent error. You need to use the debugger to set a breakpoint, probably on the SelectNodes line, and then single step the code and examine the variables and values as you go, to see what is "Nothing" in the "Author" case but is valid in the "Title" case. Then you need to determine Why they are different. That may mean running it twice.
Member 12112060 6-Nov-15 0:04am    
IT CAUSES A PROBLEM ON THE POINT "If (LCase(nodeAuthor("author").InnerText).Contains(LCase(txtSearch.Text()))) Then"
Matt T Heffron 6-Nov-15 12:40pm    
So is the name in the nodeList "author" or "Author"?
The UPPER/lower casing matters everywhere.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900