2016-08-27 1 views
0

どうすればこの問題を解決できますか?'使用前にデータテーブルを初期化する'

私は単純なデータベースプロジェクトを行っています。検索フィールドがあります。DataGridViewでデータを検索して表示するためのコードを書きましたが、 Null参照例外が表示されています。私はそれを取得しないので、警告を初期化する前に使用されます。

質問は次のとおりです。テーブルを正しく初期化するにはどうすればよいですか。

here is the screenshot of the form

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 > 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 <> "" 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 <> "") Then 
     contactView.DataSource = foundRows 
    Else ' else show the original tavle again 
     contactView.DataSource = dataSet.Tables(0) 
    End If 
    contactView.Refresh() 'refresh 
End Sub 
iは

Dim foundRows As DataTable = New DataTable 

を使用しようとしたが、それはArgumentExceptionが

この行が既に別のテーブルに属する示す

i also tried this

しかし、あなたが見ることができるように何も動作しません!

答えて

0

はい は、私は最終的にそれを

私は

はこれが最善の

をされています新しいテーブルを宣言するとき、列を初期化する

Dim foundRows As DataTable = New DataTable("PseuContact") 'this initializig causes the problem of null exception,But how can i Initialize it then????? 
    foundRows.Columns.Add("ID") 
    foundRows.Columns.Add("First Name") 
    foundRows.Columns.Add("Last Name") 
    foundRows.Columns.Add("Phone Number") 
    foundRows.Columns.Add("Mobile Number") 
    foundRows.Columns.Add("Email Address") 

が重要である列の問題を抱えていたです

関連する問題