Tuesday, 11 February 2014

Perfect login form code in Asp.net c# using stored procedure

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

usingSystem.Data.SqlClient;

namespace company
{
    public partial class LoginForm : Telerik.WinControls.UI.RadForm
    {
        publicLoginForm()
        {
            InitializeComponent();
        }

        SqlCommandcmd = new SqlCommand();
        SqlDataAdapterda = new SqlDataAdapter();
        DataSetds;
        privatevoid btnLogin_Click(objectsender, EventArgs e)
        {
            try
            {
                if(txtUserID.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Please Enter ID", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUserID.Focus();
                }
                elseif (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;

                    intoutput = 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();
                    }
                    elseif (output == 2)
                    {
                        MessageBox.Show("User Status is Inactive ", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtUserID.Focus();
                    }
                    elseif (output == -1)
                    {
                        MessageBox.Show("Invalid Password", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtPass.Focus();
                    }
                    elseif (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);
            }
        }


        privatevoid btnClose_Click(objectsender, 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 whereUM_USER_ID=@p_userid)
            Begin
                  if exists(Select 1 from USER_MASTER whereUM_USER_ID=@p_userid andUM_PASSWORD=@p_password)
                  Begin
                        if Exists(Select 1 from USER_MASTER whereUM_USER_ID=@p_userid andUM_PASSWORD=@p_password and UM_STATUS='A')
                        begin
                              set@p_result     =1
                              SelectUM_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     = --- 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