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

小弟我写了一个ajax方法,但是为什么后台第一遍执行完后又回到第一行重新执行到command.Fill(ds);就报错

2013-09-06 
我写了一个ajax方法,但是为什么后台第一遍执行完后又回到第一行重新执行到command.Fill(ds)就报错?后台 p

我写了一个ajax方法,但是为什么后台第一遍执行完后又回到第一行重新执行到command.Fill(ds);就报错?
后台
 public void ProcessRequest(HttpContext context)
        {
            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=CMSDB");
            //连接SQL数据库
            int action = Convert.ToInt32(context.Request.QueryString["action"]);

            if (action == 1)
            {
                DataSet ds = new DataSet();
                conn.Open();
                string articleId = context.Request.QueryString["article"];//后台ajax传人的文章Id
                string sql = string.Format("select * from cms_expression where article_id ={0}", articleId);
                SqlDataAdapter command = new SqlDataAdapter(sql, conn);
                command.Fill(ds););//第二遍执行到这里报错,正常情况应该是只执行一遍就把数据传到前台
                DataRow dr = ds.Tables[0].Rows[0];
                conn.Close();
                int a = Convert.ToInt32(dr["article_id"].ToString());
                int b = Convert.ToInt32(dr["glad"].ToString());
                int c = Convert.ToInt32(dr["moved"].ToString());

                LitJson.JsonData json = new LitJson.JsonData();
                context.Response.ContentType = "application/json";

                json["id"] = a;


                json["data"] = new JsonData();
                json["data"].SetJsonType(JsonType.Array);
                json["data"].Add(b);
                json["data"].Add(c);
                context.Response.Write(json.ToJson());
            }
        }
前台
$("body").load(
            $.ajax({
                type: "GET",
                url: "HandlerExpression.ashx",
                data: { article: 1, action: 1 },
                success: function (json) {
                    $.getJSON("HandlerExpression.ashx", function (json) {
                        $(".list_ul li .num").each(function (i) {
                            $(".list_ul li .num").eq(i).text(json["data"][i]);
                            var h = json["data"][i] * 55 / json["count"];
                            $(".list_ul li span").eq(i).height(h);
                            //alert(json["data"][i]);
                        });
                    });


                }
            })
            );
[解决办法]
肯定是由于其他原因(原因在你的其他前台代码里),你的ashx被请求了两次

 articleId 这个值,在第二次请求的时候,没有post上来,所以sql出错


[解决办法]
   url: "HandlerExpression.ashx",                data: { article: 1, action: 1 },
                success: function (json) {
                    $.getJSON("HandlerExpression.ashx", function (json)

热点排行