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

研究3天了可还是没有弄明白?用ajax动态生成一个xml文件,然后将其数据保存到数据库中实现那种论坛的发帖的功能大家近来看看,近者有分!解决方法

2012-01-02 
研究3天了可还是没有弄明白?用ajax动态生成一个xml文件,然后将其数据保存到数据库中实现那种论坛的发帖的

研究3天了可还是没有弄明白?用ajax动态生成一个xml文件,然后将其数据保存到数据库中实现那种论坛的发帖的功能大家近来看看,近者有分!
1,使用XmlDocument类实现编译XML的功能。
2,将别写帖子的信息保存到数据库。

代码如下:
页面代码:
          <script   type= "text/javascript ">
          var   xmlhttp;
          function   SendMsg()
          {
              document.getElementById( "divlist ").innerText= "正在发帖......... ";
          //获取参数
              var   msgtitle=document.getElementById( "txttitle ").value;
              var   msgcontent=document.getElementById( "txtcontent ").value;
              var   categoryid=document.getElementById( "DropDownList1 ").value;
             
              //创建IE中的异步对象,没有考虑其他浏览器
              xmlhttp=new   ActiveXObject( "Microsoft.XMLHTTP ");
              //对象的状态事件关联
              xmlhttp.onreadystatechange=stateChange;
              //请求的服务器地址,同时传递参数
              xmlhttp.open( "POST ", "SendMsgServer.aspx?title= "+escape(msgtitle)+ "&content= "+escape(msgcontent)+ "&categoryid= "+categoryid,true);
              //发送请求
              xmlhttp.send(null);
        }
        //判断请求的状态
        function   stateChange()
        {
            //readystate=4表示请求已经完成
            if(xmlhttp.readystate==4)
            {
                  //status==200表示已经成功返回
                  if(xmlhttp.status==200)
                  {
                      //格式化输出的内容
                      document.getElementById( "divlist ").innerHTML=xmlhttp.responseText;
                  }
            }
        }
        </script>


        public   static   SqlParameter[]   GetCachedParameters(string   cacheKey)
        {
                SqlParameter[]   cachedParms   =   (SqlParameter[])parmCache[cacheKey];

                if   (cachedParms   ==   null)
                        return   null;

                //新建一个参数的克隆列表
                SqlParameter[]   clonedParms   =   new   SqlParameter[cachedParms.Length];


                //通过循环为克隆参数列表赋值
                for   (int   i   =   0,   j   =   cachedParms.Length;   i   <   j;   i++)
                        //使用clone方法复制参数列表中的参数
                        clonedParms[i]   =   (SqlParameter)((ICloneable)cachedParms[i]).Clone();

                return   clonedParms;
        }

