Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:18:50+00:00 2026-05-16T11:18:50+00:00

I am trying to change the user password. I am not able to update

  • 0

I am trying to change the user password. I am not able to update the password :(. The message i am getting is password changed where as its not getting changed. .
My code is as follow.. Please if anyone can suggest where i am going wrong . I am just a beginner …

protected void Button1_Click(object sender, EventArgs e)
{
    DatabaseLayer data = new DatabaseLayer();

    string username = Session["Authenticate"].ToString();
    string password = TextBox1.Text;
    string newpass = TextBox2.Text;
    string confirm = TextBox3.Text;
    string flag = "";

    if (newpass.ToString() == confirm.ToString())
    {
        flag = data.passwordChange(username, password, newpass);
        Literal1.Text = flag.ToString();
    }
    else
    {
        Literal1.Text = "New Password does not match the Confirm Password ";
    }
}

The above click event must change my password, and the function passwordChange is as follows..

public string passwordChange(string username, string password, string newPasswd)
{
    string SQLQuery = "SELECT password FROM LoginAccount WHERE username = '" + username + "'";
    string SQLQuery1 = "UPDATE LoginAccount SET password = ' " + newPasswd + " ' WHERE username = ' " + username + "'";
    SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
    SqlCommand command1 = new SqlCommand(SQLQuery1, sqlConnection);

    sqlConnection.Open();
    string sqlPassword = "";
    SqlDataReader reader;

    try
    {
        reader = command.ExecuteReader();


        if (reader.Read())
        {
            if (!reader.IsDBNull(0))
            {
                sqlPassword = reader["password"].ToString();
            }
        }
        reader.Close();

        if (sqlPassword.ToString() == password.ToString())
        {
            try
            {
                int flag = 0;
                flag = command1.ExecuteNonQuery();

                if (flag > 0)
                {
                    sqlConnection.Close();
                    return "Password Changed Successfully";
                }
                else
                {
                    sqlConnection.Close();
                    return "User Password could not be changed";
                }
            }
            catch (Exception exr)
            {
                sqlConnection.Close();
                return "Password Could Not Be Changed Please Try Again";
            }
        }
        else
        {
            sqlConnection.Close();
            return "User Password does not Match";
        }
    }
    catch (Exception exr)
    {
        sqlConnection.Close();
        return "User's Password already exists";
    }
}

I had put a break point near

if(flag>0)

it still shows that executeNonquery aint returning the updated rows value and also in the Back end of SQL server, its not changing,
Please if anyone could correct me… Should i use other execute command or something?
I am doing this with VS 2008 and SQL server 2005..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:18:51+00:00Added an answer on May 16, 2026 at 11:18 am

    1: It’s your spacing between your single and double quotes: (Like: ' " + username + " ')
    2) You are begging for SQL Injection.

    Try this in your PasswordChange method:

    public string PasswordChange(string userName, string oldPass, string newPass)
    {
        using(SqlConnection sqlConnection = new SqlConnection(
            ConfigurationManager.ConnectionStrings["LoginDb"].ConnectionString))
       {
        string sqlToConfirmOldPass =
          "SELECT password FROM LoginAccount WHERE username = @userName";
        string sqlToUpdatePassword =
          "UPDATE LoginAccount SET password = @newPass WHERE username = @userName";
    
        SqlCommand confirmOldPass = new SqlCommand(sqlToConfirmOldPass, sqlConnection);
        confirmOldPass.Parameters.AddWithValue("@userName", userName);
    
        SqlCommand updatePassword = new SqlCommand(sqlToUpdatePassword, sqlConnection);
        updatePassword.Parameters.AddWithValue("@newPass", newPass);
        updatePassword.Parameters.AddWithValue("@userName", userName);
    
        [Rest of your code goes here]
       }
    }
    

    I also didn’t see where you set your SqlConnection, so I’ve added a line for that. You’ll need to modify it according to your needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a C program to change a user password using Linux
I'm trying to figure out how to set the User cannot change password attribute
I am trying to enable users to change their password. I keep getting the
I'm trying to implement a form for let the user change their password. Here
i am trying to change user agent in php.ini file as follows. user_agent=Mozilla/5.0 (iPhone
I'm trying to change some config file user settings values in my WPF application,
I'm trying to change view controllers based on a user's selection in a UIAlertView
I'm trying to figure out how to detect when the current user has changed
I´m trying to change a spring jsp example to use freemarker. I changed all
When a user is not logged I'm trying to enter areas of site for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.