Thursday 25 June 2015

Change password code using asp.net with c#


Hi Guys,
 This blog we explain how to change current password using asp.net with c# .
 For do that first we fetch the current password and match it with newpassword . If current password new same with new password then it will change the password.
Changepassword.aspx file
<table class="auto-style1">
                    <tr>
                        <td class="auto-style5">
                            <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="CurrentPassWord"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCurrentPassWord" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style5">
                            <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="NewPassWord"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtNewPassWord" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style5">
                            <asp:Label ID="Label3" runat="server" Font-Bold="True" Text="ConfirmPassWord"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtConfirmPassWord" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style5">
                            &nbsp;</td>
                        <td>
                            <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/submit.png" OnClick="ImageButton1_Click1" Width="81px" />
                            <asp:ImageButton ID="ImageButton2" runat="server" Height="21px" ImageUrl="~/Images/Reset.png" OnClick="ImageButton2_Click1" Width="137px" />
                            </td>
                       
                    </tr>
                    <tr>
                        <td class="auto-style5">&nbsp;</td>
                        <td>
                            <asp:Label ID="lblMsg" runat="server" Text="Msg"></asp:Label>
                        </td>
                    </tr>
                </table>
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>
</asp:Content>

Changepassword.aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
public partial class ChangePassword : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["123"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {
     SqlCommand cmd = new SqlCommand("select count(PassWord) from userregister where PassWord ='"+txtNewPassWord.Text+"'",con);
        con.Open();
        //cmd = new SqlCommand(con);
        int myint = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        if(myint > 0)
        {
            lblMsg.ForeColor = Color.Red;
            lblMsg.Text = "Already Exists";
            return;
        }
        try
        {
            string up;
            up = "Update userregister set PassWord ='" + txtNewPassWord.Text + "' where PassWord ='" + txtCurrentPassWord.Text + "'";
            cmd = new SqlCommand(up, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            lblMsg.ForeColor = Color.Green;
            lblMsg.Text = "Successfully change your password";

        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
   
    protected void ImageButton2_Click1(object sender, ImageClickEventArgs e)
    {

    }
}

No comments:

Post a Comment