Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.32/5 (5 votes)
See more:
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class searchresults : System.Web.UI.Page
{


    SqlConnection cn;
    SqlDataAdapter da;
    DataSet ds;
    DataView dv;
    protected void Page_Load(object sender, EventArgs e)
    {
        fun();
    }

    public void fun()
    {
        cn = new SqlConnection(ConfigurationManager.AppSettings["sree"]);
        da = new SqlDataAdapter(" select * from products where product type='" + Session["category"].ToString() + "' and availcity='" + Session["city"].ToString() + "'", cn);
        da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        SqlCommandBuilder cmb = new SqlCommandBuilder(da);
        ds = new DataSet();
        da.Fill(ds, "products");
        dv = new DataView(ds.Tables["products"]);
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        fun();
    }
}


output...............

Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 27:     {
Line 28:         cn = new SqlConnection(ConfigurationManager.AppSettings["sree"]);
Line 29:         da = new SqlDataAdapter(" select * from products where product type='" + Session["category"].ToString() + "' and availcity='" + Session["city"].ToString() + "'", cn);
Line 30:         da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
Line 31:         SqlCommandBuilder cmb = new SqlCommandBuilder(da);


Source File: c:\Users\deepak\Documents\Visual Studio 2012\RENTAL SYSTEM code\searchresults.aspx.cs    Line: 29

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   searchresults.fun() in c:\Users\deepak\Documents\Visual Studio 2012\RENTAL SYSTEM code\searchresults.aspx.cs:29
   searchresults.Page_Load(Object sender, EventArgs e) in c:\Users\deepak\Documents\Visual Studio 2012\RENTAL SYSTEM code\searchresults.aspx.cs:23
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927
Posted
Updated 23-Dec-19 2:09am
v2
Comments
[no name] 29-May-13 15:28pm    
One or both of your session variables are null.
Sergey Alexandrovich Kryukov 29-May-13 15:28pm    
OK, and then comment these lines 29 and 627, so we could see where where the exception is thrown and where it propagated.

(Yes, now I see you indicated it, but it's better to add a comment to a single code sample.)
—SA

Session["category"]
first you have to check session variable null case then convert to ToString();
If Null Of Session While you are converting the Tostring() its cause the issue
so first check the Null the convert
 
Share this answer
 
Comments
Dave Kreskowiak 23-Dec-19 13:12pm    
You do realize this is 6 years old and already had 4 other answers on it that, one of which was far more descriptive than yours...
In addition to the Solution 1 and Solution 2:

This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Instead of asking the question each time you faced with this very common problem, it would be much better to master the simple methods of digging into it to locate it. This is pretty easy, as you could see.

Good luck,
—SA
 
Share this answer
 
Comments
Absolutely correct. My +5.
Nice and clear idea for the OP, what he/she really needs to do.
Sergey Alexandrovich Kryukov 29-May-13 15:45pm    
I hope so, let's see. Thank you, Tadit.
—SA
Most Welcome... :)
My guess: Session is null... if not, then Session["category"] or Session["city"].

Could you check them?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-May-13 15:33pm    
My 5, but please also see the Solution 3. It's better to OP to master digging into such things.
—SA
Check whether you have a AppSettings key defined for "sree" inside web.config or not.

You must be missing that, that's why it is throwing the error on the below line.
C#
ConfigurationManager.AppSettings["sree"]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-May-13 15:33pm    
My 5, but please also see the Solution 3 (I think you already saw the main part if it). It's better to OP to master digging into such things.
—SA
Thanks Sergey Alexandrovich Kryukov... :)
naveen1211 29-May-13 15:38pm    
it is defined
Ok. so now you should follow your code step by step by putting debugger.

Just put debugger on function "fun" and go step by step by clicking the key F10 and see where exactly you are getting the exception.
Sergey Alexandrovich Kryukov 29-May-13 15:48pm    
OK, but you really had to check it up; it was the right idea. You can find ends step by step.
—SA

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