我做一程序,不知道为什么,基中部门采用的是无限极分类。
但不知程序总是出错》
while (dr.Read())
{
ListItem lt = new ListItem();
if (dr["Depproot"].ToString() == "0")
{
lt.Text = dr["DeppName"].ToString();
lt.Value = dr["DeppID"].ToString();
Dplistssbm.Items.Add(lt);
}
else
{
ListItem plt = Dplistssbm.Items.FindByValue(dr["Depproot"].ToString());///Depproot
lt.Text = "|--" + plt.Text;
int strindex = plt.Text.LastIndexOf("|--");
if (strindex != -1)
{
lt.Text = lt.Text.Replace(plt.Text.Substring(strindex + 3), dr["DeppName"].ToString());
}
else
{
lt.Text = lt.Text.Replace(plt.Text, dr["DeppName"].ToString());
}
lt.Value = dr["DeppID"].ToString();
Dplistssbm.Items.Insert(Dplistssbm.Items.IndexOf(plt) + 1, lt);
}
}出错位置:未将对象引用设置到对象的实例。
ListItem plt = Dplistssbm.Items.FindByValue(dr["Depproot"].ToString());///
但不知程序总是出错》
while (dr.Read())
{
ListItem lt = new ListItem();
if (dr["Depproot"].ToString() == "0")
{
lt.Text = dr["DeppName"].ToString();
lt.Value = dr["DeppID"].ToString();
Dplistssbm.Items.Add(lt);
}
else
{
ListItem plt = Dplistssbm.Items.FindByValue(dr["Depproot"].ToString());///Depproot
lt.Text = "|--" + plt.Text;
int strindex = plt.Text.LastIndexOf("|--");
if (strindex != -1)
{
lt.Text = lt.Text.Replace(plt.Text.Substring(strindex + 3), dr["DeppName"].ToString());
}
else
{
lt.Text = lt.Text.Replace(plt.Text, dr["DeppName"].ToString());
}
lt.Value = dr["DeppID"].ToString();
Dplistssbm.Items.Insert(Dplistssbm.Items.IndexOf(plt) + 1, lt);
}
}出错位置:未将对象引用设置到对象的实例。
ListItem plt = Dplistssbm.Items.FindByValue(dr["Depproot"].ToString());///
if( plt != null){
//do...
}
如果没有find到 就是null 你用他的属性或者方法,就 “未将对象引用设置到对象的实例” 了。