当前位置:编程学习 > C#/ASP.NET >>

老是报索引超出范围!找大侠帮忙

List<cai> list = new List<cai>();
            string a = "";
            cai_BLL.ChooseFacotry("cai");
            HttpCookie cookie = new HttpCookie("tjlxunshopcart");
            cookie = Request.Cookies["tjlxunshopcart"];
            HttpCookie cookierows = new HttpCookie("tjlxunshopcartrows");
            cookierows = Request.Cookies["tjlxunshopcartrows"];
            if (Request.Cookies["tjlxunshopcart"] != null)
            {
                if (cookie.Value != "")
                {
                    string[] zx = cookie.Value.Split(',');
                    string[] zx1 = cookierows.Value.Split(',');
                    if (zx.Length == zx1.Length)
                    {
                        try
                        {
                            for (int i = 0; i < zx.Length; i++)
                            {
                                if (zx[i].ToString() == "" || zx1[i].ToString() == "")
                                {

                                }
                                else
                                {
                                    list = cai_BLL.findbyzxone(int.Parse(Public.Decrypt(zx[i].ToString())), 1);
                                    if (list.Count != 0)
                                    {
                                        a += "<li id='" + jmd5(list[0].Id.ToString()) + "zxli' style='background-color:#fff6ed;'><div class='ngwcimg'><img src='../images/" + list[0].Caiimg.ToString() + "' height='50px' width='50px' /></div><div class='ngwcname'>" + list[0].Cainame.ToString() + "</div><div class='ngwcxh'>" + list[0].Kouwei.ToString().Replace("|", "  ") + "</div><div id='n" + jmd5(list[0].Id.ToString()) + "' class='ngwcmonwy'>" + list[0].Caimoney.ToString() + "</div><div class='ngwcrow'><img onclick=jian('" + jmd5(list[0].Id.ToString()) + "'); src='../img/-.jpg'/>  <span><input id='" + jmd5(list[0].Id.ToString()) + "' onkeyup=money('" + jmd5(list[0].Id.ToString()) + "') onkeypress='if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;' name='" + jmd5(list[0].Id.ToString()) + "' style='width: 33px' type='text' value='" + zx1[i] + "' /></span>  <img onclick=jia('" + jmd5(list[0].Id.ToString()) + "'); src='../img/+.jpg' /></div><div id='zn" + jmd5(list[0].Id.ToString()) + "' class='ngwcmonwy'>" + int.Parse(zx1[i]) * list[0].Caimoney + "元</div><div class='ngwcdel'><a onclick=delli('" + jmd5(list[0].Id.ToString()) + "','" + i + 1 + "') href='javascript:void(0);'>删除</a></div></li>";
                                        money += float.Parse(zx1[i]) * float.Parse(list[0].Caimoney.ToString());
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            qcgwc("tjlxunshopcart");
                            qcgwc("tjlxunshopcartrows");
                            Response.Write(Public.msg("亲,您的购物车中含有异常信息请您重新下单,", "index.aspx"));
                        }
                    }
                    else
                    {
                        qcgwc("tjlxunshopcart");
                        qcgwc("tjlxunshopcartrows");
                        Response.Write(Public.msg("亲,您的购物车中含有异常信息请您重新下单,", "index.aspx"));
                    }
                }
            }


 /// <summary>
        /// 查询查询单商品
        /// </summary>
        /// <returns></returns>
        public static List<cai> findbyzxone(int id, int show)
        {
            string sql = "select cai.id,cainame,kouwei,caiimg,caimoney from cai inner join qyvip on qyvipid=qyvip.id where cai.id=" + id + " and show=" + show;
            return Service.findbynall(sql);
        }
 /// <summary>
        /// 查询全部商品
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public List<cai> findbyzyall(string sql)
        {
            List<cai> list = new List<cai>();
            using (SqlConnection con = new SqlConnection(DB.conn))
            {
                using (SqlDataReader dr = DB.findDataReader(sql))
                {
                    while (dr.Read())
                    {
                        cai zhaoxu = new cai();
                        zhaoxu.Id = dr.GetInt32(0);
                        zhaoxu.Qyvipid = dr.GetInt32(1);
                        zhaoxu.Cainame = dr.GetString(2);
                        zhaoxu.Xiangou = dr.GetInt32(3);
                        zhaoxu.Kouwei = dr.GetString(4);
                        zhaoxu.Caiimg = dr.GetString(5);
                        zhaoxu.Caimoney = dr.GetDouble(6);
                        zhaoxu.Zaocan = dr.GetInt32(7);
                        zhaoxu.Adddate = dr.GetDateTime(8);
                        zhaoxu.Qydian = dr.GetString(9);
                        list.Add(zhaoxu);
                    }
                    dr.Close();
                    dr.Dispose();
                    con.Close();
                    con.Dispose();
                }
            }
            return list;
        }

 /// <summary>
        /// 查询DataReader
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>返回DataReader</returns>
        public SqlDataReader findDataReader(string sql)
        {
            SqlConnection con = new SqlConnection(conn);
            con.Open();
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
        }



有时候正常有时候报错。真的很头疼。搞了一个星期没弄好。找大侠帮个忙 --------------------编程问答-------------------- 在线等待!跪求大侠帮忙 --------------------编程问答-------------------- 这没人能帮你,都说了是索引超范围,那就检查所有访问索引的地方吧 --------------------编程问答-------------------- 本地调试一点也没问题。就是网站访问高峰期时有一少部分用户会报这个错误 --------------------编程问答--------------------

索引超出范围 自己看循环的地方吧,这玩意儿确实没人能帮你
你这代码看着也头疼 --------------------编程问答-------------------- 帅哥,你拼得不累啊这么搞,我看着就头晕啊。你只查询了5个字段,却要读取10(0~9)个字段,索引不超出才怪咧 --------------------编程问答-------------------- --------------------编程问答-------------------- SQL查询语句用参数啊。
你的这个代码用代码块贴出来,直接这样上文本,谁看着都会累的。
看问题的情况,貌似楼主要自己解决了。 --------------------编程问答-------------------- 索引超出范围: list[i]这里超出了。你再详细看看哦。
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,