Question

For example, in Login.aspx , I have two link.

<a href="Main.aspx?site=facebook">Facebook_link</a>
<a href="Main.aspx">Google_link</a> // (I wouldn't like to wirte "Main.aspx?site=google"

On Main Page_Load , there is :

if(Request.QueryString["site"].ToString()!="facebook")
{
.....
}

On Login page if I click Facebook_link and then I go to Main page, everything works great.

But on Login page if I click Google_link and then I go to Main page , I get error. [Error: Object reference not set to an instance of an object]

I know why I get this error.

I woud like to ask that it is possible to check on Main page that "If Request.QueryString["site"] is exist, do this".

Was it helpful?

Solution

Request.QueryString["site"] is probably evaluating to null.

To prevent this happening, you need to check for null like this:

var queryString = Request.QueryString["site"];

if (queryString != null && queryString.ToString() != "facebook")
{    .....
}

OTHER TIPS

Try this:

if (Request.QueryString["site"]!= null && Request.QueryString["site"]!= "facebook")
{    .....
}

QueryString is like session, hidden fields. Once you set a value it will hold it untill you change it. you have told that you click facebook link. so querystring will hold the value "facebook" even you click on google link. because you haven't set any values on google. So just do like this,

<a href="Main.aspx?site=google">Google_link</a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top