Thursday 20 June 2013

How to create Change Password in asp.net with connected mode

23:28    No comments
How to create change password in asp.net with connected mode


// execute query in sql server for create database and Registration table

create database test
use test
create table Registration (Name nvarchar(50),Gender nvarchar(50),Qulification nvarchar(50),EmailId nvarchar(50) primary key,Password nvarchar(50),CurrentCity nvarchar(50),SQues nvarchar(max),SAns nvarchar(50))

// THIS  IS  SOURCE CODE …..FOR ChangePassword.aspx………………………

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="ChangePassword" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table style="width: 100%">
            <tr>
                <td colspan="2">
                    <marquee behavior="alternate"<asp:Label ID="LChange" runat="server" Font-Bold="True" Font-Italic="True" ForeColor="Blue" Text="Change Password by Student"></asp:Label></marquee>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Old Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtoldpassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                        ControlToValidate="txtoldpassword" Display="Dynamic"
                        ErrorMessage="Please enter Old Password" ForeColor="Red" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="New Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtnewpassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                        ControlToValidate="txtnewpassword" Display="Dynamic"
                        ErrorMessage="Please enter New Password" ForeColor="Red" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="server"
                        ControlToCompare="txtcnewpassword" ControlToValidate="txtnewpassword"
                        ErrorMessage="New Password and Confirm New Password is  not match"
                        ForeColor="Red" SetFocusOnError="True">*</asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Confirm New Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtcnewpassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                        ControlToValidate="txtcnewpassword" Display="Dynamic"
                        ErrorMessage="Please enter Confirm  Password" ForeColor="Red"
                        SetFocusOnError="True">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="BtnChangePass" runat="server" onclick="BtnChangePass_Click"
                        Text="Change Password" />
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Label ID="lblmessage" runat="server"  Text="Label"
                        Visible="False"></asp:Label>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

// this code for  ChangePassword.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;

public partial class ChangePassword : System.Web.UI.Page
{
    string emailid;
    protected void Page_Load(object sender, EventArgs e)
    {
        //this code for get the value emailid from session when user login.......
        emailid = Session["EmailId"].ToString();

    }
    SqlConnection  con;
    SqlCommand cmd,cmd1;
    SqlDataReader dr;
    protected void BtnChangePass_Click(object sender, EventArgs e)
    {
        try
        {
            con = new SqlConnection("Data Source=KUSH-PC\\KUSH;Initial Catalog=test;Integrated Security=True");
            cmd = new SqlCommand("select EmailId,Password  from Registration where EmailId=@EmailId ", con);
            cmd.Parameters.AddWithValue("@EmailId", emailid);
            con.Open();
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                if (txtoldpassword.Text == "" + dr["Password"])
                {
                    dr.Close();
                    cmd1 = new SqlCommand("update Registration set Password= '" + txtnewpassword.Text + "'  where EmailId='" + emailid + "'", con);

                    cmd1.ExecuteNonQuery();
                    lblmessage.Visible = true;
                    lblmessage.ForeColor = Color.Green;
                    lblmessage.Text = "Password is Changed";
                }
                else
                {
                    lblmessage.Visible = true;
                    lblmessage.ForeColor = Color.Red;
                    lblmessage.Text = "Old Password is not Correct Please try again";
                }

            }
            con.Close();

        }
        catch (Exception ex)
        {
            lblmessage.Visible = true;
            lblmessage.ForeColor = Color.Red;
            lblmessage.Text = ex.Message;
        }

    }
}

0 comments:

Post a Comment