本文介绍了如何删除此错误“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的proj..same更新语句中得到此错误在其他页面中成功运行但在此页面中行为不端。谢谢提前回答。

i am getting this error in my proj..same update statement is running successfully in other pages but misbehaving in this page.Thank for answer in advance.

Syntax error in UPDATE statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at fyp_template.Changepassword.btnsubmitpassword_Click(Object sender, EventArgs e) in C:\Users\anam\Documents\Visual Studio 2015\Projects\FYP\fyp_template\fyp_template\Changepassword.aspx.cs:line 57"





我的代码如下

ne 57



My code is as follow
ne 57

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
namespace fyp_template
{
    public partial class Changepassword : System.Web.UI.Page
    {
        OleDbConnection mconn = new OleDbConnection();
        OleDbCommand cmd = new OleDbCommand();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblusername.Text = Session["Name"].ToString();
            if (Session["ID"] == null)
            { Label3.Text = "User"; }
            else
            {
                Label3.Text = Session["Name"].ToString();
            }
            try
            {
//                mconn.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = E:\abc.accdb";


                mconn.Open();
            }
            catch (Exception ex) { Response.Write("Error is " + ex); }
            finally { mconn.Close(); }

        }

        protected void btnsubmitpassword_Click(object sender, EventArgs e)
        {

            if (Session["ID"] == null)
            {
                string log1 = "Login first to change the password";
                ClientScript.RegisterStartupScript(this.GetType(), "Incorrect Password", "alert('" + log1 + "');", true);
                Response.Redirect("~/Login.aspx");

            }
            else
            {
                if (txtnewpassword.Text == "") { lblerrormessage.Text = "Enter New Password"; }
                else
                {
                    try
                    {
                        int id = Convert.ToInt32(Session["ID"]);
                        mconn.Open();
                        string squery = "update users set Password="+ txtnewpassword.Text + " where userId=" + id;
                        cmd = new OleDbCommand(squery, mconn);
                        cmd.ExecuteNonQuery();
                        mconn.Close();


                        string log1 = "Password updated successfully";
                        ClientScript.RegisterStartupScript(this.GetType(), " Password updated", "alert('" + log1 + "');", true);

                    }
                    catch (Exception ex) { Response.Write(ex); }
                    finally
                    {
                        mconn.Close();
                    }
                }
            }
        }



        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("My_Account.aspx");
        }

        protected void btnresetpassword_Click(object sender, EventArgs e)
        {
            txtnewpassword.Text = "";
        }
    }
}





我尝试了什么:



i已成功在其他页面上执行相同的查询。



What I have tried:

i have executed the same query on othr pages successfully.

推荐答案

string squery = "update users set Password="+ txtnewpassword.Text + " where userId=" + id;



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

[]

[]

[]

[]

[ ]

[]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


string squery = "update users set Password=? where userId= ?";
cmd = new OleDbCommand(squery, mconn);
cmd.Parameters.AddWithValue("@Password", txtnewpassword.Text);
cmd.Parameters.AddWithValue("@userId", id);



参考文献:

[1] []

OLE DB .NET提供程序不支持将参数传递给SQL语句或OleDbCommand调用的存储过程的命名参数CommandType设置为Text。在这种情况下,必须使用问号(?)占位符。



这篇关于如何删除此错误“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 18:13
查看更多