有一个公告表(notice),现在三层架构的DAL层需要实现一个查询最新更新的5条公告
代码如下:
        /// <summary>
        /// 查询5条公告信息
        /// </summary>
        /// <returns></returns>
        public IList<Notice> GetTop5Notice(string sql)
        {
            IList<Notice> Notice_5 = new List<Notice>();            using (SqlDataReader dr = DBHelper.GetReader(sql))
            {
                while (dr.Read())
                {
                    Notice notice = new Notice();
                    notice.Notice_ID = int.Parse(dr["Notice_ID"].ToString());
                    notice.Title = dr["Title"].ToString();
                    notice.Content = dr["Content"].ToString();
                    notice.App_user_ID = int.Parse(dr["App_user_ID"].ToString());
                    notice.Publish_date = DateTime.Now;
                    Notice_5.Add(notice);
                }
            }
            return Notice_5;
        }在notice.Notice_ID = int.Parse(dr["Notice_ID"].ToString());
和notice.App_user_ID = int.Parse(dr["App_user_ID"].ToString());
这两句都出现:用户代码未处理 IndexOutOfRangeException 异常
数据库里就只有这2个字段不能为空且为int型请问怎么办?

解决方案 »

  1.   

            /// <summary>
            /// 查询最新5条公告信息
             /// </summary>
            /// <returns></returns>
            public IList<Notice> GetTop5Notice()
            {
                string sql = "select top 5 title,content,publish_date from notice order by publish_date";
                return noti.GetTop5Notice(sql);
            }
      

  2.   

    异常表示dr["Notice_ID"]是null也就是dr里面是空的。
    你断点看下你sql的返回集或者看下dr是不是空的吧。或者单独执行下你的sql看看有没有结果
      

  3.   

    那你看看你的sql语句,你就没查出Notice_ID和App_user_ID这两个字段来啊