Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
When I try to open the page from IDE using VIEW IN BROWSER option, I get
"Object reference not set to an instance of an object" error.
The code I get this error is as follows:
C#
XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();

Thanks.
Posted
Updated 4-Mar-11 8:43am
v3

Either the Request object is null, or XResult is null, meaning the querystring element "res" does not exist in the querystring.
 
Share this answer
 
v2
Only two possibilities: either XResult or TextBox1 is null.

The later is unlikely - you should know if it is.

The most likely is that the XResult is null, implying that the Request.QueryString is returning null. The most likely reason for this is simple: Have you checked that there is a input on your form with the name "res"?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 15:35pm    
Almost 100% certain, my 5.
--SA
Assume,

if Request.QueryString["res"] it is Null, What happens in second line.

Your are trying to Conversion of null to string. That is reason for getting 'Object reference not set to an instance of an object' error.

So, What you have to do is,

Check if Query string is not null then do conversion. like this

C#
if(Request.QueryString["res"]!=null)
{
XResult = Request.QueryString["res"];
TextBox1.Text = XResult.ToString();
}
else
{
//---- Handle with out query string

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 15:36pm    
Most detailed, my 5. (Yes, it looks like OP might appreciate such level of detail.)
--SA
TweakBird 4-Mar-11 15:45pm    
Thank you SA.
Perhaps res is not being set to anything in the request (URL with a '?'), so your XResult is never set to anything?
 
Share this answer
 
Comments
wizardzz 4-Mar-11 16:20pm    
Why the heck did I get a 3? I posted this answer first, and other answers that provided the same logic got a 5, and came after mine.
#realJSOP 5-Mar-11 13:24pm    
Sometimes that happens. I think all of us answered at about the same time, and when that happens, the highest rated user's answer is listed first, so it's the one the user sees first. It's happened to me when Nish and I answer at the same time - his response is listed before mine. Once voting starts to happen, though, the order changes to the highest rated until the OP marks one as answered, and then the other sorting starts kicking in again. I don't know why yours was 3-voted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900