C# 显示数据网格视图
当我按下按钮“agregar”时,我在显示网格视图数据时遇到问题: 它显示一个小正方形,不显示任何数据C# 显示数据网格视图,c#,asp.net,forms,gridview,C#,Asp.net,Forms,Gridview,当我按下按钮“agregar”时,我在显示网格视图数据时遇到问题: 它显示一个小正方形,不显示任何数据 cn.Open(); MySqlCommand cm = cn.CreateCommand(); cm.CommandType = CommandType.Text; cm.CommandText = "select * from detalle where iddetalle= '" + txt_boleta.Text + "'and idlocal='" + txtlocal.Text
cn.Open();
MySqlCommand cm = cn.CreateCommand();
cm.CommandType = CommandType.Text;
cm.CommandText = "select * from detalle where iddetalle= '" + txt_boleta.Text + "'and idlocal='" + txtlocal.Text + "'";
cm.ExecuteNonQuery();
MySqlDataAdapter da = new MySqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds,"data");
GridView1.DataSource = ds.Tables["data"];
GridView1.DataBind();
您的查询有几个问题,一个快速解决方法是在
“'
和和和之间留出一个空格,这样您就可以通过注入为黑客打开一扇门,因此更好的选择是使用参数化查询。还有几点建议:
您正在使用适配器将查询结果收集到DataTable/DataSet,因此在此之前无需执行查询
您使用单个查询获取值,因此不必在此处使用DataSet,然后从DataSet获取所需的表,而可以使用Adapter直接将结果表获取到DataTable
也可以使用块
简言之,绑定网格的代码应如下所示:
DataTable dsDetalle=new DataTable("Data");
using (MySqlCommand commandSql = cn.CreateCommand())
{
commandSql.CommandType = CommandType.Text;
commandSql.CommandText = "select * from detalle where iddetalle=@iddetalle and idlocal=@idlocal";
commandSql.Parameters.AddWithValue("@iddetalle", "txt_boleta.Text");
commandSql.Parameters.AddWithValue("@idlocal", "txtlocal.Text");
MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(commandSql);
sqlAdapter.Fill(dsDetalle);
}
GridView1.DataSource = dsDetalle;
GridView1.DataBind();
因为你的查询执行得不好,如果它执行就会通过注入欢迎黑客。谢谢你的回答,但是当我复制代码时会出现下一个错误sqlAdapter.Fill(dsDetalle,“data”);错误2:System.Data.Common.DataAdapter.Fill(System.Data.DataTable,System.Data.IDataReader)段落的método sobrecargado重合点“tiene algunos argumentos no válidos C:\Users\Hardfix1\Documents\Visual Studiothanks但当更新代码时,ide显示下一个错误错误2 Uso de la variable local no asignada”dsDetalle“对不起,我不知道,但我是新来的programation@WladimirArnaldoBriceoMirand:谢谢你的尝试和回复,我在帖子中做了更新,你能试试这些改变吗?谢谢你更快的回答,我更新了代码,但在最后一个标签下面按agregar Button时,图片也有同样的问题