Answer:
Table Name: Users
| Field | Data Type | Constraints |
|---|---|---|
| Username | Short Text | Primary Key |
| Password | Short Text | Not Null |
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LoginApp._Default" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Login Form</h2>
<table>
<tr>
<td>Username:</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 colspan="2">
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
</td>
</tr>
</table>
<p><strong>Or</strong></p>
<asp:Button ID="btnRegister" runat="server" Text="Register New User" OnClick="btnRegister_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Data.OleDb;
using System.Web.UI;
namespace LoginApp
{
public partial class _Default : Page
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\path_to_your_database\LoginDB.accdb;"; // Change path
protected void Page_Load(object sender, EventArgs e)
{
}
// Register a new user by inserting into the database
protected void btnRegister_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "INSERT INTO Users (Username, Password) VALUES (@Username, @Password)";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);
conn.Open();
cmd.ExecuteNonQuery();
Response.Write("Registration successful!");
}
}
else
{
Response.Write("Please enter both username and password.");
}
}
// Log in by checking the username and password from the database
protected void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
if (AuthenticateUser(username, password))
{
Response.Redirect("Welcome.aspx"); // Redirect to another page after successful login
}
else
{
Response.Write("Invalid login credentials.");
}
}
// Authenticate user against the Access database
private bool AuthenticateUser(string username, string password)
{
bool isAuthenticated = false;
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT COUNT(1) FROM Users WHERE Username = @Username AND Password = @Password";
OleDbCommand cmd = new OleDbCommand(query, conn);
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);
conn.Open();
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result > 0)
{
isAuthenticated = true;
}
}
return isAuthenticated;
}
}
}