Asp.net blogs

Thursday 4 July 2013

Login form with c# in asp.net

Create two textboxes with following name and Login Button i.e

User Name : txtusername
Password :txtpassword
Login Button :btnlogin

Paste below code in your ASP page

<table style="width:100%" >
            <tr>
                <td align="center" colspan="3">
                    <asp:Label ID="lblMsg" runat="server" FontBold="True" ForeColor="#FF3300"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="left" colspan="3" style="fontweight: 700">
                   <h3> 
                       <asp:Label ID="Label1" runat="server" ForeColor="White" 
                           Text="Admin Login Panel"></asp:Label>
                    </h3><hr /></td>
            </tr>
            <tr>
                <td class="style1">
                    &nbsp;<asp:Label ID="Label2" runat="server" FontBold="True" ForeColor="White" 
                        Text="User Name :"></asp:Label>
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="requsername" runat="server" 
                        ControlToValidate="txtusername" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td class="style1">
                    </td>
            </tr>
            <tr>
                <td>
                    &nbsp;<asp:Label ID="Label3" runat="server" FontBold="True" ForeColor="White" 
                        Text="Password :"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqpassowrd" runat="server" 
                        ControlToValidate="txtpassword" ErrorMessage="*"></asp:RequiredFieldValidator>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnlogin" runat="server" Text="Admin Login" Width="93px" 
                        onclick="btnlogin_Click" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>


On Button Click, Paste the following c# code in aspx.cs code file

 int AdminStatus =  obj.AdminAuthenticate(txtusername.Text, txtpassword.Text);
            if (AdminStatus != 1)
            {
                lblMsg.Text = "Invalid Login";
            }
            else
            {

                Response.Redirect("Admin.aspx");


            }


Create a class file with the following below code:

  public int AdminAuthenticate(string username, string passowrd)
        {
            
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
                SqlCommand cmd = new SqlCommand("sp_AdminAuthenticate", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", passowrd);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                int retValue = Convert.ToInt32(cmd.ExecuteScalar());
                return retValue;

           
        }

Also create a stored procedure


CreatePROCEDURE [dbo].[sp_AdminAuthenticate] 
(
@username nvarchar(20),
@password nvarchar(20)
)
AS
BEGIN
IF EXISTS (select * from tbllogin where username=@username AND password = @password AND status=1 )
BEGIN
select 1 as retValue

END
ELSE
BEGIN
select 1 as retValue
END
END

No comments:

Post a Comment