找了半天不知道哪里 
麻烦大家帮忙看下
数据层代码 public Product Selectproducttop2()
        {
            SqlDataReader reader = null;
            reader = SqlHelper.ExecuteReader(SqlHelper.Hz5hString, CommandType.Text, selecttop2);
            if (reader.HasRows)
            {
                product.Title = reader["title"].ToString();
                product.Imgurl = reader["imgurl"].ToString();
                product.Id = Convert.ToInt32(reader["id"]);
            }
            return product;
        }
逻辑层    public class Product
    {
        DAL.Product product = new DAL.Product();
        public int display(string title, string url)
        {            productAccess bllProduct = new productAccess(product);
            product = bllProduct.Selectproducttop2();            while (product != null)
            {
                title = product.Title;
                url = product.Imgurl;
                return product.Id;
            }
            return 0;
        }
    }
最后调用   if (!IsPostBack)
        {
            BLL.Product product = new Product();
            product.display(title1.InnerHtml, img1.Src);
            product.display(title2.InnerHtml, img2.Src);
        }

解决方案 »

  1.   

    大家帮忙看下,谢了
    昨天也是这个问题
    今天还没解决。
    太杯具了。
    “/hz5h”应用程序中的服务器错误。
    在没有任何数据时进行无效的读取尝试。
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。源错误:行 12:         {
    行 13:             BLL.Product product = new Product();
    行 14:             product.display(title1.InnerHtml, img1.Src);
    行 15:             product.display(title2.InnerHtml, img2.Src);
    行 16:         }
      

  2.   

    display(string title, string url)
    这个方法是给两个参数赋值?
    没这样写过,学习下。
      

  3.   

    if (reader.HasRows)
                {
                    product.Title = reader["title"].ToString();
                    product.Imgurl = reader["imgurl"].ToString();
                    product.Id = Convert.ToInt32(reader["id"]);
                }
    =>if (reader.HasRows)
                {
                    reader.Read();
                    product.Title = reader["title"].ToString();
                    product.Imgurl = reader["imgurl"].ToString();
                    product.Id = Convert.ToInt32(reader["id"]);
                }
      

  4.   

    reader.Read(); 
      

  5.   

    恩,知道了
    那语句的最重需要再执行close方法吗?
    如果我返回的是datareader类型执行了close方法
    那前台调用的地方还能取到数据不?
      

  6.   

    product.Title = reader["title"].ToString(); 
                    product.Imgurl = reader["imgurl"].ToString(); 
                    product.Id = Convert.ToInt32(reader["id"]); -----------------------------------------------------你已经将数据读取出来了,product 这个实例对象中存储的就是你需要的
      

  7.   

                if (reader.HasRows)
    这个不用了吧
    直接while(reader.Read())
    {}