Tuesday 27 January 2015

How To Change Password in asp.net using C#



Hello Friends,
                         Now I am going to describe  how to Change Password in asp.net using C#  in  5 steps:
Step-1:
Create a login table and Insert  data  in this Table  like this:
 
Command of  sql  to  create above Table:
CREATE TABLE [dbo].[login] (
    [EmailId]  VARCHAR (50) NOT NULL,
    [Password] VARCHAR (50) NOT NULL
);

 
Step-2:
Design the Login Form Like This:


Write This code  in .aspx page   to design the above Form :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 37%;
            border: 1px solid #FF00FF;
        }
        .auto-style4 {
        }
        .auto-style5 {
            width: 90%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td colspan="2" style="font-size: medium; font-weight: bold; color: #FF0000">Change Password</td>
            </tr>
            <tr>
                <td class="auto-style4">Old Password</td>
                <td class="auto-style5">
                    <asp:TextBox ID="txtoldpsw" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtoldpsw" ErrorMessage="Please Enter Your Valid Password" ForeColor="#FF6666"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">New Password</td>
                <td class="auto-style5">
                    <asp:TextBox ID="txtnewpsw" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtnewpsw" ErrorMessage="Please Enter Your New Password" ForeColor="#FF3399"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style4">Confirm New Password</td>
                <td class="auto-style5">
                    <asp:TextBox ID="txtconfirmpsw" runat="server"></asp:TextBox>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtconfirmpsw" ErrorMessage="Password donot Match" ForeColor="#FF0066" ControlToCompare="txtnewpsw"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="savebtn" runat="server"  Text="Save Password" OnClick="savebtn_Click" />
                    <br />
                </td>
            </tr>
            <tr>
                <td class="auto-style4" colspan="2">
                    <asp:Label ID="lblmsg" runat="server" ForeColor="#FF3399"></asp:Label>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Step-3:

Write Your Connection String in Web.config File:

<connectionStrings>
<add name="conn" connectionString="Data Source=xyz;Initial Catalog=x;Integrated Security=True"/>
  </connectionStrings>

Step-4:
Code of .aspx.cs Page:

Import  Following Namespace  Top of the .aspx.cs Page:

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Write this code within public partial class:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

Step-5:

Doubleclick on Save Password Button and Write This Code:

string sql = "update login set Password='" + txtnewpsw.Text + "'where Password='" + txtoldpsw.Text + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
              adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                lblmsg.Text = "Your Password has been changed successfully ";
            }
            else
            {
                lblmsg.Text = "Your Password is missmatch... ";
            }






Thanks
Rabi

No comments:

Post a Comment