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?

Steven V
  • 16,357
  • 3
  • 63
  • 76
JoseTheDev
  • 62
  • 1
  • 3
  • 12
  • Can you post the lines surrounding the code that is causing the issue. It will help us figure out how to help you better. – Steven V Apr 17 '13 at 02: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); – JoseTheDev Apr 17 '13 at 02: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.. – Camron Apr 17 '13 at 02:58
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 17 '13 at 03:26
  • Also, I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 17 '13 at 03:27
  • @JohnSaunders, OP tagged the (scant in details) question as [asp-classic]. Why mention .Net exceptions? – Euro Micelli Apr 17 '13 at 03:50
  • Because the question was mistagged I have fixed it. – John Saunders Apr 17 '13 at 04:17
  • See my answer http://stackoverflow.com/a/16052052/2218635 – Ramesh Rajendran Apr 17 '13 at 05:16

4 Answers4

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;
 }
Satpal
  • 132,252
  • 13
  • 159
  • 168
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
}
Zeddy
  • 2,079
  • 1
  • 15
  • 23
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;
 }
Ken Clark
  • 2,500
  • 15
  • 15
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
 }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234