Login Page In ASP.NET Using Access Database

In this blog, let us see how to create a login page using C# language in an ASP.NET application that is using MS Access database. Please follow the below steps.

STEP 1

  • First of all, open Visual Studio and create a new website.
  • Go to the "Website" tab, click on "Add New Item", choose language as "Visual C#" and select the "Web Form" option.
  • Name this page as login.aspx and press the "Add" button.
  • Add one more "Web Form" and name that Default.aspx.

STEP 2

Now, design your web pages by trying the below code.

login.aspx

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.       
  5.         <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>  
  6.         <asp:TextBox ID="TextBox1" runat="server" Width="146px"></asp:TextBox>  
  7.         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"   
  8.             ControlToValidate="TextBox1" ErrorMessage="Enter username." ForeColor="Red"></asp:RequiredFieldValidator>  
  9.         <br />  
  10.         Password<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"   
  11.             Width="147px"></asp:TextBox>  
  12.         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"   
  13.             ControlToValidate="TextBox2" ErrorMessage="Enter password." ForeColor="Red"></asp:RequiredFieldValidator>  
  14.         <br />  
  15.         <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Log In" />  
  16.         <br />  
  17.         <asp:Label ID="Label2" runat="server"></asp:Label>  
  18.     </div>  
  19.     </form>  
  20. </body>  

After inserting the code, you will get a UI like in the below image.

Log In Page In ASP.NET Using Access Database 

Default.aspx

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.       
  5.         <asp:Label ID="Label1" runat="server" Text="Welcome"></asp:Label>  
  6.       
  7.     </div>  
  8.     </form>  
  9. </body>  

STEP 3

Now, let's create our Access database. Here are the details -

 

  • Database Name: LogInDatabase.mdb
  • Table Name: login

 

Log In Page In ASP.NET Using Access Database 

Insert some data into the login table.

Note - Do not forget to create the database (LogInDatabase.mdb) under your project folder.

STEP 4

After creating the database, let us come back to our Visual Studio project. Double-click the "Log In" button and add the below code.

login.aspx.cs

First of all, add two classes.

  1. using System.Data;  
  2. using System.Data.OleDb;  

Now, add your code.

  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if (!IsPostBack)  
  4.             TextBox1.Focus(); // blink cursor in TextBox1  
  5.     }  
  6.     protected void Button1_Click(object sender, EventArgs e)  
  7.     {  
  8.         string sql; int row;  
  9.         OleDbConnection con = new OleDbConnection();  
  10.         // establish connection  
  11.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("LogInDatabase.mdb");  
  12.         con.Open(); // connection open  
  13.         // sql query  
  14.         sql = "select count(*) from login where user='"+TextBox1.Text+"' and pass='"+TextBox2.Text+"'";  
  15.         OleDbCommand cmd = new OleDbCommand(sql, con);  
  16.         row = (int)cmd.ExecuteScalar(); // cast into integer and ExecuteScalar() get single value from database.   
  17.         con.Close(); // connection close  
  18.         if (row > 0)  
  19.             Response.Redirect("Default.aspx");  
  20.         else  
  21.             Label2.Text = "Username or Password is invalid...";  
  22.     }  

STEP 5

Now, your project is complete. Run your project by pressing F5.

Log In Page In ASP.NET Using Access Database  Log In Page In ASP.NET Using Access Database 

In this blog, I covered the creation of the Login page in ASP.NET with Access database. If you face a problem with this code, please comment below.