c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

How to change password in asp.net using c#?

| | ASP-NET , CSharp , SQL

In previous post, we have seen how to create a simple login form in asp.net c# using sql database with example.

In this article I will show you how to change password code in c# asp.net. For change password, first the user needs to register the details. When the registered user wants to change the password, he/she has to login into the account with username and password.

The user has to verify the current password, if it matches then enter a new password and click the change password button.Here, we used “compare validator”  for confirming new password and confirm password fields.

Design code for change password:

<form id="form1" runat="server">
        <table align="center" style="border: thin solid #008080">
            <tr>
                <td colspan="3">
                    <h2>Change Password Form</h2>
 
                </td>
            </tr>
            <br />
            <tr>
                <td>Enter Current Password :
                </td>
                <td>
                    <asp:TextBox TextMode="Password" ID="txtcurrentpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                        ControlToValidate="txtcurrentpass" ErrorMessage="!!" ForeColor="Red"
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>Enter New Password :</td>
                <td>
                    <asp:TextBox TextMode="Password" ID="txtnewpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                        ControlToValidate="txtnewpass" ErrorMessage="!!" ForeColor="Red"
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td>Confirm Password : </td>
                <td>
                    <asp:TextBox TextMode="Password" ID="txtconfirmpass" runat="server" Width="120px"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                        ControlToValidate="txtconfirmpass" ErrorMessage="!!" ForeColor="Red"
                        SetFocusOnError="True"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="CompareValidator1" runat="server"
                        ControlToCompare="txtnewpass" ControlToValidate="txtconfirmpass"
                        ErrorMessage="passwordnot same !!" ForeColor="Red"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <asp:Button ID="btnchangepass" runat="server" Text="Change Password"
                        OnClick="btnchangepass_Click" />
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td colspan="2">
                    <asp:Label ID="lblmsg" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="3">&nbsp;</td>
            </tr>
        </table>
    </form>

change password code in c#:

protected void btnchangepass_Click(object sender, EventArgs e)
        {
            lblmsg.Text = "";
            string constr = ConfigurationManager.ConnectionStrings["foo"].ToString();
            SqlConnection conn = new SqlConnection(constr);
            SqlDataAdapter da = new SqlDataAdapter("select * from UserLogin where password='" +txtcurrentpass.Text + "'", conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
 
            if (dt.Rows.Count == 0)
            {
                lblmsg.Text = "Invalid current password";
                lblmsg.ForeColor =System.Drawing.Color.Red;
            }
            else
            {
                da = new SqlDataAdapter("update UserLogin set password='" + txtnewpass.Text + "'where UserName='" + Session["uname"].ToString()+ "'", conn);
                da.Fill(dt);
                lblmsg.Text = "Password changed successfully";
                lblmsg.ForeColor = System.Drawing.Color.Green;
            }
        }