Click here to Skip to main content
15,120,478 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hi can any one solve my problem..

here is my login code for btnLogin
C#
protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
   {
       LoginPL loginPL = new LoginPL();
       loginPL.UserName = txtUserName.Text;
       loginPL.Password = txtPassword.Text;

       LoginBLL loginBLL = new LoginBLL();
       bool loginSuccess;
       loginSuccess = loginBLL.Login(loginPL);
       if (loginPL.UserName=="admin" && loginPL.Password=="admin")

       {
           Response.Redirect("Admin/HomeUser.aspx");


       }
       else
       {
           //Response.Redirect("Tester/TesterWorkStatus.aspx");



           lblMessage.Text="Invalid Username / Password";
       }
       if (//how to give code here ......)
       {
           Response.Redirect("Tester/TesterWorkStatus.aspx");

       }
       else{
           lblMessage.Text="Invalid UserName/Password";
       }

   }

if(// how to give code here i mean if other user login with their login name and password it should match table and to redirect..plea help me for small problem.and there are many users so they want to login from their username and password and it should match sqltable..can any one solve this..

thanks and regards..
fahad sheikhji
Posted
Updated 26-Feb-12 20:34pm
v2
Comments
ythisbug 27-Feb-12 1:14am
   
here is my sql Tbale

UserName  Password  Role
admin        admin        True
shahid        shahid        False
shaz          shaz        False
Mazi          mazi        False
Rux          Rux        False

Hi,

you need to create one function in your BAL project. the function would return true/false depending upon your data that you insert.

So, your next if statement required that function in it. That will work for you.

and your BAL function will call DAL function for searching single username and password in database. if it is there then it will return true.

hope this information will helps you,

thanks
-amit.
   
Comments
ythisbug 27-Feb-12 1:41am
   
public class LoginBLL
{
public bool Login(LoginPL loginPL)
{
bool loginSuccess = false;

LoginDAL loginDAL = new LoginDAL();

DataTable dt = new DataTable();

dt = loginDAL.GetLogin(loginPL);

if (dt.Rows.Count > 0)
{
loginSuccess = true;
}

return loginSuccess;
}

hi u mean this code..???
AmitGajjar 27-Feb-12 1:43am
   
yes, so your function name would be checkLogin. and your if condition will be something like if(checkLogin(loginPL)) {//Redirect page}

ythisbug 27-Feb-12 1:54am
   
can u give me sample..i don knw how to create checklogin..i tried but its comin error..
AmitGajjar 27-Feb-12 3:44am
   
show me your code...
ythisbug 27-Feb-12 4:21am
   
protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
{


LoginPL loginPL = new LoginPL();
loginPL.UserName = txtUserName.Text;
loginPL.Password = txtPassword.Text;

LoginBLL loginBLL = new LoginBLL();
bool loginSuccess;
loginSuccess = loginBLL.Login(loginPL);
//if (loginPL.UserName == "admin" && loginPL.Password == "admin")
if(Login(loginPL))///how to write here code...??
{
Response.Redirect("Admin/HomeUser.aspx");


}
else
{
//Response.Redirect("Tester/TesterWorkStatus.aspx");



lblMessage.Text = "Invalid Username / Password";
}
//if (loginPL.UserName == txtUserName.Text && loginPL.Password == txtPassword.Text)
if(Login(loginPL))///how to write here code...??
{
Response.Redirect("Tester/TesterWorkStatus.aspx");

}
else
{
lblMessage.Text = "Invalid UserName/Password";
}
AmitGajjar 27-Feb-12 4:35am
   
your code is correct. whats return value if you have correct credential from Login(loginPL)?
ythisbug 27-Feb-12 5:25am
   
namespace Masters.Login
{
public class LoginBLL
{
public bool Login(LoginPL loginPL)
{
bool loginSuccess = false;

LoginDAL loginDAL = new LoginDAL();

DataTable dt = new DataTable();

dt = loginDAL.GetLogin(loginPL);

if (dt.Rows.Count > 0)
{
loginSuccess = true;
}

return loginSuccess;
}

public DataTable GetLoginAll()
{
DataTable dt = new DataTable();

LoginDAL loginDAL = new LoginDAL();

dt = loginDAL.GetLoginAll();

return dt;
}
}

hi here is my BLL file..where should i delcare..can u tel me?
AmitGajjar 27-Feb-12 5:27am
   
your code is correct. what problem do you have ?
ythisbug 27-Feb-12 6:10am
   
problem is if i login with any name it redirects if i type wrong password also..so can u solve..
AmitGajjar 27-Feb-12 6:43am
   
whats your code in loginDAL.GetLogin method ?
ythisbug 27-Feb-12 7:03am
   
public class LoginDAL
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());

public DataTable GetLogin(LoginPL loginPL)
{
DataTable dt = new DataTable();

cnn.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "Select * from tblLogin where UserName='" + loginPL.UserName + "' AND Password='" + loginPL.Password + "'";
cmd.CommandType = CommandType.Text;
cmd.Connection = cnn;

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt);

cmd.Dispose();
cnn.Close();

return dt;

}

public DataTable GetLoginAll()
{
DataTable dt = new DataTable();

cnn.Open();

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "Select * from tblLogin";
cmd.CommandType = CommandType.Text;
cmd.Connection = cnn;

SqlDataAdapter da = new SqlDataAdapter(cmd);

da.Fill(dt);

cmd.Dispose();
cnn.Close();

return dt;

}


}

this is LoginDAL
AmitGajjar 27-Feb-12 7:14am
   
Your code is correct. when you debug your Login method what dt.Rows.Count return for incorrect and correct value ?
ythisbug 27-Feb-12 7:16am
   
this is my login code..


protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
{


LoginPL loginPL = new LoginPL();
loginPL.UserName = txtUserName.Text;
loginPL.Password = txtPassword.Text;

LoginBLL loginBLL = new LoginBLL();
bool loginSuccess;
loginSuccess = loginBLL.Login(loginPL);
if (loginPL.UserName == "admin")

{
Response.Redirect("Admin/HomeUser.aspx");


}
else
{


Response.Redirect("Tester/TesterWorkStatus.aspx");

}
AmitGajjar 27-Feb-12 7:20am
   
if (loginPL.UserName == "admin" && loginPL.PassWord == "admin") { Response.Redirect("Admin/HomeUser.aspx"); } else if(loginSuccess) { Response.Redirect("Tester/TesterWorkStatus.aspx"); }

use above code and try.
ythisbug 27-Feb-12 7:31am
   
thanks amit..
AmitGajjar 27-Feb-12 7:33am
   
Welcome..
ythisbug 27-Feb-12 7:18am
   
if i enter username as admin and password if i type any it redirects to admin..
AmitGajjar 27-Feb-12 7:17am
   
Another Problem :SQL Injection could be possible if you code your query like this. you need to either filter your textbox inputs or place your query in StoredProcedure.
ythisbug 27-Feb-12 7:19am
   
and for other user also same if any name u enter it redirects to other master page.so there s the problem
protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e) 
{ 
LoginPL loginPL = new LoginPL(); 
loginPL.UserName = txtUserName.Text;
 loginPL.Password = txtPassword.Text; 
LoginBLL loginBLL = new LoginBLL();
 bool loginSuccess; loginSuccess = loginBLL.Login(loginPL);
if(loginPL.UserName == "admin" && loginPL.Password == "admin")
 { Response.Redirect("Admin/HomeUser.aspx"); 
} 
else if(loginSuccess) 
{ 
Response.Redirect("Tester/TesterWorkStatus.aspx");
 }
   
before checking the login name and password from the textbox, get the data from the sql table into datatable as shown below

C#
using (SqlCommand command = new SqlCommand("SELECT UserName, Password FROM TableName WHERE UserName = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'", connection))
        {
        //
        // Invoke ExecuteReader method.
        //
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                if(reader.GetString(1) == txtUserName.Text && reader.GetString(2) == txtPassword.Text)
                {
                    Response.Redirect("Admin/HomeUser.aspx"); 
                }else
                {
                    //Invalid User
                    Response.Redirect("Tester/TesterWorkStatus.aspx");
                }
            }
        }

Hope this will give you an idea...
   
v2
Comments
Tech Code Freak 27-Feb-12 4:03am
   
Added a " which was missed by mistake.
Add UserType in a table

Based on UserType u can give the permission to access

loginBLL.Login() this funtion should return the loginPL object

C#
if (loginPL.UserType==1)

        {
            Response.Redirect("Admin/HomeUser.aspx");


        }
        else if (loginPL.UserType==2 )      {
          Response.Redirect("Tester/TesterWorkStatus.aspx");

        }
else if (loginPL.UserType==3 )      {
          Response.Redirect("User/Home.aspx");

        }
else
{
 lblMessage.Text="Invalid UserName/Password";

}
   
v2
Comments
Tech Code Freak 27-Feb-12 4:05am
   
Corrected some tags.
ythisbug 27-Feb-12 4:28am
   
how to create usertype in cs class..can u telme?
add namespace

C#
using System.Data.SqlClient;


 protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
    {
        LoginPL loginPL = new LoginPL();
        loginPL.UserName = txtUserName.Text;
        loginPL.Password = txtPassword.Text;
 
        LoginBLL loginBLL = new LoginBLL();
        bool loginSuccess;
        loginSuccess = loginBLL.Login(loginPL);
      
       //Add connection string put your sever name in DataSource  and put                               //database name in Initial Catalog.

       SqlConnection con = new SqlConnection("DataSource=(LOCAL); Initial Catalog=databasename; Integrated Security=true;");
con.Open();
       
       SqlCommand cmd = new SqlCommand("Select * from tablename where user_name = @us", con);
        cmd.Parameters.Add(new SqlParameter("@us", txtUserName.Text));
        SqlDataReader dr = cmd.ExecuteReader();
        int flag = 0;
        while (dr.Read())
        {
           if (loginPL.Password==dr["Password"].ToString())           
           {
              flag++;                  
           }
        }
        if(flag > 0)
        {       
            lblMessage.Text="Invalid Username / Password";
        }
        else
        {
             Response.Redirect("Admin/HomeUser.aspx"); 
        }
        if (//how to give code here ......)
        {
            Response.Redirect("Tester/TesterWorkStatus.aspx");
 
        }
        else{
            lblMessage.Text="Invalid UserName/Password";
        }
    
    }


Think this will help..... good luck :)
   
Comments
ythisbug 27-Feb-12 4:28am
   
hi varun ..m using class files ..so here its not necessary to give again sqlconnection..??rite.
varuncodee 27-Feb-12 4:33am
   
yes, not necessary if you are using class file.
varuncodee 27-Feb-12 4:41am
   
just call the Class here and open the connection...

For example:
Class1 a = new Class1();
SqlConnection con = new SqlConnection(a.obj);
con.Open();
 protected void imgbtnLogin_Click(object sender, ImageClickEventArgs e)
{ 
  LoginPL loginPL = new LoginPL();
  loginPL.UserName = txtUserName.Text; 
  loginPL.Password = txtPassword.Text;
  LoginBLL loginBLL = new LoginBLL();
  bool loginSuccess; loginSuccess = loginBLL.Login(loginPL);
  if(loginPL.UserName == "admin" && loginPL.Password == "admin") 
{ 
  Response.Redirect("Admin/HomeUser.aspx");
} 
else if(loginSuccess)
{
 Response.Redirect("Tester/TesterWorkStatus.aspx"); 
}
   

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