Empty argument. .Net and SQL

2

The following function gives me the following error:

System.ArgumentNullException: 'El valor no puede ser nulo.
Nombre del parámetro: dataTable'

I executed the Query, directly in SQL and I had an answer. So I do not understand why it does not bring the data I need.

Private Sub txt_ppu_TextChanged(sender As Object, e As EventArgs) Handles txt_ppu.TextChanged
    Dim cont_ As Integer = 0
    If Len(txt_ppu.Text) > 2 Then
        query_ = "SELECT Placa FROM Seremitt WHERE Placa LIKE '" + txt_ppu.Text + "%'"
        Dim dr As New SqlDataAdapter(query_, IConexion)
        dr.Fill(dt)

        MsgBox(dt.Rows.Count)
    End If
End Sub
    
asked by Luippo 05.12.2017 в 15:29
source

1 answer

2

This error occurs because your DataTable is not initialized. Where you have defined dt , you must change it to be something like this:

Dim dt As New DataTable

By the way, you should never compose your query by concatenation of strings, since this makes it vulnerable to the Injection of SQL . Always use parameters:

query_ = "SELECT Placa FROM Seremitt WHERE Placa LIKE @placa"
Dim selectCommand AS SqlCommand = New SqlCommand(query_, IConexion)
selectCommand.Parameters.Add("@placa", SqlDbType.VarChar, 80).Value = txt_ppu.Text + "%"
Dim dr As New SqlDataAdapter(selectCommand)
dr.Fill(dt)
    
answered by 05.12.2017 в 15:34