Click here to Skip to main content
15,881,780 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hey

i create one login form for user and admin

in login button code

C#
protected void Button1_Click(object sender, EventArgs e)
       {
           if (cd.Loginuser(UserName.Text, Password.Text))
           {
               Session["UserID2"] = cd.Loginuser(UserName.Text, Password.Text);
               Session["Login4"] = UserName.Text;
               Session["Login5"] = Password.Text;
               Lgin.Text = ("Login Successfully");
               Response.Redirect("WebForm1.aspx");



               Lgin.Text = ("Incorrect Passsword");
           }

           else if (cd.Loginuser(UserName.Text, Password.Text))
           {
               Session["UserID"] = cd.Loginuser(UserName.Text, Password.Text);
               Session["Login2"] = UserName.Text;
               Session["Login3"] = Password.Text;
               Lgin.Text = ("Login Successfully");
               Response.Redirect("Home.aspx");

               Lgin.Text = ("Incorrect Passsword");
           }
       }


and the funtion of loginuser is follow

C#
public bool Loginuser(string UserName, string Password)
       {
           if (db.ExecuteDataSet("splogin2", new object[] { UserName, Password }).Tables[0].Rows.Count > 0)
           {
               return true;
           }
           return false;
       }



and the sp is

C#
ALTER procedure [dbo].[splogin2]
@UserName nvarchar(50),
@Password nvarchar(50)

as
select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password 
and UserTypeID=1 and UserTypeID=2



now when i debug the app
then user is login and proceed to next form but when i try to login admin the it shows me incorrect password

where is probelm in that case???

i wnat to done one login form for tboth user and admin
can somebody help me
Posted
Updated 18-Jul-13 10:03am
v2
Comments
ZurdoDev 18-Jul-13 16:12pm    
Why does it show incorrect password? Because it is wrong.
Also, why do you have and UserTypeID=1 and UserTypeID=2. Is it possible for that to be both values?
Boipelo 18-Jul-13 16:19pm    
Maybe the password is wrong, try change the UserTypeID of the one that is working to Admin and test again. I am trying to understand the "If else statement", I see no difference. I don't think the "else if" statement is useful. If "if" fails, you still trying again the same thing and it will fail again... also label "Lgin.Text" will never be display the error as you are redirecting before display it... I suggest you rework it again from stored procedure... how many UserTypes do you have? I see only 1 and 2 can log on, what about other users? Why not remove the clause "UserTypeID=1 and UserTypeID=2" and return the row, then use the return UserType to redirect you...
Diya Ayesa 18-Jul-13 16:37pm    
i copy and paste passowrd in textbox from database ...
thers is only 2 users in it one is admin and other is simpleuser..and in this only simpleuser log in not admin
Dholakiya Ankit 18-Jul-13 23:17pm    
instead of doing that one add one field in datbase like side = a or u if a then admin if u then user side so you need to check that and redirect user according to that one.........
MuhammadUSman1 19-Jul-13 0:59am    
Why you are using

[UserTypeID=1 and UserTypeID=2]

What is UserTypeID in Procedure?
And why you are using And between both type id's

Use OR on type id condition and try

Now first of all you need to Create a Role for Both the users and in your button click you need to fetch the role of the user and check into your if else conditions,


C#
if (cd.GetUserRole(UserName.Text, Password.Text) == "Administrator")
{
//do your coding here
}
else if(cd.GetUserRole(UserName.Text, Password.Text) == "User")
{
//do your coding here
}
 
Share this answer
 
v2
Comments
Diya Ayesa 18-Jul-13 16:40pm    
whe i try this it gives me error

"Operator '==' cannot be applied to operands of type 'bool' and 'string'
If my assumption about UserTypeID is not wrong then Change your SP to

SQL
ALTER procedure [dbo].[splogin2]
@UserName nvarchar(50),
@Password nvarchar(50)

as
select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password
and UserTypeID=1 OR UserTypeID=2




OR has been added as i think you may assign the Admin to UserTypeID=1 and User to UserTypeID=2,
But in your SP if you write AND then it will always be FALSE.


XML
Updated Solution->

Change Your SP as Follows-

<pre lang="text">ALTER procedure [dbo].[splogin2]
@UserName nvarchar(50),
@Password nvarchar(50)

as
If exists(select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password
and UserTypeID=1 )   select 1

If exists(select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password
and UserTypeID=2 )   select 2</pre>


Change the Function for the login User as-

//Change the return Type to int
<pre lang="c#">public int Loginuser(string UserName, string Password)
        {
// return the values if Admin then 1 else 2 for User
            return db.ExecuteDataSet("splogin2", new object[] { UserName, Password }).Tables[0].Rows[0][1].ToString())

        }
</pre>


Alter your button Click as-

