C# Database Tutorials In Urdu (Code) - How to create Login Screen


The code is shown below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DatabaseDemo2
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}

private void ExitApplicationButton_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void LoginButton_Click(object sender, EventArgs e)
{
if (IsValidated())
{
try
{
bool isUserNameCorrect, isPasswordCorrect, isActive;

GetIsUserLoginCorrect(out isUserNameCorrect, out isPasswordCorrect, out isActive);

if (isUserNameCorrect && isPasswordCorrect)
{
if (isActive)
{
this.Hide();

if (RememberMeCheckBox.Checked)
{
Properties.Settings.Default.UserName = UserNameTextBox.Text;
Properties.Settings.Default.Save();
}

ManageStudentsForm msf = new ManageStudentsForm();
msf.ShowDialog();
}
else
{
MessageBox.Show("Your account is not active. Please consult adminstrator for further info.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

UserNameTextBox.Clear();
PasswordTextBox.Clear();
UserNameTextBox.Focus();
}
}
else
{
if(!isUserNameCorrect)
{
MessageBox.Show("User name is not correct.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
UserNameTextBox.Clear();
PasswordTextBox.Clear();
UserNameTextBox.Focus();
}
else
{
MessageBox.Show("Password is not correct.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
PasswordTextBox.Clear();
PasswordTextBox.Focus();
}
}
}
catch(ApplicationException ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

private void GetIsUserLoginCorrect(out bool isUserNameCorrect, out bool isPasswordCorrect, out bool isActive)
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("usp_UserCheckLoginDetails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

// Output Parameter
cmd.Parameters.Add("@IsUserNameCorrect", SqlDbType.Bit).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@IsPasswordCorrect", SqlDbType.Bit).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Direction = ParameterDirection.Output;

// Parameter
cmd.Parameters.AddWithValue("@UserName", UserNameTextBox.Text);
cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);

cmd.ExecuteNonQuery();

isUserNameCorrect = (bool)cmd.Parameters["@IsUserNameCorrect"].Value;
isPasswordCorrect = (bool)cmd.Parameters["@IsPasswordCorrect"].Value;
isActive = (bool)cmd.Parameters["@IsActive"].Value;
}
}
}

private bool IsValidated()
{
if (UserNameTextBox.Text.Trim() == string.Empty)
{
MessageBox.Show("UserName is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
UserNameTextBox.Clear();
PasswordTextBox.Clear();
UserNameTextBox.Focus();
return false;
}

if (PasswordTextBox.Text.Trim() == string.Empty)
{
MessageBox.Show("Password is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
PasswordTextBox.Clear();
PasswordTextBox.Focus();
return false;
}

return true;
}

private void LoginForm_Load(object sender, EventArgs e)
{
UserNameTextBox.Text = Properties.Settings.Default.UserName;
}
}
}

Stored Procedure: usp_UserCheckLoginDetails

CREATE PROCEDURE [dbo].[usp_UserCheckLoginDetails]
(
@IsUserNameCorrect BIT OUTPUT,
@IsPasswordCorrect BIT OUTPUT,
@IsActive BIT OUTPUT,
@UserName NVARCHAR(50),
@Password NVARCHAR(50)
)
AS

BEGIN

SET @IsUserNameCorrect = 0
SET @IsPasswordCorrect = 0
SET @IsActive = 0

IF EXISTS(SELECT * FROM [dbo].[Users] WHERE [UserName] = @UserName
AND [Password] = @Password
AND [IsActive] = 1)
BEGIN
SET @IsUserNameCorrect = 1
SET @IsPasswordCorrect = 1
SET @IsActive = 1
END
ELSE
BEGIN
IF EXISTS(SELECT * FROM [dbo].[Users] WHERE [UserName] = @UserName AND [Password] = @Password)
BEGIN
SET @IsUserNameCorrect = 1
SET @IsPasswordCorrect = 1
END
ELSE
BEGIN
IF EXISTS(SELECT * FROM [dbo].[Users] WHERE [UserName] = @UserName)
BEGIN
SET @IsUserNameCorrect = 1
END

END
END


END

Comments

Post a Comment