I have vb.net application with MS access database.
I use following code to autocomplete textbox from database.
the problem is that, although its working but more frequently it gives an error "AccessViolationException was unhandled----Attempted to read or write protected memory. This is often an indication that other memory is corrupt." & the application is stoped working.
So what shuld i do?

Private Sub PatientNametxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PatientNametxt.TextChanged

Dim con As New OleDbConnection
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Pathology.accdb"
        con.Open()

        Dim dt As New DataTable
        Dim ds As New DataSet
        ds.Tables.Add(dt)

        Dim da As New OleDbDataAdapter("SELECT PatientName FROM Accessonbook", con)
        da.Fill(dt)

        Dim r As DataRow

        PatientNametxt.AutoCompleteCustomSource.Clear()

        For Each r In dt.Rows
            PatientNametxt.AutoCompleteCustomSource.Add(r.Item(0).ToString)
        Next

        con.Close()

    End Sub

Edited 2 Years Ago by Satyam_1: New query

So every time you are pressing a key you are opening a connection to the database and selecting and closing the connection. I don't think this is practical nor reliable. What you have to do is to create a dataview on the form level, and on the load event fill this dataview with the complete accessonbook. Now on textchanged event simply use the rowfilter method of the dataview to fill the auto-complete list. This is much faster and much more reliable and will probably solve your problem.

I have tried it in Page Load event. Its working to some extent.
But what could be done if I want to Autocomplete 3 textbox e.g. PatientNametxt , RegNotxt, Historytxt in the page load event?

This article has been dead for over six months. Start a new discussion instead.