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> </td>
<td>
<asp:Button ID="btnchangepass" runat="server" Text="Change Password"
OnClick="btnchangepass_Click" />
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2">
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3"> </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;
}
}
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article