首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为啥if段代码可以执行,当条件不成立时else段的代码无法执行

2012-09-04 
为什么if段代码可以执行,当条件不成立时else段的代码无法执行?protected void TreeView1_SelectedNodeChan

为什么if段代码可以执行,当条件不成立时else段的代码无法执行?
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
  {
  string s = TreeView1.SelectedNode.Text.ToString();
  string value = DropDownList_Service.SelectedValue;
  SqlServerDataBase sd = new SqlServerDataBase();
  string s1 = "select User_ID from [db].[dbo].[User] where User_Name='"+s+"'";
  DataSet ds1 = sd.Select(s1, sd.DBconn());
  if (ds1.Tables[0].Rows.Count != 0)
  {
  int id = (int)ds1.Tables[0].Rows[0]["User_ID"];
  String s2 = "select User_Name,a.User_ID from [db].[dbo].[User] as a,User_Service as b,Service where a.User_ID=b.User_ID and Leader_ID="
  + id+" and Service.Service_ID=b.Service_ID and Service_Name='"+value+"'";
  DataSet ds2 = sd.Select(s2, sd.DBconn());
  if (ds2.Tables.Count !=0)
  {
  foreach (DataRow row in ds2.Tables[0].Rows)
  {
  TreeNode n = new TreeNode(row["User_Name"].ToString(), row["User_ID"].ToString());
  //n.PopulateOnDemand = true;
  //n.SelectAction = TreeNodeSelectAction.Expand;
  TreeView1.SelectedNode.ChildNodes.Add(n);
  }
  }
  else
  {
  string s3 = "select Data_ID from [db].[dbo].[ data] as a,Service as b where User_ID="+id+" and a.Service_ID=b.Service_ID "
  +"and Service_Name='"+value+"'";
  DataSet ds3 = sd.Select(s3,sd.DBconn());
  if (ds3.Tables[0].Rows.Count != 0)
  {
  foreach (DataRow row1 in ds3.Tables[0].Rows)
  {
  TreeNode n = new TreeNode(row1["Data_ID"].ToString(), "1");
  //n.PopulateOnDemand = false;
  //n.SelectAction = TreeNodeSelectAction.Expand;
  TreeView1.SelectedNode.ChildNodes.Add(n);
  }
  }
  }
  }
   

  }

else对应的if段代码可以实现,当条件不成立时,else却不能执行?是哪里出问题了,求大神指导

[解决办法]
string s3 = "select Data_ID from [db].[dbo].[ data] as a,Service as b where User_ID="+id+" and a.Service_ID=b.Service_ID "
+"and Service_Name='"+value+"'";


response.write(s3); 然后到数据库里执行下

当然会打段点最好了
[解决办法]
我来帮你重新发过吧。

C# code
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)        {            string s = TreeView1.SelectedNode.Text.ToString();            string value = DropDownList_Service.SelectedValue;            SqlServerDataBase sd = new SqlServerDataBase();            string s1 = "select User_ID from [db].[dbo].[User] where User_Name='" + s + "'";            DataSet ds1 = sd.Select(s1, sd.DBconn());            if (ds1.Tables[0].Rows.Count != 0)            {                int id = (int)ds1.Tables[0].Rows[0]["User_ID"];                String s2 = "select User_Name,a.User_ID from [db].[dbo].[User] as a,User_Service as b,Service where a.User_ID=b.User_ID and Leader_ID="                + id + " and Service.Service_ID=b.Service_ID and Service_Name='" + value + "'";                DataSet ds2 = sd.Select(s2, sd.DBconn());                if (ds2.Tables.Count != 0)                {                    foreach (DataRow row in ds2.Tables[0].Rows)                    {                        TreeNode n = new TreeNode(row["User_Name"].ToString(), row["User_ID"].ToString());                        //n.PopulateOnDemand = true;                        //n.SelectAction = TreeNodeSelectAction.Expand;                        TreeView1.SelectedNode.ChildNodes.Add(n);                    }                }                else                {                    string s3 = "select Data_ID from [db].[dbo].[ data] as a,Service as b where User_ID=" + id + " and a.Service_ID=b.Service_ID "                    + "and Service_Name='" + value + "'";                    DataSet ds3 = sd.Select(s3, sd.DBconn());                    if (ds3.Tables[0].Rows.Count != 0)                    {                        foreach (DataRow row1 in ds3.Tables[0].Rows)                        {                            TreeNode n = new TreeNode(row1["Data_ID"].ToString(), "1");                            //n.PopulateOnDemand = false;                            //n.SelectAction = TreeNodeSelectAction.Expand;                            TreeView1.SelectedNode.ChildNodes.Add(n);                        }                    }                }            }        } 


[解决办法]
你的if条件句ds2.Tables.Count != 0 有问题,你用select语句从数据库查询数据,放到DataSet中,即使从数据库中查询出来时空的,也会生成一个DataSet,所以你用这个条件就永远不会走到else里面。你可以把条件设置为:ds2.Tables[0].Rows.Count != 0这样试试,这样应该没问题了。

热点排行