Reputation: 35
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();
Upvotes: 1
Views: 86
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:
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