1

I know this question has been asked by someone, but this is my case:

the correct url is: http://localhost:1478/application/ProductID.aspx?ID=1001

This retrieves the list of my product. But if I remove the query string let say:

http://localhost:1478/application/ProductID.aspx 

The error is Object reference not set to an instance of an object. Is there any implementation of error handling in this problem?

8
  • Can you post the lines surrounding the code that is causing the issue. It will help us figure out how to help you better. Commented Apr 17, 2013 at 2:54
  • Line 68: private void BindGridMain() Line 69: { Line 70: string ProductID = Request.QueryString["ID"].ToString(); //Problems is in here Line 71: ShowProductSearchResult showResult = new ShowProductSearchResult(); Line 72: DataList1.DataSource = showResult.ShowProductResult(ProductID); Commented Apr 17, 2013 at 2:58
  • Without looking at the code, I can't be sure.. but I think you are using something like Request.QueryString["ID"] which will throw that exception, because there is no ID in the query string. I think you can use Request.QueryString.HasKey("ID")... if I remember correctly.. Commented Apr 17, 2013 at 2:58
  • Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. Commented Apr 17, 2013 at 3:26
  • Also, I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Apr 17, 2013 at 3:27

4 Answers 4

2

Change

string ProductID = Request.QueryString["ID"].ToString(); 

to

 if(!string.IsNullOrWhiteSpace(Request.QueryString["ID"]))
 {
    string ProductID = Request.QueryString["ID"]; 
    DataList1.DataSource = showResult.ShowProductResult(ProductID);
 } 
 else
 {
    //Do something when Id is empty or null
    //DataList1.DataSource =null;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Basically your getting that error because the page needs the missing information to complete its process.

If you want it to run without the error then you would need an "if" block to check if a (product) Id parameter has been passed in the querystring or url.

VB

If trim(request("ID")) <> "" Then
  'Do your product search here and display the results in your page HTML 
Else
  'No product ID parameter was passed! - You may optionally display this fact in your HTML
End If

C#

if (!string.IsNullOrEmpty(request("ID")) {
  //Do your product search here and display the results in your page HTML 
} else {
  //No product ID parameter was passed! - You may optionally display this fact in your HTML
}

Comments

0

Change

string ProductID = Request.QueryString["ID"].ToString(); 

to

 string ProductID="";
 if(Request.QueryString["ID"]!=null)
 {
    ProductID = Request.QueryString["ID"].ToString(); 
    DataList1.DataSource = showResult.ShowProductResult(ProductID);
 } 
 else
 {
    //DataList1.DataSource =null;
 }

Comments

0

Try this way,first check condition for query string return not null . see below code.

int ProductID ;
if(Request.QueryString["ID"]!=null)
 {
    ProductID =Convert.ToInt32( Request.QueryString["ID"].ToString()); 
    showResult.YourmethodName(ProductID);
 } 
 else
 {
    //Show one error lik "INVALID URL" ,Please go back
 }

Comments

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.