Introduction
In this article we will show you how to create Change Password using Windows Application in Visual Studio using C#.
Prerequisites
Visual Studio 2010/2012/2013/15/17, SQL Server 2005/08/2012
Project used version
VS2017, SQL SERVER 2012
Step 1: Create new Stored Procedure "spChangePassword" and Paste the paste the below code.
USE [DBLoginSystem]
GO
/****** Object: StoredProcedure [dbo].[spChangePassword] Script Date: 17-12-2018 5.11.06 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spChangePassword](
@Email varchar(50),
@CurrentPassword varchar(50),
@NewPassword varchar(50),
@ConfNewPassword varchar(50),
@Result varchar(max) output
)
AS
BEGIN
declare @count int
SET NOCOUNT ON;
-- if New password and Confirmed New Password is not matched then returns "New password and confirm password not matched !!"
-- else If Current Password is matched against Database then returns "Sucessfully Changed"
-- else return "Invalid Current Password"
if(@NewPassword = @ConfNewPassword)
begin
select @count = count(*) from Users where Email = @Email and Password = @CurrentPassword
if(@count > 0)
begin
update Users set Password = @NewPassword where Email = @Email;
select @Result = 'Your Password changed successfully!';
end
else
begin
select @Result = 'Your current password is invalid.';
end
end
else
begin
select @Result = 'New password and confirm password not matched.';
end
END
GO
Step 2: Drag and drop the stored procedure "spChangePassword" in the LINQ to SQL Classes as shown below.
Step 3: Add new window form "ChangePassword.cs"
Step 4: Goto Admin Home Form and Double click on Change Password Menu Item.
The paste code to navigate forms.
private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
{
frmChangePassword chgpas = new frmChangePassword();
chgpas.Show();
}
Step 5:Design form as shown below, Double click on Change Password Button and the paste the follwing code.
Add the following items as shown below.
Controls | Text | Name(Design) |
Group Box | Change Password | grbChangePassword |
Label | Current Password | lblCurrent Password |
Label | New Password | lblNew Password |
Label | Confirm New Password | lblConfirmNewPassword |
TextBox | txtChnagePassword | |
TextBox | txtNewPassword | |
TextBox | txtConfirmChangePassword | |
Button | Change Password | btnChnagePassword |
Complete Source Code
using System;
using System.Windows.Forms;
namespace AdvancedLoginSystem
{
public partial class frmChangePassword : Form
{
public string result;
public frmChangePassword()
{
InitializeComponent();
}
private void BtnChangePassword_Click(object sender, EventArgs e)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
db.spChangePassword(AdvancedLoginSystem.Properties.Settings.Default.Email, txtCurrentPassword.Text, txtNewPassword.Text, txtConfirmNewPassword.Text, ref result);
db.SubmitChanges();
MessageBox.Show(result);
}
}
}
}
Step 6: Run Program and login as a admin, then go to master option and click on Change Password.
Password not match when new password and confirm new password not match.