How to Create Change Password form/page in - ASPMANTRA | Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server AND WCF snippets and tutorial

How to Create Change Password form/page in

Introduction: In this post I will explain how we can Change the Password in
Change Password


Users want to change their password. Users redirect to change password page and enter current password after that new password and confirm the password. Firstly here we check the users session on page load and after that check the current password on button click. If current password is correct than  user will be able to change the password successfully.

Add a new webform to project. Drag and drop the control from Toolbox and design the .aspx page as shown below:
<table align="center">
        <asp:Label ID="lblusername" runat="server" Text=""></asp:Label></td></tr>
        <tr><td><b>Change Password =></b></td></tr>
    <table align="center"><tr><td></td></tr>
    <tr><td>Current Password:</td><td>
        <asp:TextBox ID="txtcurrent" runat="server" TextMode="Password"></asp:TextBox></td></tr>
    <tr><td>New Password</td><td>
        <asp:TextBox ID="txtnew" runat="server" TextMode="Password"></asp:TextBox></td></tr>
    <tr><td>Confirm Password</td><td>
        <asp:TextBox ID="txtconfirm" runat="server" TextMode="Password"></asp:TextBox></td></tr>
        <tr><td>&nbsp;</td><td><asp:Button ID="btnchange" runat="server"
                Text="Update Password" onclick="btnchange_Click" /></td></tr>

Now on button click write the below given code(.aspx.cs):
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
    byte up;
    protected void Page_Load(object sender, EventArgs e)
        if (con.State == ConnectionState.Closed)
        if (Session["USERNAME"] != null)
            lblusername.Text = Session["USERNAME"].ToString();
    protected void btnchange_Click(object sender, EventArgs e)
            string query = "select * from USER_REGISTRATION";
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
                if (txtcurrent.Text == dr["PASSWORD"].ToString())
                    up = 1;

            if (up == 1)
                if (txtnew.Text == txtconfirm.Text)
                    string Update = "Update USER_REGISTRATION set PASSWORD = @PASSWORD where USERNAME ='" + Session["USERNAME"].ToString() + "'";
                    SqlCommand cmdupadte = new SqlCommand(Update, con);
                    cmdupadte.Parameters.AddWithValue("@PASSWORD", txtnew.Text);
                    Messagebox("Password Changed Successfully");
                    Messagebox("Password not Match");
                Messagebox("Enter Correct Current Password"); 
        catch (Exception ex)
    private void Clear()
        txtcurrent.Text = string.Empty;
        txtconfirm.Text = string.Empty;
        txtnew.Text = string.Empty;
    //Show Message
    private void Messagebox(string Message)
        Label lblMessageBox = new Label();

        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";

In VB (.aspx.vb)

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("Connection").ToString())
    Dim up As Byte
    Protected Sub btnchange_Click(sender As Object, e As System.EventArgs) Handles btnchange.Click
            Dim query As String = "select * from USER_REGISTRATION"
            Dim cmd As New SqlCommand(query, con)
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            While dr.Read()
                If txtcurrent.Text = dr("PASSWORD").ToString() Then
                    up = 1

                End If
            End While


            If up = 1 Then
                If txtnew.Text = txtconfirm.Text Then
                    Dim Update As String = "Update USER_REGISTRATION set PASSWORD = @PASSWORD where USERNAME ='" & Session("USERNAME").ToString() & "'"
                    Dim cmdupadte As New SqlCommand(Update, con)
                    cmdupadte.Parameters.AddWithValue("@PASSWORD", txtnew.Text)
                    Messagebox("Password Changed Successfully")

                    Messagebox("Password not Match")
                End If
                Messagebox("Enter Correct Current Password")
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Clear()
        txtcurrent.Text = String.Empty
        txtconfirm.Text = String.Empty
        txtnew.Text = String.Empty
    End Sub
    'Show Message
    Private Sub Messagebox(Message As String)
        Dim lblMessageBox As New Label()

        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If con.State = ConnectionState.Closed Then
        End If
        If Session("USERNAME") IsNot Nothing Then
            lblusername.Text = Session("USERNAME").ToString()
        End If

    End Sub

Now run the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.


thnk u so much workd

thanks for appreciation. stay tuned for more article.