简体   繁体   English

如何使用TextBox过滤DataGridView

[英]How to filter a DataGridView with TextBox

I want to try to filter a DataGridView with a TextBox , but I'm receiving this error: 我想尝试使用TextBox过滤DataGridView ,但是我收到此错误:

An unhandled exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll Npgsql.dll中发生了'Npgsql.NpgsqlException'类型的未处理异常

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). 创建一个连接对象(好的),然后不使用它,但是您依靠一个全局对象在sqlCommand创建内部建立与数据库的连接(不好的)。 Don't forget that npgSqlCommand expects a connection object, with could be different than whatever Conexao.Conectar is returning. 不要忘记npgSqlCommand需要一个连接对象,它可能与Conexao.Conectar返回的对象不同。

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   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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