2016-08-27 2 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 
제가

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") 

중요 열에서 문제가 된있어

관련 문제