|
-
Jun 30th, 2004, 02:34 AM
#1
Thread Starter
Lively Member
database search revived...
hi! I have seen the post about this before and tried following the solutions given by one of our friends re: searching a database. my problem now is when i click the search button, I get an error:
Object reference not set to an instance of an object.
here is my code:
Dim AppPath As String = Replace(Application.ExecutablePath, Application.ProductName + ".exe", "")
Dim ds As New DataSet()
'Dim dbAdaptr As New System.Data.OleDb.OleDbDataAdapter("Select * from CustomerName", cn)
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Quotation.mdb"
Dim cn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(conStr)
Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim daCust As New System.Data.OleDb.OleDbDataAdapter("Select * from CustomerName", cn)
Dim dtcust As DataTable
cn.Open()
Try
ds.Clear()
daCust.Fill(ds)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
dtcust = ds.Tables(0)
DataGrid1.DataSource = dtcust
'datagrid1.DataMember=
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim ds1 As New DataSet()
Try
'this assumes textbox1 is where the name is to search for
Dim filter As String = String.Format("CustomerName Like '{0}*'", txtsearch.Text)
ds1.Tables("CustomerName").DefaultView.RowFilter = filter
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
End Sub
That's it...i'm wondering where did I go wrong...please enlighten me on this soon! my boss is killing me with pressure and I'm just a beginner!
thanks a bunch!
-
Jun 30th, 2004, 06:51 AM
#2
Hyperactive Member
Your exception is coming from this method:
VB Code:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim ds1 As New DataSet()
Try
'this assumes textbox1 is where the name is to search for
Dim filter As String = String.Format("CustomerName Like '{0}*'", txtsearch.Text)
'Nowhere are you populating this dataset with data!!!
ds1.Tables("CustomerName").DefaultView.RowFilter = filter
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
End Sub
You can add your datatable that you extracted from your other datasource but you would have to declare it as a form level object.
Try replacing your code with this:
VB Code:
Private AppPath As String = Replace(Application.ExecutablePath, Application.ProductName + ".exe", "")
Private ds As New DataSet()
Private conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Quotation.mdb"
Private cn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(conStr)
Private daCust As New System.Data.OleDb.OleDbDataAdapter("Select * from CustomerName", cn)
Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
ds.Clear()
daCust.Fill(ds)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
DataGrid1.DataSource = ds
DataGrid1.DataMember = ds.Tables(0)
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Try
'this assumes textbox1 is where the name is to search for
Dim filter As String = String.Format("CustomerName Like '{0}*'", txtsearch.Text)
ds.Tables(0).DefaultView.RowFilter = filter
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
End Try
End Sub
-
Jun 30th, 2004, 06:27 PM
#3
Thread Starter
Lively Member
ok...this is a follow-up question...I hope you all would take a little of your time to answer me.
I'm just wondering, will this work with a master/detail form? What part of the code should I change?
Thanks!
Last edited by siomai; Jul 28th, 2004 at 04:05 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|