ASP.NET C# code for Page direction dynamically  based on database value

Posted on 2013-01-25
Last Modified: 2013-01-26
I am designing a web application on ASP.NET  with C# code.
I wish to redirect to respective homepages based on userids. The userlogin table has a field “pagedirection” to indicate the page for redirection. For example,
for “visitor” userid the pagedirection value is
In the usual non-dynamic mode one uses the following code

The following code to redirect dynamically is not working.

 Can anyone modify the code to get the results that I desire.
Question by:smanthanims
  • 3
  • 3
LVL 41

Expert Comment

ID: 38820422
I would first see if the correct value is returned for pagedirection.
Add this and set a breakpoint and see:

string url  = dbreader["pagedirection"].ToString();
Response.Redirect(url); //set a breakpoint and see what is the url that you are getting?

most likely you will need to have your url in ~/ format.

Author Comment

ID: 38821309
The url is returning the expected value at breakpoint i.e.
However at the end the following error message is displayed:

Server Error in '/websitesmnew' Application.
HTTP Error 400 - Bad Request.
LVL 41

Expert Comment

ID: 38821321
And what do you see in the address bar for url when the error is thrown?
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!


Author Comment

ID: 38821694
I am seeing the following in the address bar

when I commented the dyanamic mode and activated nondynamic mode, it is directed to the correct desired page in which address bar is displaying

LVL 41

Accepted Solution

guru_sami earned 250 total points
ID: 38822826
ok so you need to remove the " (double quotes) and .. from your url and add a ~ before doing a Redirect.

I would suggest you to store url without " and .. in your db e.g.

string url  = dbreader["pagedirection"].ToString();
Response.Redirect("~/" + url.TrimStart('/'));

Author Closing Comment

ID: 38823579
Thank you for the accurate solution

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question