Click here to Skip to main content
15,914,892 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Dear All,

am working on Asp.net , C#, SqlServer 2005

In this project i have two panels 1)Admin 2)User
In Admin Folder I have all admin pages & in user folder i have all user pages.

now here am using store procedure to redirect to different pages
If admin Logged-in, it must redirect to admin pages &
If User Logged-in, it must redirect to user pages.

This is my code....



C#
protected void BtnLogin_Click(object sender, ImageClickEventArgs e)
   {
       DataTable dt = new DataTable();
       try
       {
           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CollegeDataBaseConnectionString"].ConnectionString);
           SqlDataAdapter adp = new SqlDataAdapter("CHECK_UserAccounts_Proc", con);
           adp.SelectCommand.CommandType = CommandType.StoredProcedure;
           adp.SelectCommand.Parameters.Add("@USERNAME", SqlDbType.VarChar, 50).Value = TxtUserName.Text.Trim();
           adp.SelectCommand.Parameters.Add("@PASSWORD", SqlDbType.VarChar, 50).Value = TxtPassword.Text.Trim();
           adp.Fill(dt);


           if (dt.Rows.Count > 0)
           {

               Response.Redirect("~/User/Home.aspx");
           }
           else
           {
       ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Invalid UserName or Password');", true);
           }
       }
       catch (Exception ex)
       {
           Response.Write("Error occured : " + ex.ToString());
       }
       finally
       {
           dt.Clear();
           dt.Dispose();
       }
   }



This is my Stored Procedure
---------------------------

SQL
CREATE PROCEDURE [dbo].[CHECK_UserAccounts_Proc]
                        @USERNAME VARCHAR(50),
                        @PASSWORD VARCHAR(50)
AS
BEGIN
 SELECT * FROM UserInformation WHERE USERNAME COLLATE Latin1_general_CS_AS =@USERNAME AND [PASSWORD] COLLATE Latin1_general_CS_AS=@PASSWORD
END



Please can you help.

Thanks.
Posted
Updated 1-Dec-13 23:00pm
v2
Comments
Sampath Lokuge 2-Dec-13 4:59am    
What happened when you use above code ?
Member239258 2-Dec-13 5:03am    
its working but am redirecting only to user pages

I am admin it should redirect to admin pages, if am user it must redirect to user pages.

presently in my code, it is redirecting to only user pages,

i want to redirect to admin pages also..

Please help.

Thanks..

You have to maintain the User Role in your 'UserInformation' table.Then you can check the 'User Role' of the logged-in user and according to that value where redirect it to the relevant page.

On User Role column you can have 'Admin','User',etc...
 
Share this answer
 
v2
Comments
Member239258 2-Dec-13 5:10am    
ya, already i have mentioned UserType in Table

By the way.., my SqlTable Fields are UserName,Password,UserType
Sampath Lokuge 2-Dec-13 5:12am    
So then just test the 'UserType' column value and according to that value do the redirection.
First suggestion : Don't store password directly in database either in plain text or in encrypted format.

As per as the answer is concerned,i can't see the code where you are trying to redirect the Admins. Redirection has nothing to do with stored procedure.Well,there are two solutions: one is to use Roles,for that see these links :

Authenticate User by Roles in ASP.NET[^]

Understanding Role Management[^]

Another one is to have one column as roles in database. After successful login,check the user's role in database and redirect it accordingly.
 
Share this answer
 
Below is snippet code from your full code, please tried like below

C#
if (dt.Rows.Count > 0)
           {
               if (dt.Rows[0].Columns["UserType"] == "Admin")
               {
                    Response.Redirect("~/Admin/Home.aspx");
               }
               else
               {
                    Response.Redirect("~/User/Home.aspx");
               }
           }
           else
           {
       ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Invalid UserName or Password');", true);
           }
 
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