Simple Login Form in ASP.Net Using C#

Introduction

This article shows how to make a simple Login Form in ASP.Net Using C#.

There are two inputs, a Username and Word, and a login button. When the user clicks that login button, the user is redirected to a new page (his account page). Otherwise, it gets an error message.

Initial Chamber

Step 1. Open Your Visual Studio 2010 and create an Empty Website; provide a suitable name (LoginForm_demo).

Step 2. In Solution Explorer, you get your empty website, then add two web forms and an SQL Server database as in the following.

For Web Form

Right-click LoginForm_demo (your empty website), then select Add New Item -> Web Form. Name it Login_demo.aspx. Use that process again, add another web form, and name it Redirectpage.aspx.

For SQL Server Database

Right-click LoginForm_demo (your empty website), then select Add New Item -> SQL Server Database. Add the database inside the App_Data_folder.

Database Chamber

Step 3. In Server Explorer, click on your database (Database.mdf), then select Tables -> Add New Table. Make the table like this.

Table -> tbl_data (Don't forget to make the ID as IS Identity = True).

DB Chamber

Figure 1. Database Chamber

Now enter some data into your database by right-clicking Tables, then select Show Table Data and enter whatever you want to add in the username and word. I entered the following data.

DB Table

Figure 2. Database Table

We will match this username and word, so if the data is correct, the user is redirected to his account page; otherwise, the user gets an error message.

DESIGN CODE

Step 4. Now make some designs for your application by going to Login_demo.aspx and try the code as in the following.

Login_demo.aspx


 
Login Form
     
Username:    
word:    
     
     

You will get something like this.

Login Form

Figure 3. Login Form

CODE CHAMBER

Step 5. We will provide some code in the Login_demo.aspx.cs page so that our login form works.

Login_demo.aspx.cs

Login Demo

Figure 4. Login Demo

public partial class _Default: System.Web.UI.Page {  
    protected void Page_Load(object sender, EventArgs e) {  
  
    }  
    protected void Button1_Click(object sender, EventArgs e) {  
        SqlConnection con = new SqlConnection(@  
        "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
        SqlCommand cmd = new SqlCommand("select * from tbl_data where username=@username and word=@word", con);  
        cmd.Parameters.AddWithValue("@username", TextBox1.Text);  
        cmd.Parameters.AddWithValue("word", TextBox2.Text);  
        SqlDataAdapter sda = new SqlDataAdapter(cmd);  
        DataTable dt = new DataTable();  
        sda.Fill(dt);  
        con.Open();  
        int i = cmd.ExecuteNonQuery();  
        con.Close();  
  
        if (dt.Rows.Count > 0) {  
            Response.Redirect("Redirectform.aspx");  
        } else {  
            Label1.Text = "Your username and word is incorrect";  
            Label1.ForeColor = System.Drawing.Color.Red;  
  
        }  
  
  
    }  
}  

Output

This is your final output. As you can see, this is our Default.aspx page (in your terms, Login_demo.aspx). When the user clicks on the login button, the user is redirected to a new page (Redirectpage.aspx).

Output

Figure 5. Output

Welcome Page

Figure 6. Welcome Page

I hope you like it. Thank you for reading. Have a nice Day!


Similar Articles