I want the btnLogin_Click event to verify the username/password from the ms access database, if it matches it will take them to a certain page, if not it will give a message that user/password is incorrect try again. I really don't know where to start, can you please help

first I have entered the login page code, second I will enter my codebehind

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FreightBillInquiry.aspx.cs" Inherits="FBInquiry.FreightBillInquiry" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    New users can <a href="CarrierRegistration.aspx">register</a> here.<br />

    <table  border="0" style="border-width: 3px; border-color:#c2c2c2;
border-style: solid;">  
   <tr>
   <td></td>
   <td><strong>Please Login</strong></td></tr>
    <tr>
    <td>
        <asp:Label ID="lblUser" runat="server" Text="Username:"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
    </td></tr>
    <tr>
    <td>
        <asp:Label ID="lblPass" runat="server" Text="Password:"></asp:Label>
    </td>
    <td>
        <asp:TextBox ID="txtPass" runat="server"></asp:TextBox>
    </td></tr>
    <tr>
    <td></td>
    <td>

    </td></tr>
    <tr>
    <td></td>
    <td> <asp:Button ID="btnLogin" runat="server" Text="Login" 
            onclick="btnLogin_Click" /></td></tr></table>

    </div>
    </form>
</body>
</html>

codebehind for login form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace FBInquiry
{
    public partial class FreightBillInquiry : System.Web.UI.Page
    {

        System.Data.OleDb.OleDbConnection con;
        DataSet dsl;
        System.Data.OleDb.OleDbDataAdapter da;

        protected void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.OleDb.OleDbConnection();
            dsl = new DataSet();
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OleDb.4.0; Data Source=C:\\Users\\Christie\\Desktop\\login\\FBInquiry\\App_Data\\prosearchusers.mdb";
            con.Open();

            string sql = "SELECT * FROM tblCustRecords";
            da=new System.Data.OleDb.OleDbDataAdapter(sql,con);

            con.Close();
            con.Dispose();

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {

        }
    }
}

Dani AI

Generated

A few concrete fixes and a safe, minimal pattern to get login working.

Common problems seen in this thread: you created a DataAdapter but never call Fill, you open/close the connection at the wrong time, and event-handler code ended up inside a DataLayer class (that causes parse/compile errors). was right about the C# if syntax; is right that you should query for matching rows; and ’s logic works conceptually but is unsafe because it concatenates user input into SQL.

Use this checklist first:

  • Make sure the code-behind class, namespace and the page’s Inherits match exactly (parse errors often come from mismatched names).
  • Put btnLogin_Click in the Page class (not inside a helper/DataLayer class), and use Page_Load (and if (!IsPostBack) when appropriate).
  • Move the connection string into web.config and read it with ConfigurationManager.
  • Never build SQL by concatenation. Use parameterized OleDbCommand (Access uses positional ? parameters).
  • Display errors in a Label on the page (do not use MessageBox in a web app) and always return a generic message like “Username or password is incorrect.”

Example (minimal, original) pattern for btnLogin_Click:

protected void btnLogin_Click(object sender, EventArgs e)
{
    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MyAccess"].ConnectionString;

    using (var conn = new System.Data.OleDb.OleDbConnection(connStr))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT Cust_Pass FROM tblCustRecords WHERE Cust_Name = ?";
        cmd.Parameters.AddWithValue("?", txtUser.Text.Trim());
        var dbVal = cmd.ExecuteScalar();

        if (dbVal != null && dbVal != DBNull.Value)
        {
            string stored = dbVal.ToString();
            if (stored == txtPass.Text)        // temporary: replace with hash verification
            {
                Session["User"] = txtUser.Text.Trim();
                Response.Redirect("ProtectedPage.aspx");
            }
            else
            {
                lblError.Text = "Username or password is incorrect.";
                lblError.Visible = true;
            }
        }
        else
        {
            lblError.Text = "Username or password is incorrect.";
            lblError.Visible = true;
        }
    }
}

Security notes: do not store plain-text passwords — use PBKDF2 (Rfc2898DeriveBytes) or a modern hashing library and verify hashes. Prefer FormsAuthentication or an authentication cookie rather than relying on raw session flags.

Recommended Answers

All 4 Replies

Change your SQL statement so it selects all records that match the entered user name and password. Only one result should be returned so if that is true the user has successfully logged so you can redirect them.
If you are using a dataAdapter, fill it from the database and then check the number of rows the resulting dataSet has.

I guess I don't really understand....
this is what I have for the codebehind is this right? I am getting a parse error when I run this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace FBInquiry
{

    public class DataLayer
    {
        private OleDbConnection conn;
        private readonly String connString;

        public DataLayer()
        {
            connString = @"PROVIDER=Microsoft.Jet.OleDb.4.0; Data Source=C:\Users\Christie\Desktop\login\FBInquiry\App_Data\prosearchusers.mdb";
            conn = new OleDbConnection(connString);                    
        }

        private void OpenConnection()
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
        }

        public DataTable FillTable(String sql)
        {
            DataTable table = new DataTable();
            using (OleDbDataAdapter da = new OleDbDataAdapter(sql, conn))
            {
                string sql = "SELECT * FROM tblCustRecords";
                da.Fill(table);
            }
            return table;
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if username = "Cust_Name" and if password = "Cust_Pass"
            then msgbox" you are logged in"
            else
            msgbox"login error" 
        }
    }
}

if username = "Cust_Name" and if password = "Cust_Pass"
then msgbox" you are logged in"
else
msgbox"login error"

This part is syntactically wrong. It should be something like this:

if (username=="Cust_Name" && password=="Cust_Pass")
{
    MessageBox.Show("You are logged in");
}
else
{
    MessageBox.Show("Error");
}

try this code,I am using here SQL server but it is the same logic when you verify your username and password.

 string cmdstr = "Select count(*) from tblCustRecords where Username='" + TextBoxUN.Text + "'";
        SqlCommand checkUser = new SqlCommand(cmdstr, con);
        int temp = Convert.ToInt32(checkUser.ExecuteScalar().ToString());

        if (temp == 1)
        {
            string cmdstr2 = "Select Password from tblCustRecords where Username='" + TextBoxUN.Text + "'";
            SqlCommand pass = new SqlCommand(cmdstr2, con);
            string password = pass.ExecuteScalar().ToString();
            con.Close();
            if (password == TextBoxP.Text)
            {
               // do anything
            }
            else
            {
                Label1.Visible = true;
                Label1.Text = "Invalid password..!!";
            }
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "Invalid username..!!";
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.