Results 1 to 3 of 3

Thread: database search revived...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    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!

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Your exception is coming from this method:
    VB Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.     Dim ds1 As New DataSet()
    3.     Try
    4.         'this assumes textbox1 is where the name is to search for
    5.         Dim filter As String = String.Format("CustomerName Like '{0}*'", txtsearch.Text)
    6.         'Nowhere are you populating this dataset with data!!!
    7.         ds1.Tables("CustomerName").DefaultView.RowFilter = filter
    8.     Catch ex As Exception
    9.         MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
    10.     End Try
    11. 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:
    1. Private AppPath As String = Replace(Application.ExecutablePath, Application.ProductName + ".exe", "")
    2. Private ds As New DataSet()
    3. Private conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Quotation.mdb"
    4. Private cn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(conStr)
    5. Private daCust As New System.Data.OleDb.OleDbDataAdapter("Select * from CustomerName", cn)
    6.  
    7. Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8. Try
    9.     ds.Clear()
    10.     daCust.Fill(ds)
    11. Catch ex As Exception
    12.     MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
    13. End Try
    14.     DataGrid1.DataSource = ds
    15.     DataGrid1.DataMember = ds.Tables(0)
    16. End Sub
    17.  
    18. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    19.     Try
    20.         'this assumes textbox1 is where the name is to search for
    21.         Dim filter As String = String.Format("CustomerName Like '{0}*'", txtsearch.Text)
    22.         ds.Tables(0).DefaultView.RowFilter = filter
    23.     Catch ex As Exception
    24.         MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error")
    25.     End Try
    26. End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    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
  •  



Click Here to Expand Forum to Full Width