c#
  • asp.net
  • forms
  • gridview
  • 2017-01-10 108 views 1 likes 
    1

    我有一个网格视图的数据显示一个问题,当我按下按钮“agregar”:数据显示网格视图

    enter image description here

    它显示了一个小广场,并显示任何数据。

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

    因为你在没有执行好,如果执行的查询将欢迎黑客通过注入 –

    回答

    0

    你必须与你的查询几个问题,一个快速的解决办法是在"'and"'and之间给人一种空间,通过这个你是通过注射开门黑客,所以更好的选择是使用的参数查询。几个建议:

    1. 您正在使用适配器收集查询结果到DataTable/DataSet中,这样你就不必在这之前
    2. 执行查询您使用的是单一的查询,所以它不是获取的值这里需要使用DataSet,然后从Dataset中获取所需的表,而不是直接使用Adapter将结果表提取到DataTable。
    3. 你可以利用使用块以及

    总之码结合网格的应该是这样的:

    DataTable dsDetalle=new DataTable("Data");   
    using (MySqlCommand commandSql = cn.CreateCommand()) 
    { 
        commandSql.CommandType = CommandType.Text; 
        commandSql.CommandText = "select * from detalle where [email protected] and [email protected]"; 
        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(); 
    
    +0

    感谢您的回答,但是当我复制代码出现下一个错误 sqlAdapter.Fill(dsDetalle,“data”); 错误\t \t 2的La MEJOR coincidencia德方法方法sobrecargado第 'System.Data.Common.DataAdapter.Fill(System.Data.DataTable,System.Data.IDataReader)' tiene algunos argumentos没有válidos\t C:\用户\ Hardfix1 \文档\ Visual Studio的 –

    +0

    感谢 但是当更新的代码 的IDE显示下一个错误 错误 USO德拉局部变量没有asignada“dsDetalle” 对不起我的无知,但即时通讯在programation –

    +0

    @WladimirArnaldoBriceoMirand新:谢谢对于尝试和回应,我已在帖子中进行了更新,请您尝试更改吗? –

    相关问题