Get Object Reference Error when trying to use Request.QueryString

asp.net, c#, nullreferenceexception, query-string

Solution

You need to check for nulls

if (Request.QueryString["id"] != null && Request.QueryString["id"].Equals("1"))
{
   //Do something
}

Problem

I have a page.aspx that reads the query string, and if the QueryString is something like page.aspx?id=1, it runs some code. However, I want it to simply ignore that code if no QueryString is specified. If I simply go to page.aspx. Right now, the only code I have on page load is ``` if (Request.QueryString["id"].Equals("1")) { //Do something } ``` That works if I go to page.aspx?id=1. However, simply going to page.aspx gives me this error: object reference not set to an instance of an object How can I load the page with no query string?

Original source

Related problems