Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Object Reference Error when trying to use Request.QueryString [duplicate]

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?

like image 742
Boxiom Avatar asked Sep 24 '26 11:09

Boxiom


1 Answers

You need to check for nulls

if (Request.QueryString["id"] != null && Request.QueryString["id"].Equals("1"))
{
   //Do something
}
like image 74
pbjork Avatar answered Sep 26 '26 00:09

pbjork



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!