0

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".

3 Answers 3

2

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")
{    .....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why do you think that? It might help us give a better answer! :)
Because many times I have tried string queryString = Request.QueryString["site"].ToString(); And I have got error. I understood. I confused. Sorry and thank you , It works :)
1

Try this:

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

Comments

0

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>

1 Comment

Actually in my project, User can be logged in by 3 ways:by wrtting login and parol, log in with facebook and log in with google. I have written their methods. but on Main Page I don't know how I must spercify that user comes by writting login and parol, or with google, or with facebook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.