Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Dear All,

sorry for my english, I'm newbie in .Net programming
I am using vb. net 2010 framework 4. I want to make the autocomplete textbox but get error message "AccessViolationException". provisions:
2 pieces that use autocomplete textbox, textbox each data retrieved from a database type (sql server) but different databases. the code that I know:

First code of textbox 1 :
-------------------------
VB
Call OpenInventory()
CommandInv.Connection = ConInv
CommandInv.CommandType = CommandType.Text
CommandInv.CommandText = "SELECT ART_NAME FROM ARTIST"
ObjDtAdapterInv.SelectCommand = CommandInv
ObjDtAdapterInv.Fill(objDataSetInv, "Artist")
ObjDtAdapterInv.Dispose()
Dim col As New AutoCompleteStringCollection
Dim i As Integer
For i = 0 To objDataSetInv.Tables("Artist").Rows.Count - 1
    col.Add(objDataSetInv.Tables("Artist").Rows(i)("ART_NAME").ToString())
Next
TBArtist.AutoCompleteSource = AutoCompleteSource.CustomSource
TBArtist.AutoCompleteCustomSource = col
TBArtist.AutoCompleteMode = AutoCompleteMode.Suggest
CommandInv.Dispose()
ObjDtAdapterInv.Dispose()
ConInv.Close()



secondary code of textbox 2:
----------------------------
VB
Call OpenAuction()
CommandAuct.Connection = ConAuct
CommandAuct.CommandType = CommandType.Text
CommandAuct.CommandText = "SELECT cli_firstname FROM tclient WHERE cli_firstname LIKE '%" & TBOwner.Text & "%'"
ObjDtAdapterAuct.SelectCommand = CommandAuct
ObjDtAdapterAuct.Fill(objDataSetAuct, "Owner")
Dim ColOwner As New AutoCompleteStringCollection
 Dim i As Integer
For i = 0 To objDataSetAuct.Tables("Owner").Rows.Count - 1
ColOwner.Add(objDataSetAuct.Tables("Owner").Rows(i)("cli_firstname").ToString())
 Next
TBOwner.AutoCompleteSource = AutoCompleteSource.CustomSource
TBOwner.AutoCompleteCustomSource = ColOwner
TBOwner.AutoCompleteMode = AutoCompleteMode.Suggest
CommandAuct.Dispose()
ObjDtAdapterAuct.Dispose()
ConAuct.Close()


