日期:2014-05-18 浏览次数:20512 次
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; } } }
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); } }
------解决方案--------------------
没有满足
if (lin == "")
{
obj = dt.Tables[0].Clone();
}
条件时候
obj为null
在后台一调用就出错,提示:当前上下文中不存在名称“obj”
------解决方案--------------------
if (lin == "")
{
obj = dt.Tables[0].Clone();
}
else if(obj==null) obj = new DataTable();