Click here to Skip to main content
16,018,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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"


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 = "";
        }
    }
}


What I have tried:

i have executed the same query on othr pages successfully.
Posted
Updated 22-Jun-18 9:55am
Comments
Member 13680033 27-Jun-18 13:26pm    
i am just taking input in textbox but it is in password mode
Richard Deeming 27-Jun-18 13:30pm    
Irrelevant. You're still storing the passwords in the database in plain text. If you connect to your database and run SELECT * FROM users, you'll see the password for all of your users.

Read the articles. Understand the problem. Then update your code so that it only stores a salted hash of the users' passwords.
Richard Deeming 26-Jun-18 14:08pm    
And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems built-in - for example, ASP.NET Identity[^]
Member 13680033 27-Jun-18 13:26pm    
i found some error in that authentication systems that why i built it

Password was a keyword.i changed it password to pw and now its working.Thanks for all who consider the issue and provided their solutions.
 
Share this answer
 
As ppolymorphe stated, the syntax is an invitation for SQL Injection, and you should review those articles. Wouldn't want to have to refer to you as "Little Bobby Tables" in the future...

While the OleDB class doesn't support named parameters[1], they do use the AddWithValue method which has named parameters. What you must do is add the parameters in the same order as they appear in the query
I prefer to do it this with as it is mirrors the syntax when working with AdoDB and SQL server.

The following lines should replace the corresponding lines in your Try block:
C#
string squery = "update users set Password=? where userId= ?";
cmd = new OleDbCommand(squery, mconn);
cmd.Parameters.AddWithValue("@Password", txtnewpassword.Text);
cmd.Parameters.AddWithValue("@userId", id);

References:
[1] OleDbCommand.Parameters Property[^]
The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used.
 
Share this answer
 
C#
string squery = "update users set Password="+ txtnewpassword.Text + " where userId=" + id;

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[^]
 
Share this answer
 
Comments
Member 13680033 22-Jun-18 15:09pm    
Then what should i do to avoid sql injection?
Member 13680033 22-Jun-18 15:10pm    
Thanks for the consideration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900