Error with textbox in VB.Net using autocomplete

1

I have the following code to perform an autocomplete in several textboxes that I have in my application. It is connected to a database in another computer that is a server. But the error gives me as much of the clients as of the servers.

Taking the issue of sqlinjection out that I know my code is vulnerable, there are times where the code works and moments where it does not.

I clarify that the base is very large and the query I show is just one of the textbox that I will use. The info is taken from a table called employees, and the column is nro_doc.

Public Sub autocomplete (ByVal textbx As TextBox, ByVal table As String, ByVal description As String) Dim cmd As New OleDb.OleDbCommand Dim res As DataTable Dim sql As String=""

sql &= "SELECT top 10 " & descripcion & " FROM " & tabla & " WHERE " & descripcion & " LIKE '" & textbx.Text & "%'"


If conectar() = resultado.ok Then
    Try
        If textbx.Text <> "" Then
            res = acceso.consulta(sql)
            If res.Rows.Count() <> 0 Then
                textbx.AutoCompleteSource = AutoCompleteSource.None
                If res.Rows.Count() <> 0 Then
                    Dim c As Integer = 0
                    For c = 0 To res.Rows.Count() - 1
                        textbx.AutoCompleteSource = AutoCompleteSource.CustomSource
                        textbx.AutoCompleteMode = AutoCompleteMode.Suggest
                        textbx.AutoCompleteCustomSource.Add(res.Rows(c).ToString)
                        'textbx.AutoCompleteCustomSource = res.Item(descripcion)
                    Next
                End If
            End If
        End If

    Catch ex As Exception
    MessageBox.Show("Error al intentar conectar", "Error grave")
    Me.ultimo_error = ex.Message
End Try
End If

End Sub In turn, the method I'm calling from the textbox ChangeText event.

Private Sub txt_nro_documento_TextChanged (sender As Object, and As EventArgs) Handles txt_nro_documento.TextChanged acceso.autocompletar (txt_nro_documento, "EMPLOYEES", "nro_doc") End Sub

And now yes, the error that you are giving me is this:

system.accessviolationexception attempt to read or write to protected memory. This often indicates that there is another corrupted memory.

I searched everywhere but I have not found a solution that suits my problem. I put the textbox because before this did not happen and I think that everything is originating there because when I take out the autocomplete there are no errors.

Thank you, I hope someone can help me, I'm a novice, so you need patience!

    
asked by Lorena Soledad Ledesma 04.04.2017 в 07:39
source

1 answer

0

Suggestions

Change

Public Sub autocompletar(ByVal textbx As TextBox, 
      ByVal tabla As String, ByVal descripcion As String)

make it

Public Sub autocompletar(ByRef textbx As TextBox, 
       ByVal tabla As String, ByVal descripcion As String)

basically it is changing that the TextBox passes by reference and not by value.

Before

If conectar() = resultado.ok Then

make the changes:

textbx.AutoCompleteSource = AutoCompleteSource.None
textbx.AutoCompleteMode = AutoCompleteMode.None
textbx.AutoCompleteCustomSource.Clear

Inside

If res.Rows.Count() <> 0 Then

perform

textbx.AutoCompleteSource = AutoCompleteSource.CustomSource
textbx.AutoCompleteMode = AutoCompleteMode.Suggest

and only within For

textbx.AutoCompleteCustomSource.Add(res.Rows(c).ToString)
    
answered by 04.04.2017 в 08:27