The above code is running normal if only one textbox is executed, if executed after the first textbox and then executes the second textbox then get the error message. Please help urgent :( thanks b4
Posted
Updated 29-Jul-12 18:45pm
v2

1 solution

There are issues with changing the AutoCompleteSource on the fly. I am assuming your secondary code is happening as the user is typing i.e. TextChanged event.
I have heard that setting the AutoCompleteSource to AutoCompleteSource.None prior to re-assigning the new source should solve the AccessViolation issue but have not tried it myself.
 
Share this answer
 
Comments
om3n 30-Jul-12 3:45am    
Thanks Trak4net,
As you say, The following is the complete code that I have tried :
==========================
Connection Module
==========================
Imports System.Data
Imports System.Data.SqlClient

Module Connection
Public ConStrInv As String
Public ConStrAuct As String

Public ConInv As New SqlConnection
Public ConAuct As New SqlConnection

Public CommandInv As New SqlCommand
Public CommandAuct As New SqlCommand

Public ObjDtAdapterInv As New SqlDataAdapter
Public ObjDtAdapterAuct As New SqlDataAdapter

Public objDataSetInv As New DataSet
Public objDataSetAuct As New DataSet

Public Sub OpenInventory()
ConStrInv = "Server=h04mc3kn;Database=inventory;user id=sa;password=123;"
ConInv.ConnectionString = ConStrInv
Try
If ConInv.State <> ConnectionState.Closed Then
ConInv.Close()
ConInv.Open()
Else
ConInv.Open()
End If
Catch ex As Exception
MsgBox("Sorry Can not open connection ! ")
End Try
End Sub

Public Sub OpenAuction()
ConStrAuct = "Server=h04mc3kn;Database=auction;user id=sa;password=123;"
ConAuct.ConnectionString = ConStrAuct
Try
If ConAuct.State <> ConnectionState.Closed Then
ConAuct.Close()
ConAuct.Open()
Else
ConAuct.Open()
End If
Catch ex As Exception
MsgBox("Sorry Can not open connection ! ")
End Try
End Sub
End Module

============
Form
============
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Private Sub TBArtist_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBArtist.TextChanged
Call OpenInventory()
CommandInv.Connection = ConInv
CommandInv.CommandType = CommandType.Text
CommandInv.CommandText = "SELECT ART_NAME FROM ARTIST"
ObjDtAdapterInv.SelectCommand = CommandInv
ObjDtAdapterInv.Fill(objDataSetInv, "Artist")
ObjDtAdapterInv.Dispose()
Dim col As New AutoCompleteStringCollection
Dim i As Integer
For i = 0 To objDataSetInv.Tables("Artist").Rows.Count - 1
col.Add(objDataSetInv.Tables("Artist").Rows(i)("ART_NAME").ToString())
Next
TBArtist.AutoCompleteSource = AutoCompleteSource.CustomSource
TBArtist.AutoCompleteCustomSource = col
TBArtist.AutoCompleteMode = AutoCompleteMode.Suggest
CommandInv.Dispose()
ObjDtAdapterInv.Dispose()
ConInv.Close()
End Sub

Private Sub TBOwner_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBOwner.TextChanged
Call OpenAuction()
CommandAuct.Connection = ConAuct
CommandAuct.CommandType = CommandType.Text
CommandAuct.CommandText = "SELECT cli_firstname FROM tclient WHERE cli_firstname LIKE '%" & TBOwner.Text & "%'"
ObjDtAdapterAuct.SelectCommand = CommandAuct
ObjDtAdapterAuct.Fill(objDataSetAuct, "Owner")
Dim ColOwner As New AutoCompleteStringCollection
Dim i As Integer
For i = 0 To objDataSetAuct.Tables("Owner").Rows.Count - 1
ColOwner.Add(objDataSetAuct.Tables("Owner").Rows(i)("cli_firstname").ToString())
Next
TBOwner.AutoCompleteSource = AutoCompleteSource.CustomSource
TBOwner.AutoCompleteCustomSource = ColOwner
TBOwner.AutoCompleteMode = AutoCompleteMode.Suggest
CommandAuct.Dispose()
ObjDtAdapterAuct.Dispose()
ConAuct.Close()
End Sub
End Class

First step, I typed in the TBArtist, a result I managed to get the Artist's data after that I typed again in the TBOwner. when I enter the first character of the word "MR" there is no error message, when I enter the second character is "R" I get the error message :
"AccessViolationException was unhandled"
Attempted to read or write
om3n 30-Jul-12 3:47am    
Error message :
"AccessViolationException was unhandled"
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Inventory.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Trak4Net 30-Jul-12 4:18am    
You will probably want to change your logic a little. If you clear the autocomplete source when the text box is empty and only re-initialize the autocomplete source with your query when TBOwner.Text.Length = 1, if the user keeps typing the auto complete source will auto filter as they type.
I think there is a timing issue with changing the auto complete source while it is trying to filter in background.

Something along the lines of this (in a simplified example)
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length = 1 Then
Dim col As New AutoCompleteStringCollection()
col.Add("Seven")
col.Add("Sentinal")
col.Add("Seventy")
col.Add("Sensorship")
TextBox1.AutoCompleteCustomSource = col
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
ElseIf TextBox1.Text.Length = 0 Then
TextBox1.AutoCompleteMode = AutoCompleteMode.None
TextBox1.AutoCompleteSource = AutoCompleteSource.None
TextBox1.AutoCompleteCustomSource = Nothing
End If
End Sub
om3n 31-Jul-12 0:44am    
Thank you so much Trak4Net, the problem can be solved. you're a genius. I was motivated to study even harder
Trak4Net 31-Jul-12 1:56am    
Glad I could help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900