简体   繁体   中英

show data grid view

I have a problem with show data of grid view when I press button "agregar":

在此处输入图片说明

It shows a little square and show no data.

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();

You have several issues with your query, A quick fix is by giving a space in between "' and and in "'and , By this you are opening a door for hackers through injection, So better option is use of parameterized queries. few more suggestions:

  1. You are collecting the query result to a DataTable/DataSet using adapter, so you need not to Execute the query before that
  2. You are fetching the values using a single query so it is not necessary to use DataSet here and followed by taking required table from Dataset, instead of that you can directly fetch the result table to a DataTable using Adapter.
  3. You can make use of Using blocks as well

In short the code for binding the grid should be like this:

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();

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