Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb; //connecting database

public partial class LawyersHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        string a = Request.Form["uname"].ToString();
        string b = Request.Form["pwd"].ToString();
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\MoratuwaLawyersAssociation\MLADB.mdb";
        con.Open();
        string sql = "SELECT * FROM admin";
        OleDbCommand comm = new OleDbCommand(sql, con);
        OleDbDataReader or;
        or = comm.ExecuteReader();
        while (or.Read())
        {
            if ((a == or["aID"].ToString()) && (b == or["apw"].ToString()))
            {
                con.Close();
                Response.Redirect("LawyersHome.aspx");
            }
            else
            {
                con.Close();
                Response.Redirect("IncorrectLogin.aspx");
            }
        }
        


    }
}


even though i enter a correct password it re direct into wrong page can anybody help me to overcome this.
Posted
Updated 20-Sep-10 2:15am
v2

may be space is there in username and password. so use trim

if ((a == or["aID"].ToString().trim) && (b == or["apw"].ToString().trim))
   
Comments
Strider1987 20-Sep-10 8:51am
   
This works only once when i try second it doesn't. i think there s something wrong inside my code. ty for ur support
How many records you have in your admin table?
Debug your application and you will find the disaster you have made. If the first record does not match with your username and password it will redirect to the “wrong page”.
Select the only for the username and password specified using where condition and then check whether any records fetched or not. If fetched then redirect to success page, error page otherwise. That will solve your problem.
   
Comments
Strider1987 23-Sep-10 5:40am
   
yeah thatz the problem. TY
Hi,

There is a logic issue in your code.

First say, your admin table contains following records
sno aID aPW

1 ID1 PW1
2 ID2 PW2
3 ID3 PW3

As per your code logic, if i give ID2 and PW2, the WHILE loop will check the database and fetches the first record ID1 and PW1 and it fails the IF condition and goes to ELSE part and closses connection and redirects to the Web page in ELSE part.

Here modified your code, see below,

protected void Page_Load(object sender, EventArgs e)
{
string a = Request.Form["uname"].ToString();
string b = Request.Form["pwd"].ToString();
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=H:\MoratuwaLawyersAssociation\MLADB.mdb";
con.Open();
string sql = "SELECT * FROM admin";
OleDbCommand comm = new OleDbCommand(sql, con);
OleDbDataReader or;
or = comm.ExecuteReader();
bool isFound = false;
while (or.Read())
{
if ((a == or["aID"].ToString()) && (b == or["apw"].ToString()))
{
  isFound = true;
  break;
}
else
{
  isFound = false;
}
}

con.close();

if(isFound)
{
  Response.Redirect("LawyersHome.aspx");
}
else
{
  Response.Redirect("IncorrectLogin.aspx");
}

}


NOTE: You are using Openconnection method. It takes time to load your data. Try using Closed connection method. That will speed up your process.


Regards,
Suresh
   
v4
Comments
Strider1987 20-Sep-10 8:49am
   
Ty Mate. its work. :D
check the condition. During record is found, then further process continue, otherwise banned the page..
   
debug your code by break point and check string a and "or["aID"].ToString()" is equal or not, this method help you find your error
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900