title: 'Initializing data table before use'
body: <p>how can i solve this please ? </p>
  
  <p>i am doing a simple database project and i have a search field i wrote the following code for searching and displaying data in DataGridView  but the 
  Null Reference Exception keeps showing up because i'm initializing the table with nothing and yet if i don't do so  i get 'Used Before Initializing warning.</p>
  
  <p>the question is :how can i initialize the table properly .</p>
  
  <pre><code>Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As    System.EventArgs) Handles txtSearch.TextChanged
      Dim searchText As String = txtSearch.Text
      Dim matchText As String = ""
      Dim rowMatch As Boolean = False
      Dim foundRows As DataTable = Nothing 'this initializig causes the   problem of null exception,But how can i Initialize it then?????
  
  
  For Each rw As DataRow In dataSet.Tables(0).Rows
  
          Dim cl As String = rw.Item(1).ToString '  this cell is FirstName  Cell
          If searchText.Length &gt; cl.ToString.Length Then
              matchText = cl.ToString
          Else
              matchText = cl.ToString.Substring(0, searchText.Length)
          End If
  
          'bellow it adds the Found Row (rw) to the table 
          If (searchText.Equals(matchText) And searchText &lt;&gt; "" And Not foundRows Is Nothing) Then foundRows.Rows.Add(rw)
  
  Next
  
  
      'to shows data if the search text field is not empty then show the found matching rows
      If (searchText &lt;&gt; "") Then
          contactView.DataSource = foundRows
      Else ' else show the original tavle again 
          contactView.DataSource = dataSet.Tables(0)
      End If
      contactView.Refresh() 'refresh 
  End Sub
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/5fW8s.jpg" rel="nofollow">here is the screenshot of the form</a></p>
  
  <p>i tried to use </p>
  
  <pre><code>Dim foundRows As DataTable = New DataTable 
  </code></pre>
  
  <p>but it shows <strong>ArgumentException</strong> </p>
  
  <p><strong><em>This Row already belongs to another Table</em></strong></p>
  
  <p><a href="http://i.stack.imgur.com/UnZ9B.jpg" rel="nofollow">i also tried this</a></p>
  
  <p>but as you can see nothing works please Help !</p>