Click here to Skip to main content
15,891,777 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 19: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.
 
Share this answer
 
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";
}
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");
 }
 
Share this answer
 
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...
 
Share this answer
 
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";

}
 
Share this answer
 
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 :)
 
Share this answer
 
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"); 
}
 
Share this answer
 

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