Please enable Javascript for better experience...
How to create a login form in Asp.Net
By Big | Feb 17, 2015 | In Tips | Total Views [ 1242 ]
Taged In
(0 Like)
Rate

The example illustrates simple login form using Asp.Net

Introduction

In this example I will create simple login form using SQL in Asp.Net. Let's see step by step.

Login page

Let's create a simple login page.

            <table>
                <tr>
                    <td>User Name:</td>
                    <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" />
                    </td>
                    <td>
                        <asp:Label ID="mess" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>

Code behind

Patte below code to perform login.

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(!string.IsNullOrWhiteSpace(txtUserName.Text) && !string.IsNullOrWhiteSpace(txtPassword.Text))
        {
            cstring.Open();
            SqlCommand cmd1 = new SqlCommand("select * from userLogin where username='" + txtUserName.Text.Trim() + "' and Password='" + txtPassword.Text + "'", cstring);
            SqlDataReader dr = cmd1.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                string  usr = dr[2].ToString();
                FormsAuthentication.RedirectFromLoginPage(usr, false);
                Session["user"] = dr["UserName"].ToString();
                dr.Close();
               
            }
            else
            {
                mess.Text = "Please enter correct user name, password..";
            }
            cstring.Close();

        }

        else
        {
            mess.Text = "Please enter correct user name, password..";
        }
    }

Hop this can help you.

Share this

About the Author

Big
Rahul Jha (Big)
Founder, Developer dotnet-concept.com
Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment