有时回报错: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。
奇怪了,是有时候。
 public static IList<AModel> Get_ASearchBy_date_th(String StartTime, String EndTime, String name)
        {            string sql = "select * from A where ((A_usern='" + name + "') or( A_td='" + name + "')) ";
            string sort = "order by A_dd desc";
            if (StartTime != "" && EndTime != "" && StartTime != null && EndTime != null)
                sql += " and (A_date between '" + StartTime + "' and '" + EndTime + "')";
            //if (th != "" && th != null)
            //    sql += " and A_th like '%" + th + "%'";
            return Get_AallBySql(sql+sort);
        }
        public static IList<AModel> Get_AallBySql(string safeSQL)
        {
            List<AModel> list =new List<AModel>();
            try
            {
                SqlDataReader rd=DBHelper2.GetReader(safeSQL);
                if(rd.HasRows)
                {
                    while(rd.Read())
                    {
                        AModel Amodel =new AModel();
                        Amodel.Id = Int32.Parse(rd["id"].ToString());
                        Amodel.Number = rd["number"].ToString();                        Amodel.A_date = rd["A_date"].ToString();
                        Amodel.A_th = rd["A_th"].ToString();
                        Amodel.A_why = rd["A_why"].ToString();
                        Amodel.A_sl = rd["A_sl"].ToString();
                        Amodel.A_mon = rd["A_mon"].ToString();
                        Amodel.A_qia = rd["A_qia"].ToString();
                        Amodel.A_tel = rd["A_tel"].ToString();
                        Amodel.A_it = rd["A_it"].ToString();
                        Amodel.A_unum = rd["A_unum"].ToString();
                        Amodel.A_usern = rd["A_usern"].ToString();
                        Amodel.A_usere = rd["A_usere"].ToString();
                        Amodel.A_gs = rd["A_gs"].ToString();
                        Amodel.A_dq = rd["A_dq"].ToString();
                        Amodel.A_bm = rd["A_bm"].ToString();
                        Amodel.A_zw = rd["A_zw"].ToString();
                        Amodel.A_td = rd["A_td"].ToString();
                        Amodel.A_dd = DateTime.Parse(rd["A_dd"].ToString());
                        Amodel.A_mail = rd["A_mail"].ToString();
                        list.Add(Amodel);
                    }
                }
                rd.Close();
                return list;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }namespace WEB
{
    public partial class A_list : System.Web.UI.Page
    {
        string loginid ;
        string name;
        string startime;
        string endtime;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loginid"] == null)
            {
                Response.Redirect("login_ask.aspx");
                Response.End();
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    loginid = Session["loginid"].ToString();//登录账号邮件姓名
                    this.txt_rq1.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                    this.txt_rq2.Text = DateTime.Now.ToString("yyyy-MM-dd");
                    BinData();
                }
            }
        }
        //绑定griview数据
        public void BinData()
        {
            startime = this.txt_rq1.Text.Trim();
            endtime = this.txt_rq2.Text.Trim();
            name = Session["ryxm"].ToString();//登录者中文名
            IList<AModel> list = ASQL.Get_ASearchBy_date_th(startime, endtime, name);
            GridView1.DataSource = list;            GridView1.DataKeyNames = new string[] { "number" };
            GridView1.DataBind();
            this.ddlCurrentPage.Items.Clear();
            for (int i = 1; i <= this.GridView1.PageCount; i++)
            {
                this.ddlCurrentPage.Items.Add(i.ToString());
            }
            if (this.ddlCurrentPage.SelectedIndex ==-1)
            {
                Response.Write("<script>alert('没有数据!');</script>");
            }
            else
            {
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
            
        }        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string nid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
            string xm = Session["ryxm"].ToString();
            QhmxModel qhmxmodel = new QhmxModel();
            qhmxmodel.djh = nid;
            qhmxmodel.djry = xm;
            if (!QhmxSQL.checkdel2(ref qhmxmodel))
            {
                ABLL Abll = new ABLL();
                Abll.Deleted_A(nid);
                BinData();
            }
            else
            {
                Response.Write("<script>alert('对不起,您的单子已审核,不能删除');</script>");
            }
        }        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //其实就是给GridView加上鼠标划过的JS
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F3F7FF'");
                //e.Row.Attributes.Add("onclick", "c=this.style.backgroundColor;this.style.backgroundColor='#EBFBBA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                e.Row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.open('A_Update.aspx?number={0}',_blank,'height=200;width=400;status=no;toolbar=no;menubar=no;location=no');", GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
                e.Row.Attributes["title"] = "双击打开详细页面";
                Button btn = e.Row.FindControl("btxian") as Button;
                if (btn != null)
                {
                    e.Row.Attributes.Add("onclick", "document.getElementById('" + btn.ClientID + "').click();");
                }
                string nid = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                if ((e.Row.Cells[5].Text).Length > 30)
                {
                    e.Row.Cells[5].Text = (e.Row.Cells[5].Text).Substring(0, 30) + "...";
                }
                if ((e.Row.Cells[6].Text).Length > 30)
                {
                    e.Row.Cells[6].Text = (e.Row.Cells[6].Text).Substring(0, 30) + "...";
                }
                this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.GridView1.PageIndex + 1, this.GridView1.PageCount);            }
        }        protected void Button1_Click(object sender, EventArgs e)
        {
            BinData();
        }        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("A_add.aspx");
        }
        protected void Btn_all_Click(object sender, EventArgs e)
        {
            this.txt_rq1.Text = "";
            this.txt_rq2.Text = "";
            BinData();
        }
        #region 翻页
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            BinData();
        }
        protected void lnkbtnFrist_Click(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = 0;
            BinData();
        }
        protected void lnkbtnPre_Click(object sender, EventArgs e)
        {
            if (this.GridView1.PageIndex > 0)
            {
                this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
                BinData();
            }
        }
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            if (this.GridView1.PageIndex < this.GridView1.PageCount)
            {
                this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
                BinData();
            }
        }
        protected void lnkbtnLast_Click(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.GridView1.PageCount;
            BinData();
        }

解决方案 »

  1.   

    代码写的还行,就是都到一块怎么那么垃圾呢?
    1。不要用gridview
    2。不要迷恋3层
      

  2.   

    本机运行,没有报错,当放到网络上,测试时,就会报
    [InvalidOperationException: 已有打开的与此命令相关联的 DataReader,必须首先将它关闭。]
      

  3.   

    至于为什么没关,是你读数据时用的DataReader没有关闭!就这么简单
      

  4.   

    using(SqlDataReader rd=DBHelper2.GetReader(safeSQL))
    {}
      

  5.   

    http://www.cnblogs.com/liaojunbo/archive/2008/04/26/1172348.html
      

  6.   

    using(SqlDataReader rd=DBHelper2.GetReader(safeSQL))
    {}