How to Create Change Password form/page in Asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

How to Create Change Password form/page in Asp.net

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

Description:

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">
    <tr><td></td><td></td><td>
        <asp:Label ID="lblusername" runat="server" Text=""></asp:Label></td></tr>
        <tr><td><b>Change Password =></b></td></tr>
    <tr><td>
    <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>
    </table></td></tr></table>


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)
            con.Open();
        if (Session["USERNAME"] != null)
        {
            lblusername.Text = Session["USERNAME"].ToString();
        }
    }
    protected void btnchange_Click(object sender, EventArgs e)
    {
        try
        {
            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;
                }
                  
            }
          
            cmd.Dispose();
            dr.Close();

            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);
                    cmdupadte.ExecuteNonQuery();
                    Clear();
                    Messagebox("Password Changed Successfully");
                }
                else
                {
                    Messagebox("Password not Match");
                                  
                }
            }
            else
            {
                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>";
        Page.Controls.Add(lblMessageBox);
    }

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
        Try
            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

            cmd.Dispose()
            dr.Close()

            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)
                    cmdupadte.ExecuteNonQuery()
                    Clear()
                    Messagebox("Password Changed Successfully")
                Else

                    Messagebox("Password not Match")
                End If
            Else
                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>"
        Page.Controls.Add(lblMessageBox)
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If con.State = ConnectionState.Closed Then
            con.Open()
        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.

Share this

Share on FacebookTweet on TwitterPlus on Google+

2 comments

thnk u so much .......it workd

thanks for appreciation. stay tuned for more article.


EmoticonEmoticon