Private Sub txtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown
If txtSearch.Text <> "" Then
If (e.KeyCode = Keys.Enter) Then
Dim sqlBook As String = ""
sqlBook = "SELECT * FROM Student"
Select Case cboSearch.SelectedIndex
Case 0
sqlBook &= " WHERE (Class='" & txtsearch.Text & "')"
Case 1
sqlBook &= " WHERE (School LIKE '%" & txtsearch.Text & "%')"
Case 2
sqlBook &= " WHERE (Nickname LIKE '%" & txtsearch.Text & "%')"
End Select
sqlBook &= " ORDER BY Class"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
da.SelectCommand.CommandText = sqlBook
If IsFind = True Then
ds.Tables("Student").Clear()
End If
da.Fill(ds, "Student")
If ds.Tables("Student").Rows.Count <> 0 Then
IsFind = True
ClearAllBinding()
Else
IsFind = False
MessageBox.Show("ไม่พบข้อมูลนักเรียนที่ค้นหา", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearData()
bnPosition.Text = ""
tsAdd.Enabled = True
tsEdit.Enabled = False
bnMovefirst.Enabled = False
bnMovePrevious.Enabled = False
bnMovenext.Enabled = False
bnMovelast.Enabled = False
txtsearch.Focus()
txtsearch.SelectAll()
Exit Sub
End If
End If
End If
End Sub
---------------------------------
เวลาerror จะฟ้องว่า Object reference not set to an instance of an object แล้วกระพริบตรงคำสั่ง da.SelectCommand.CommandText = sqlBook