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

Posted on 2013-01-25
Medium Priority
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:Srinivas Mantha
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

by:Srinivas Mantha
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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Srinivas Mantha
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 1000 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

by:Srinivas Mantha
ID: 38823579
Thank you for the accurate solution

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

718 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