<pre lang="cs">protected void Button1_Click(object sender, EventArgs e)
       {
           if (cd.Loginuser(UserName.Text, Password.Text)==1)
           {
               Session[&quot;UserID2&quot;] = cd.Loginuser(UserName.Text, Password.Text);
               Session[&quot;Login4&quot;] = UserName.Text;
               Session[&quot;Login5&quot;] = Password.Text;
               Lgin.Text = (&quot;Login Successfully&quot;);
               Response.Redirect(&quot;WebForm1.aspx&quot;);



               Lgin.Text = (&quot;Incorrect Passsword&quot;);
           }

           else if (cd.Loginuser(UserName.Text, Password.Text)==2)
           {
               Session[&quot;UserID&quot;] = cd.Loginuser(UserName.Text, Password.Text);
               Session[&quot;Login2&quot;] = UserName.Text;
               Session[&quot;Login3&quot;] = Password.Text;
               Lgin.Text = (&quot;Login Successfully&quot;);
               Response.Redirect(&quot;Home.aspx&quot;);

               Lgin.Text = (&quot;Incorrect Passsword&quot;);
           }
       }</pre>




Thanks
 
Share this answer
 
v2
Comments
Diya Ayesa 19-Jul-13 2:30am    
ok..i will be try this
Diya Ayesa 19-Jul-13 2:48am    
yeah it workss.... but when i login from both of the users it redirect only page that is webform1.aspx.....
where as i show when users login then they will go home page and admin proceed in admin page...how i show this? where as i menitoed above redirect pages....
The Doer 19-Jul-13 2:55am    
for that you have to alter your code a little bit, tell me how you distinguish user from Admin?
is it UserTypeID=1 for Admin
and
UserTypeID=2 For user?
Diya Ayesa 19-Jul-13 3:02am    
yes usertypeid=1 for admin
The Doer 19-Jul-13 3:31am    
couldnt update my previous answer , so posted as new Solution, Please check.
Updated Solution-

Change Your SP as Follows-

SQL
ALTER procedure [dbo].[splogin2]
@UserName nvarchar(50),
@Password nvarchar(50)

as
If exists(select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password
and UserTypeID=1 )   select 1

If exists(select  Users.UserID  from [Users] where UserName=@UserName and
Password=@Password
and UserTypeID=2 )   select 2


Change the Function for the login User as-

C#
public int Loginuser(string UserName, string Password)
        {
// return the values if Admin then 1 else 2 for User
            return db.ExecuteDataSet("splogin2", new object[] { UserName, Password }).Tables[0].Rows[0][1].ToString())

        }


Alter your button Click as-
C#
protected void Button1_Click(object sender, EventArgs e)
       {
// here Checking whether the login person is Admin(1) or User(2)
           if (cd.Loginuser(UserName.Text, Password.Text)==1)
           {
               Session[&quot;UserID2&quot;] = cd.Loginuser(UserName.Text, Password.Text);
               Session[&quot;Login4&quot;] = UserName.Text;
               Session[&quot;Login5&quot;] = Password.Text;
               Lgin.Text = (&quot;Login Successfully&quot;);
               Response.Redirect(&quot;WebForm1.aspx&quot;);



               Lgin.Text = (&quot;Incorrect Passsword&quot;);
           }

           else if (cd.Loginuser(UserName.Text, Password.Text)==2)
           {
               Session[&quot;UserID&quot;] = cd.Loginuser(UserName.Text, Password.Text);
               Session[&quot;Login2&quot;] = UserName.Text;
               Session[&quot;Login3&quot;] = Password.Text;
               Lgin.Text = (&quot;Login Successfully&quot;);
               Response.Redirect(&quot;Home.aspx&quot;);

               Lgin.Text = (&quot;Incorrect Passsword&quot;);
           }
       }
 
Share this answer
 
Comments
Diya Ayesa 19-Jul-13 3:32am    
ok ...i will try this
Diya Ayesa 19-Jul-13 4:01am    
it gives me error
in
this line
return db.ExecuteDataSet("splogin3", new object[] { UserName, Password }).Tables[0].Rows[0][1].ToString();

error : cannot implicitly convert type 'string' to 'int'
Diya Ayesa 19-Jul-13 4:08am    
i solve this error string to int
but here i need to tell that why u wrtie [1] here??

tables[0].Rows[0][1].ToString())
SQL
here is done this

public int Loginuser1(string UserName, string Password)
        {
            // return the values if Admin then 1 else 2 for User
           int a = Convert.ToInt32( db.ExecuteDataSet("splogin3", new object[] { UserName, Password }).Tables[0].Rows[0][1].ToString());
            return a;

        }


then it gives me error 

"CANNOT FIND COLUMN 1"
 
Share this answer
 
Comments
The Doer 19-Jul-13 4:27am    
It may be because the location Rows[0][1] may not be the appropriate one. Please Check the dataset in your window has got values(1 or 2) or not.
Diya Ayesa 19-Jul-13 7:50am    
HOW?
Diya Ayesa 19-Jul-13 13:51pm    
i write Rows[0][0] instead of Rows[0][1] and it workss
The Doer 21-Jul-13 8:31am    
Yeah thats alright, Cheers.

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