执行代码:

  private   const   string   SQL_INSERT_BBSINFO   =   "INSERT   INTO   BBSInfo   VALUES   "   +
          "(@title,@filename,@posttime,@replycount,@lastreplytime,   @postuser,@categoryid) ";
        /////   <summary>
        /////   编写者:杜鹏
        /////   添加帖子信息到数据库的方法
        /////   </summary>
        /////   <param   name= "title "> </param>
        /////   <param   name= "user "> </param>
        /////   <param   name= "categoryid "> </param>
        public   void   AddMsg(string   title,   string   user,   int   categoryid)
        {
                //使用StringBuild连接字符串比使用“+”效率高很多
                StringBuilder   strSQL   =   new   StringBuilder();
                //获取缓存参数,如果没有,此方法会自动创建缓存列表
                SqlParameter[]   newsParms   =   GetParameters();
                //   依次给参数赋值
                newsParms[0].Value   =   title;
                //一个获取文件名的私有方法
                newsParms[1].Value   =   getFilename().ToString();
                xmlfilename   =   getFilename().ToString();
                //注意发布的日期取当日
                newsParms[2].Value   =   DateTime.Now;
                //默认添加的回复数是0
                newsParms[3].Value   =   0;
                newsParms[4].Value   =   DateTime.Now;
                newsParms[5].Value   =   user;
                newsParms[6].Value   =   categoryid;

                //获取数据库的连接字符串
                using   (SqlConnection   conn   =   new   SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
                {
                        strSQL.Append(SQL_INSERT_BBSINFO);



                        //执行添加的SqlCommand命令
                        SqlHelper.ExecuteNonQuery(conn,   CommandType.Text,   strSQL.ToString(),   newsParms);
                }
        }
        /////   <summary>
        /////   创建或获取缓存参数的私有方法
        /////   </summary>
        /////   <returns> </returns>
        private   static   SqlParameter[]   GetParameters()
        {
                //将SQL_INSERT_NEWSINFO做为哈希表缓存的键值
                SqlParameter[]   parms   =   SqlHelper.GetCachedParameters(SQL_INSERT_BBSINFO);

                //首先判断缓存是否已经存在
                if   (parms   ==   null)
                {
                        //缓存不存在的情况下,新建参数列表
                        parms   =   new   SqlParameter[]  
                                        {
                                                new   SqlParameter( "@title ",   SqlDbType.NVarChar,50),
                                                new   SqlParameter( "@filename ",   SqlDbType.NVarChar,100),
                                                new   SqlParameter( "@posttime ",   SqlDbType.DateTime),
                                                new   SqlParameter( "@replycount ",   SqlDbType.Int),
                                                new   SqlParameter( "@lastreplytime ",   SqlDbType.DateTime),
                                                new   SqlParameter( "@postuser ",   SqlDbType.NVarChar,   50),
                                                new   SqlParameter( "@categoryid ",   SqlDbType.Int)  
                                        };

                        //将新建的参数列表添加到哈希表中缓存起来


                        SqlHelper.CacheParameters(SQL_INSERT_BBSINFO,   parms);
                }
                //返回参数数组
                return   parms;
        }


可是现在执行到   :     SqlParameter[]   parms   =   SqlHelper.GetCachedParameters(SQL_INSERT_BBSINFO);这句话报错:sqalHelper的类型初始值设定项引发异常。

sqlhelper类的代码如下:
public   static   SqlParameter[]   GetCachedParameters(string   cacheKey)
        {
                SqlParameter[]   cachedParms   =   (SqlParameter[])parmCache[cacheKey];

                if   (cachedParms   ==   null)
                        return   null;

                //新建一个参数的克隆列表
                SqlParameter[]   clonedParms   =   new   SqlParameter[cachedParms.Length];

                //通过循环为克隆参数列表赋值
                for   (int   i   =   0,   j   =   cachedParms.Length;   i   <   j;   i++)
                        //使用clone方法复制参数列表中的参数
                        clonedParms[i]   =   (SqlParameter)((ICloneable)cachedParms[i]).Clone();

                return   clonedParms;
        }


大家帮我看看到底是哪里错了?

[解决办法]
你都用POST方法了为什么还要这么写呢?
//请求的服务器地址,同时传递参数
xmlhttp.open( "POST ", "SendMsgServer.aspx?title= "+escape(msgtitle)+ "&content= "+escape(msgcontent)+ "&categoryid= "+categoryid,true);
//发送请求
xmlhttp.send(null);
我觉得这么写更好些:
var querystring = "title= "+escape(msgtitle)+ "&content= "+escape(msgcontent)+ "&categoryid= "+categoryid + "&ts= " + new Date().getTime();
xmlhttp.open( "POST ", "SendMsgServer.aspx ",true);
//发送请求
xmlhttp.send(querystring);

取值的时候用Request.Form[ " "]取值

[解决办法]
SqlHelper类定义有问题,其不能被初始化(TypeInitializationException),这跟GetCachedParameters方法没有直接关系,只要你调用SqlHelper应该都会报这个异常。检查一下这个类的定义吧,看看有没有哪里抛这个异常
[解决办法]
可能的情况:
1 SqlHelper.CacheParameters(SQL_INSERT_BBSINFO, parms) 这个方法里可能有错 但你没贴哦
2 SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey] 这句可能有错
3 这句没错啊 不过看着不习惯
for (int i = 0, j = cachedParms.Length; i < j; i++)
怎么不用通用的写法?for (int i = 0; i < cachedParms.Length; i++)
4 用调试时的实习查看可以跟踪到内部查看 一般都能看到错误源

[解决办法]
近者~
复杂。
[解决办法]
mark

热点排行