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

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

STEP 3

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

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.