show data grid view

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

enter image description here

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

Upvotes: 1

Views: 86

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29006

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

Upvotes: 1

Related Questions