【问题标题】:GridView does not bind with datasourceGridView 不与数据源绑定
【发布时间】:2014-09-01 12:00:33
【问题描述】:

以下代码有什么问题? GridView GridView1 根本不显示在页面上。

  public void display_range_mark()
    {
        int from = int.Parse(ddl_from_mark.SelectedValue.ToString());
        int to = int.Parse(ddl_to_mark.SelectedIndex.ToString());
        DataTable dt=Data.DoSomthing(string.Format("select ts.Name,ts.FamilyName,ts.Id_student,td.Name,tn.NomreAdad from tblStudent ts,tblDars td,tblNomre tn where tn.NomreAdad>='{0}' AND tn.NomreAdad<='{1}' AND ts.Id=tn.Id_student AND td.Id=tn.Id_dars",from,to));
        //DataTable data = Data.DoSomthing(string.Format("select t.Name,t.Id from tblStd t where t.DateSabt='{0}'", p.GetYear(DateTime.Now)));
        GridView1.DataSource = dt;
        GridView1.HeaderRow.Cells[0].Text = "نام";
        GridView1.HeaderRow.Cells[1].Text = "نام خانوادگی";
        GridView1.HeaderRow.Cells[2].Text = "شماره دانش آموزی";
        GridView1.HeaderRow.Cells[3].Text = "درس";
        GridView1.HeaderRow.Cells[4].Text = "نمره";
        GridView1.DataBind();



    }

我收到此错误:
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。 错误出现在这一行:

GridView1.HeaderRow.Cells[0].Text = "نام";

顺便说一下,Data.DoSomthing 的代码如下(它位于类 Database 中):

 SqlConnection sc = new SqlConnection(@"Data Source=.;Initial Catalog=School;Integrated Security=True");



public DataTable DoSomthing(string text)
    {
        sc.Open();
        DataTable data = new DataTable();
        try
        {
            SqlCommand command = new SqlCommand();
            command.Connection = sc;
            command.CommandType = CommandType.Text;
            command.CommandText = text;

            SqlDataAdapter sd = new SqlDataAdapter(command);
            sd.Fill(data);
            if (data.Rows.Count == 0)
                data = null;
        }
        catch (Exception ex)
        {
            sc.Close();
            return null;
        }
        finally
        {
            if (sc.State != ConnectionState.Closed)
            {
                sc.Close();
            }
        }
        return data;
    }

【问题讨论】:

  • 你确定在GridView1.DataSource = dt;之后直接调用GridView1.DataBind();吗?
  • 尝试在数据绑定后设置单元格文本。或者你应该改用GridView1.Columns[x].HeaderText=''
  • @TasosK。是的,我已经尝试过了。也没有用
  • 那么,错误信息呢。它说“空引用”,而我不认为它是空对象赋值。
  • @misaq,请检查我的回答。 Null 引用是因为没有 gridview 来查找标题。 Gridview 在填充之前为空。

标签: c# asp.net gridview


【解决方案1】:

连接数据库并取值的连接对象在哪里?

这样做的方法:

string query="select ts.Name,ts.FamilyName,ts.Id_student,td.Name,tn.NomreAdad from     tblStudent ts,tblDars     td,tblNomre tn where tn.NomreAdad>=@from AND tn.NomreAdad<=@to AND ts.Id=tn.Id_student AND td.Id=tn.Id_dars";

//Create Sqlconnection object
using(SqlConnection con = new SqlConnection(connectionstring))
{
//open the connection
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(query, con);
//To avoid sql injection using parameters
sda.Paramaters.AddWithValue("@from",from);
sda.Paramaters.AddWithValue("@to",to);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

【讨论】:

  • 一切正常!它没有填充数据表,因为数据库中根本没有匹配项。通过来回改变值,它终于奏效了。此外,正如在 cmets 中所说,data.DataBind() 似乎有必要在列标题分配之前
  • 太酷了。这就是为什么我要求你在调试模式下运行。并且还添加了我添加到命令中的参数以避免 SQL 注入。不知道为什么它被否决
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
相关资源
最近更新 更多