public class logic : System.Web.UI.Page
     {
     protected System.Web.UI.WebControls.DataGrid d_grid2;
     private datab dg_tmp = new datab();
 private DataView dv = new DataView();
 public void Page_Load(object sender,System.EventArgs e)
   {
   dv=dg_tmp.tb_grid();
   d_grid2.DataSource=dv;
   d_grid2.DataBind();
   }
     }public class datab
  {
     public DataView tb_grid()
 {
 string strcon="server=localhost;database=stu2;user id=sa;password=1111";
 SqlConnection conn= new SqlConnection(strcon);
 DataSet ds =new DataSet();
 SqlDataAdapter da =new SqlDataAdapter();
 da.SelectCommand.CommandText="sysRefresh";
 da.SelectCommand.CommandType=CommandType.StoredProcedure;
 da.SelectCommand.Connection.Open();
 da.SelectCommand.ExecuteNonQuery();
 da.Fill(ds,"ThisTab");
 da.SelectCommand.Connection.Close();
 if(ds!=null)
 return ds.Tables["ThisTab"].DefaultView;
         }
}浏览时,提示 
Object reference not set to an instance of an object.
Stack Trace: 
[NullReferenceException: Object reference not set to an instance of an object.]
   stu3.b.datab.tb_grid() +85
   stu3.a.logic.Page_Load(Object sender, EventArgs e) +16
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +731请能者帮小弟!谢谢!

解决方案 »

  1.   

    lz的方法tb_grid()
    是有问题的,改写成下面这样吧:
     public DataView tb_grid()
            {
                string strcon="server=localhost;database=stu2;user id=sa;password=1111";
                SqlConnection conn= new SqlConnection(strcon);
                SqlCommand cm = new SqlCommand("sysRefresh",conn);
                cm.CommandType=CommandType.StoredProcedure;
                SqlDataAdapter da =new SqlDataAdapter(cm);
                DataTable dt = new DataTable();
                conn.Open();
                da.Fill(dt);
                conn.Close();
                DataView dv =dt.DefaultView;
                return dv;
            }
      

  2.   

    class test()
    {
       .....
    }
    这是一个类,当创建一个此类的实例时,此动作叫做“实例化”,例如DataSet ds =new DataSet();就是创建了一个DataSet类的实例ds
    lz的问题:
     SqlDataAdapter da =new SqlDataAdapter(); 这只创建了一个SqlDataAdapter的实例,并不要理所当然地去访问它的任何属性,因为有可能仍然是个空的对象,SelectCommand就是,所以报错
      

  3.   

    Eddie005(♂) 暴赱 『零零伍』(︶︵︶) ( ) 信誉:123 
    用你的方法试过,出现错误:
    The SelectCommand property has not been initialized before calling 'Fill'.
      

  4.   

    public DataView tb_grid()
            {
                string strcon="server=localhost;database=stu2;user id=sa;password=1111";
                SqlConnection conn= new SqlConnection(strcon);
                SqlCommand cm = new SqlCommand("sysRefresh",conn);
                cm.CommandType=CommandType.StoredProcedure;
                conn.Open();
                SqlDataAdapter da =new SqlDataAdapter(cm);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                DataView dv =dt.DefaultView;
                return dv;
            }
    这样看看呢
      

  5.   

    又有一下错误提示:Control 'd_grid2__ctl7__ctl1' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.