在asp.net中使用javascript不保存sql数据 [英] sql data is not saved using javascript in asp.net

查看:75
本文介绍了在asp.net中使用javascript不保存sql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用asp.net网页中的javascript在sql server中保存数据。但是数据没有保存在数据库中,它在catch语句中显示异常



错误描述:undefined



这是代码

i am trying to save data in sql server using javascript in asp.net webpage. But data is not saved in database its show me exception in catch statement

Error description: undefined

Here is the code

<script language="javascript" type="text/javascript">
     function UpdateStatus() {

         try {

             var ustatus = document.getElementById('<%=TextBox1.ClientID %>').value;

             var ConProv = new ActiveXObject("ADODB.Connection");
             var ConnString = "Server=abcd;Database =abcd;Uid=abcd;Pwd=abcd;MultipleActiveResultSets=True;" ;
             ConProv.Open(ConnString);
             var RecSet = new ActiveXObject("ADODB.Recordset");  


             var SqlSt = "insert into tbPostStatus(Ustatus,PostDate,PostFrom) Values('" + ustatus + "','','1')";
             //RecSet.ExecQuery(SqlSt, ConProv);
              ConProv.Execute(SqlSt, ConProv);

         }
         catch (err) {
             txt = "Error description: " + err.description;
             alert(txt);
         }

     }
  </script> 

推荐答案

你为什么用JavaScript做?这不是一个很好的方法,您将连接字符串放在一个可由任何人打开的文件中,这意味着您的Web应用程序可能容易受到SQL注入攻击。我的建议是,不是调试你现在遇到的这个问题,而是在服务器端执行数据库访问部分。
Why are you doing it in JavaScript? This is not a good approach you are putting your connection string in a file that can be opened by anybody which means that your web application can be prone to SQL injection attacks. My suggestion is that, instead of debugging this issue that you have right now, you do the database access part on the server side.


azad javascript是用于插入其使用的最糟糕的想法客户机器人,所以有100%的机会你已经入侵了网站所以这不是一个好主意........你会在你的网站上得到每天烦人的东西........问候:)
azad javascript is the worst idea to use for inserting its using client maching so there is 100% chances that you have hacked website so it is not good idea ........you will get every day annoying things in your website........regards :)


正如您在上面的帖子中建议的那样,您应该避免使用javascript访问数据。相反,您应该保持对服务器端代码的数据访问,并对其进行ajax调用。你可以用jQuery和ASP.Net页面方法做得很好。



你必须创建一个静态方法来处理你的数据访问操作,比如 -

As you are suggested in above posts you should avoid your data access using javascript. Instead you should keep data access to your server side code and make an ajax call to that. You can do that very well with jQuery and ASP.Net page methods.

You have to create a static method to handle your data access operation like -
[WebMethod]
public static void UpdateStatus(string userStatus) {
  // code to handle that
}



,并使用jQuery。 ajax()只是对它进行ajax调用。



你可以参考博客文章 -

http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-方法/ [ ^ ]



我希望这会对你有帮助。
谢谢。


and, with the use of jQuery.ajax() just make an ajax call to that.

You can refer to the blog post -
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/[^]

I hope this will help you.
Thanks.


这篇关于在asp.net中使用javascript不保存sql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