Click here to Skip to main content
15,898,658 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to redirect users on different pages based on their role ,i.e. if user is admin or super admin he will be redirected to AdmnDashboard.aspx nad rest of the users are redirceted to some other page..
problem is that though i am log in as admin and debug my code, it validate that i am admin but when it reaches to response.redirect method,it suddenly goes to Catch part and give error message ..pllz do suggest some changes..

C#
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {

                lblUserName.Text = Session["Employee"].ToString();
                lblDate.Text = Convert.ToString(DateTime.Now.ToString("dd-MMM-yyyy"));

                string role = Session["Role"].ToString();
                if (role == "Super Admin" || role == "Admin")
                {
                    Response.Redirect("AdminDashboard.aspx");

                }
                else
                {
                    Response.Redirect("ClientDashBoard.aspx");
                }
            }
        }
        catch (Exception ex)
        {
            string str = ex.Message;
            Response.Redirect("ErrorPage1.aspx?Error='" + str + "'");
        }

    }
Posted
Updated 17-Feb-14 20:33pm
v2
Comments
Aravindba 18-Feb-14 2:38am    
hi did u get any value in role ? if not Super Admin or Admin what u get in role ? and what u get in error msg ? pls post ur error msg here.And wht u write in AdminDashboard.aspx page load ?
rahulDer 18-Feb-14 2:58am    
yes , i got value in role as super admin and so it passes IF condition but inside if condition,that respose.redirect not works and directly go to catch part.
abdul subhan mohammed 18-Feb-14 2:51am    
have u done debug??
rahulDer 18-Feb-14 2:59am    
yes,i have done debugging ,i got value in role as super admin and so it passes IF condition but inside if condition,that respose.redirect not works and directly go to catch part.
thatraja 18-Feb-14 2:57am    
what's the error message? always include those details in your question

Response.Redirect Method[^] when called with just one parameter would throw ThreadAbortException. You should pass false as the second parameter and then complete the request. Quoting from the link mentioned above:
MSDN HttpResponse.Redirect Method :
When you use this method in a page handler to terminate a request for one page and start a new request for another page, set endResponse to false and then call the CompleteRequest method. If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended


That's the best way to handle it. You may also handle ThreadAbortException specifically in your code.
C#
try
{
    Response.Redirect("AdminDashboard.aspx");
}
catch (ThreadAbortException te)
{
    //do nothing or handle as required
}

Hope that helps!
 
Share this answer
 
Hai rahulDer,

Pls try my solution and reply me it work or not.i am just modified ur code ,use this and reply,
C#
string role = Session["Role"].ToString();
              if (role == "Super Admin" || role == "Admin")
              {
                  Response.Redirect("AdminDashboard.aspx",false);

              }
              else
              {
                  Response.Redirect("ClientDashBoard.aspx",false);
              }


add "false" in response.redirect function.And can u tell me in what reason it goes to catch part,why u write Error.aspx page ? How it go exception part in ur code ? if role have values it redirect to AdminDashboard page and if not vlaue in role it automatically go to ClientDashboard page.Then y u add error page in exception.

Now it come to exeception i know and i am also try in my place,u r correct it goes to exception bcz some reasoan.

Note: if u got working code or any clear u need to click "Accept Solution" or post ur working code in "I've solved this myself!" dont forget,not only u benefit other people also have same problem can get solution quickly.

Regards
Aravind

Happy Codings.....
 
Share this answer
 
v2
Comments
Aravindba 19-Feb-14 21:53pm    
hi did u get solution ?

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