简体   繁体   中英

How to filter a DataGridView with TextBox

I want to try to filter a DataGridView with a TextBox , but I'm receiving this error:

An unhandled exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll

Additional information: External component has thrown an exception

Dim strSql As String = "select * from Caixa where Recibo like '%" + textBox18.Text + "%'"
Dim con As New Conexao 
Dim cmd As New npgSqlCommand(strSql, Conexao.Conectar)   
cmd.CommandType = CommandType.Text

Dim da As New npgSqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
GDLoadCaixa.DataSource = dt
Dim con As New Conexao 
Dim cmd As New npgSqlCommand(strSql, Conexao.Conectar)   

These lines smells. You create a connection object (good), then you don't use it but you rely on a global object to establish the connection to the db inside the sqlCommand creation (bad). Don't forget that npgSqlCommand expects a connection object, with could be different than whatever Conexao.Conectar is returning.

You probably want something like

Dim con As New Conexao 
con.Connectar() 'Connect THIS connection  
Dim cmd As New npgSqlCommand(strSql, con) 'use THIS connection   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM