This is perfect login code for login into application,
here I am using stored procedures. Here I posted my table code and stored
procedure code.
LoginForm.cs
using
System.Data.SqlClient;
namespace company
{
public partial class LoginForm : Telerik.WinControls.UI.RadForm
{
public
LoginForm()
{
InitializeComponent();
}
SqlCommand
cmd = new SqlCommand();
SqlDataAdapter
da = new SqlDataAdapter();
DataSet
ds;
private
void btnLogin_Click(object
sender, EventArgs e)
{
try
{
if
(txtUserID.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter ID", "", MessageBoxButtons.OK,
MessageBoxIcon.Information);
txtUserID.Focus();
}
else
if (txtPass.Text.Trim().Length == 0)
{
MessageBox.Show("Please Enter Password", "", MessageBoxButtons.OK,
MessageBoxIcon.Information);
txtPass.Focus();
}
else
{
if
((Program.Con.State == ConnectionState.Closed)) Program.Con.Open();
cmd = new SqlCommand("USP_Chk_UserDetails", Program.Con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@p_userid",
txtUserID.Text);
cmd.Parameters.AddWithValue("@p_password",
txtPass.Text);
cmd.Parameters.Add("@p_result", SqlDbType.Int);
cmd.Parameters["@p_result"].Direction = ParameterDirection.Output;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "Users");
//cmd.ExecuteNonQuery()
Program.userID
= txtUserID.Text;
int
output = Convert.ToInt32(cmd.Parameters["@p_result"].Value);
if
(output == 1)
{
Program.userName = ds.Tables[0].Rows[0]["UM_USER_NAME"].ToString();
Program.userpwd = ds.Tables[0].Rows[0]["UM_PASSWORD"].ToString();
this.Hide();
MainForm frm = new MainForm();
frm.Show();
}
else
if (output == 2)
{
MessageBox.Show("User Status
is Inactive ", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUserID.Focus();
}
else
if (output == -1)
{
MessageBox.Show("Invalid
Password", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPass.Focus();
}
else
if (output == -2)
{
MessageBox.Show("Invalid User
ID", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUserID.Focus();
}
else
{
MessageBox.Show("Invalid User
ID/Password", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtUserID.Focus();
}
}
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private
void btnClose_Click(object
sender, EventArgs e)
{
this.Close();
}
}
}
This is my stored procedure USP_Chk_UserDetails
Create procedure [dbo].[USP_Chk_UserDetails]
(
@p_userid varchar(20),
@p_password varchar(20),
@p_result int output
)
as
set nocount on
begin
if Exists(Select 1 from USER_MASTER where
UM_USER_ID=@p_userid)
Begin
if exists(Select 1 from USER_MASTER where
UM_USER_ID=@p_userid and
UM_PASSWORD=@p_password)
Begin
if Exists(Select 1 from USER_MASTER where
UM_USER_ID=@p_userid and
UM_PASSWORD=@p_password and UM_STATUS='A')
begin
set
@p_result =
1
Select
UM_USER_NAME,UM_PASSWORD, from USER_MASTER where UM_USER_ID=@p_userid
and UM_PASSWORD=@p_password
End
else
set
@P_result = 2 -- Inactive
End
Else
set @p_result = -1 -- Invalid Password
End
Else
set @p_result = -2 -- Invalid UserId
end
This is my Table
CREATE TABLE [dbo].[USER_MASTER](
[UM_USER_ID] [varchar](20) NOT NULL,
[UM_PASSWORD] [varchar](20) NOT NULL,
[UM_USER_NAME] [varchar](40) NOT NULL,
[UM_ADDRESS] [varchar](100) NULL,
[UM_PHNO] [varchar](20) NULL,
CONSTRAINT
[PK_USER_MASTER] PRIMARY KEY CLUSTERED
(
[UM_USER_ID] ASC
)WITH
(PAD_INDEX
= OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT
[UQ__USER_MASTER__0BC6C43E] UNIQUE NONCLUSTERED
(
[UM_USER_ID] ASC
)WITH
(PAD_INDEX
= OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
No comments:
Post a Comment