Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server ADO.NET VB.NET , +
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 :
-------------------------
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:
----------------------------
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 Frown | :( thanks b4
Posted 29-Jul-12 18:44pm
om3n433
Edited 29-Jul-12 19:45pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
om3n at 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 at 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 at 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 at 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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 30 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100