【问题标题】:Gridview is not displaying expected results when databinding数据绑定时,Gridview 未显示预期结果
【发布时间】:2017-01-10 16:14:30
【问题描述】:

从存储在我的数据库中的数据中对我的 gridview 进行数据绑定时,我看到一个小方块出现,而不是预期的 gridview 结果。

我正在使用的代码:

 try
        {

               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();
        }
        catch (Exception ex)
        {

            lblerror.Text = ex.ToString();
        }

【问题讨论】:

  • 调试时你尝试过什么?例如,我要做的第一件事是检查数据表以确保按预期存储内容,如果没有按照之前的代码进行操作。
  • 感谢更正,是的,我检查了数据并且查询正常
  • 你能从aspx页面显示GridView代码吗?也许它在那里......
  • 感谢您的帮助,我发现了@the Muffin Man 的错误,原因是我在查询中遇到了问题,感谢您的帮助:D

标签: c# asp.net gridview


【解决方案1】:

您必须在每个参数的值中省略引号:

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

还要确保您正在打开 SQL 连接并将其与您的 SQL 命令绑定

【讨论】:

  • 感谢您的 aswner ,我删除了 qotes 但有同样的错误:/
猜你喜欢
  • 2022-08-14
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2011-10-02
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
相关资源
最近更新 更多