Introduction

The registered user needs to login with his/her login credentials (user name and password). After successful login a Change password link will be visible. Here by clicking the link a new page will appear where the user must enter the Current Password, New Password and Confirm Password and then click on the Update button to change his/her password respectively.

Table structure
password1.jpg
Now let's move to the coding part.

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Password_Change_in_asp.net._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Untitled Page</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:Label ID="Label1" runat="server" Text="Name" Font-Bold="True"
  11. Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
  12. <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox><br />
  13. <asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True"
  14. Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>
  15. <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600"
  16. TextMode="Password" Width="100px"></asp:TextBox><br />
  17. <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True"
  18. BackColor="#CCFF99" onclick="btn_login_Click" /><br />
  19. <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label>
  20. </div>
  21. </form>
  22. </body>
  23. </html>

Default.aspx.cs

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14. namespace Password_Change_in_asp.net
  15. {
  16. public partial class _Default : System.Web.UI.Page
  17. {
  18. string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  19. string str = null;
  20. SqlCommand com;
  21. protected void btn_login_Click(object sender, EventArgs e)
  22. {
  23. object obj = null;
  24. SqlConnection con = new SqlConnection(strConnString);
  25. con.Open();
  26. Session["UserName"] = TextBox_user_name.Text;
  27. str = "select count(*) from login where UserName=@UserName and Password =@Password";
  28. com = new SqlCommand(str, con);
  29. com.CommandType = CommandType.Text;
  30. com.Parameters.AddWithValue("@UserName", Session["UserName"]);
  31. com.Parameters.AddWithValue("@Password", TextBox_password.Text);
  32. obj = com.ExecuteScalar();
  33. if ((int)(obj) != 0)
  34. {
  35. Response.Redirect("Welcome.aspx");
  36. }
  37. else
  38. {
  39. lb1.Text = "Invalid Username and Password";
  40. }
  41. con.Close();
  42. }
  43. }
  44. }
Welcome.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="Password_Change_in_asp.net.Welcome" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5. <title>Untitled Page</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />
  11. </div>
  12. <asp:LinkButton ID="lnk_changepassword" runat="server"
  13. onclick="lnk_changepassword_Click">Change Password</asp:LinkButton>
  14. </form>
  15. </body>
  16. </html>
Welcome.aspx.cs
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. namespace Password_Change_in_asp.net
  14. {
  15. public partial class Welcome : System.Web.UI.Page
  16. {
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. lb1.Text = "WELLCOME :: " + Session["UserName"];
  20. }
  21. protected void lnk_changepassword_Click(object sender, EventArgs e)
  22. {
  23. Response.Redirect("Changepassword.aspx");
  24. }
  25. }
  26. }
Changepassword.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Changepassword.aspx.cs"
  2. Inherits="Password_Change_in_asp.net.Changepassword" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. <head runat="server">
  6. <title>Untitled Page</title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:Label ID="Label1" runat="server" Text="Current password" Width="120px"
  12. Font-Bold="True" ForeColor="#996633"></asp:Label>
  13. <asp:TextBox ID="txt_cpassword" runat="server" TextMode="Password"></asp:TextBox>
  14. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  15. ControlToValidate="txt_cpassword"
  16. ErrorMessage="Please enter Current password"></asp:RequiredFieldValidator>
  17. <br />
  18. <asp:Label ID="Label2" runat="server" Text="New password" Width="120px"
  19. Font-Bold="True" ForeColor="#996633"></asp:Label>
  20. <asp:TextBox ID="txt_npassword" runat="server" TextMode="Password"></asp:TextBox>
  21. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
  22. ControlToValidate="txt_npassword" ErrorMessage="Please enter New password"></asp:RequiredFieldValidator>
  23. <br />
  24. <asp:Label ID="Label3" runat="server" Text="Confirm password" Width="120px"
  25. Font-Bold="True" ForeColor="#996633"></asp:Label>
  26. <asp:TextBox ID="txt_ccpassword" runat="server" TextMode="Password"></asp:TextBox>
  27. <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
  28. ControlToValidate="txt_ccpassword"
  29. ErrorMessage="Please enter Confirm password"></asp:RequiredFieldValidator>
  30. <asp:CompareValidator ID="CompareValidator1" runat="server"
  31. ControlToCompare="txt_npassword" ControlToValidate="txt_ccpassword"
  32. ErrorMessage="Password Mismatch"></asp:CompareValidator>
  33. </div>
  34. <asp:Button ID="btn_update" runat="server" Font-Bold="True" BackColor="#CCFF99" onclick="btn_update_Click" Text="Update" />
  35. <asp:Label ID="lbl_msg" Font-Bold="True" BackColor="#FFFF66" ForeColor="#FF3300" runat="server" Text=""></asp:Label><br />
  36. <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Login</asp:HyperLink>
  37. </form>
  38. </body>
  39. </html>
Changepassword.aspx.cs

  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14. namespace Password_Change_in_asp.net
  15. {
  16. public partial class Changepassword : System.Web.UI.Page
  17. {
  18. string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  19. string str = null;
  20. SqlCommand com;
  21. byte up;
  22. protected void btn_update_Click(object sender, EventArgs e)
  23. {
  24. SqlConnection con = new SqlConnection(strConnString);
  25. con.Open();
  26. str = "select * from login ";
  27. com = new SqlCommand(str, con);
  28. SqlDataReader reader = com.ExecuteReader();
  29. while (reader.Read())
  30. {
  31. if (txt_cpassword.Text == reader["Password"].ToString())
  32. {
  33. up = 1;
  34. }
  35. }
  36. reader.Close();
  37. con.Close();
  38. if (up == 1)
  39. {
  40. con.Open();
  41. str = "update login set Password=@Password where UserName='" + Session["UserName"].ToString()+ "'";
  42. com = new SqlCommand(str, con);
  43. com.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 50));
  44. com.Parameters["@Password"].Value = txt_npassword.Text;
  45. com.ExecuteNonQuery();
  46. con.Close();
  47. lbl_msg.Text = "Password changed Successfully";
  48. }
  49. else
  50. {
  51. lbl_msg.Text = "Please enter correct Current password";
  52. }
  53. }
  54. }
  55. }
Output

Providing credentials (user name and password) for login:

password2.jpg

After successful login:

password3.jpg

Changing the current password by providing a new password:

password4.jpg

Password changed successfully:

password5.jpg
Conclusion

So here we learned how to change a password in ASP.Net.