public class book : System.Web.UI.Page
{
    private string sql;
    public string csql
    {
        set
        {
          sql = value;
        }
    }
    private DataTable obj;
    public DataTable cobj
    {
        get
        {
            return obj;
        }
    }
    public void datatree(string ParentID, string lin)
    {
        string sqltxt = "select id,name,cname='" + lin + "'+name,fid FROM " + sql + " WHERE  fid=" + ParentID + " order by showPage asc";
        DataSet dt = new _DB().GetSqlDataSet(sqltxt);
        if (lin == "")
        {
            obj = dt.Tables[0].Clone();
        }
        if (dt.Tables[0].Rows.Count > 0)
        {            lin += "__";
            for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
            {
                obj.ImportRow(dt.Tables[0].Rows[i]);
                datatree(dt.Tables[0].Rows[i]["id"].ToString(), lin);                  
            }
        }
        else
        {
            return;
        }
    }}
在后台一调用就出错,提示:当前上下文中不存在名称“obj”是什么回事?

解决方案 »

  1.   

    obj 你只是声明了,没有 new 
      

  2.   


            if (dt.Tables[0].Rows.Count > 0)
            {
                if(obj==null)
                    obj = new DataTable();
                lin += "__";
                for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
                {
                    obj.ImportRow(dt.Tables[0].Rows[i]);
                    datatree(dt.Tables[0].Rows[i]["id"].ToString(), lin);                  
                }
            }
      

  3.   

    没有满足
            if (lin == "")
            {
                obj = dt.Tables[0].Clone();
            }
    条件时候
    obj为null
    在后台一调用就出错,提示:当前上下文中不存在名称“obj” 
      

  4.   

    if (lin == "")
            {
                obj = dt.Tables[0].Clone();
            }
    else if(obj==null) obj = new DataTable();