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)
        {

        }
    }
}

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.