change password form in asp net c#

In previous post we have learned how to create simple login form and create registration form in asp.net using sql database with c# language.

In this asp.net post, we will learn how to create a change password form for already registered user. For change password user first need to register with detail and get username and password. After register user has to login with his username and password. if username and password march then change password  form loaded to user screen and user can change his password by verifying current password.

change password form in asp.net c#
change password form in asp.net c#

First create a sql table for store user registration detail. we have here created sql table “UserMst” like below:

Here is the sql script for UserMst table:

CREATETABLE[dbo].[UserMst]
(

[ID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] [nvarchar](50) NULL,
[SurName] [nvarchar](50) NULL,
[City] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,

)

change password form in asp.net c#
change password form in asp.net c#

The html source code for change password form design.

<table align="center" class="style1" style="border: thin solid #008080">
            <tr>
                <td colspan="3" 
                    style="border-bottom: thin solid #008080; font-weight: 700; text-align: center;">
                    Change Password Form</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style4">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style6">
                    Enter Current Password :
                </td>
                <td class="style4">
                    <asp:TextBox ID="txtcurrentpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="txtcurrentpass" ErrorMessage="!!" ForeColor="Red" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style6">
                    Enter New Password :</td>
                <td class="style4">
                    <asp:TextBox ID="txtnewpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                        ControlToValidate="txtnewpass" ErrorMessage="!!" ForeColor="Red" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="style6">
                    Confirm Password : </td>
                <td class="style4">
                    <asp:TextBox ID="txtconfirmpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                        ControlToValidate="txtconfirmpass" ErrorMessage="!!" ForeColor="Red" 
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" 
                        ControlToCompare="txtnewpass" ControlToValidate="txtconfirmpass" 
                        ErrorMessage="password not same !!" ForeColor="Red"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style4">
                    <asp:Button ID="btnchangepass" runat="server" Text="Change Password" 
                        onclick="btnchangepass_Click" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style5">
                    &nbsp;</td>
                <td class="style2" colspan="2">
                    <asp:Label ID="lblmsg" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    &nbsp;</td>
            </tr>
        </table>

The design asp.net form layout is:

change password form in asp.net c#
change password form in asp.net c#

Here, we first check the use current password, if matches then user can make new password or  we can also say change password.

we user required field validator control for all three textboxes and user compare validator for compare new password and confirm password field in change password asp.net example.

The c# code for change password button.

protected void btnchangepass_Click(object sender, EventArgs e)
    {
        lblmsg.Text = "";
        SqlConnection SQLConn = new SqlConnection("Data Source=.\\SQLExpress; Initial Catalog=Blog; Integrated Security=True");
        SqlDataAdapter SQLAdapter = new SqlDataAdapter("select * from UserMst where password='"+txtcurrentpass.Text+"'", SQLConn);
        DataTable DT = new DataTable();
        SQLAdapter.Fill(DT);
        
        if (DT.Rows.Count == 0)
        {
            lblmsg.Text = "Invalid current password";
            lblmsg.ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            SQLAdapter = new SqlDataAdapter("update usermst set password='" + txtnewpass.Text + "' where email='" + Session["uname"].ToString() + "'", SQLConn);
            SQLAdapter.Fill(DT);
            lblmsg.Text = "Password changed successfully";
            lblmsg.ForeColor = System.Drawing.Color.Green;                       
        }
    }

In above change password asp.net example we use  session[“uname”] in update sql query for change password. When user login in to system with his username and password at that time if credential match then store username or email in session[“uname”].

5 thoughts on “change password form in asp net c#

  1. Thank you so much, your this code much help me
    m read to many sites StackOverflow, snippets but your codes simple & easy for understand its work thank you 🙂

  2. Thank you so much, your this code much help me
    m read to many sites StackOverflow, snippets but your codes simple & easy for understand its work thank you ?

Leave a Reply to Muhammad Imran Cancel reply

Your email address will not be published. Required fields are marked *