Check whether Querystring is available or not.
Now , i am going to explain how to check Whether the Querystring is present or available or not.
For ex: I am passing id as a querystring to the page2.aspx from page1.aspx.
So on Page1.aspxError : Object Reference Exception.Button1_Clickevent() Response.Redirect("page2.aspx?id=1",false); Then on Page2.aspx Page2_Loadevent() If(!IsPostback) { If(!String.ISNullOrEmpty(Request.QueryString["id"])) { // get the value of Querstring id , if id is not null or empty } else if(!String.ISNullOrEmpty(Request.QueryString["name"])) { // get the value of name querystring if name is not null or empty } } When you run the above code - ie: on Page2.aspx you can get the id Querystring value. this works fine.Suppose if you pass some other querystring name ex: name from the Page1.aspx the above code givesie: Button2_Clickevent()Response.Redirect("page2.aspx?name=Dotnet",false);
Because it first check the if condition so in the first condition it contains id so the querystring id will not present so it leads to the Null reference exception.
to avoid this exception you have to check like this.
Page2_Loadevent()If(!IsPostback) { if(Request.QueryString["id"] != null) { If(!String.ISNullOrEmpty(Request.QueryString["id"])) { // get the value of Querstring id , if id is not null or empty } } if ((Request.QueryString["name"] != null) { if(!String.ISNullOrEmpty(Request.QueryString["name"])) { // get the value of name querystring if name is not null or empty } } }
This if(Request.QueryString["id"] != null) - checks if id is a QueryString variable.
If(!String.ISNullOrEmpty(Request.QueryString["id"])) - Check if the id is empty or not
Read more »