-1

This part give me a error something like this,

Object reference not set to an instance of an object.

here is the my code which raised when any changes happen on sql server.

     private void  dependency_OnDataChangedDelegate(object sender, SqlNotificationEventArgs e)
            {


System.Web.HttpContext.Current.Response.Redirect("Default.aspx");//raised error here
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= new OnChangeEventHandler(dependency_OnDataChangedDelegate);

            }

when I chech it with breakpoint,It can reach inside the dependency_OnDataChangedDelegate but give me exception.

sakir
  • 3,391
  • 8
  • 34
  • 50
  • Why are you redirecting before working on the dependency object? – Allan S. Hansen Mar 25 '14 at 08:21
  • my original question here.http://stackoverflow.com/questions/22592858/sqldependency-issue-with-the-asp-net . I want rephresh the data when any changes happened on sql server. – sakir Mar 25 '14 at 08:25
  • but even those codes gave error. – sakir Mar 25 '14 at 08:25
  • First of all move the `Response.Redirect` on the last line, and then try to use `"~/Default.aspx"`. And as @Aristos this code might not work, as it might not be triggered by a request. – Nilesh Mar 25 '14 at 08:50
  • nope still give erro nilesh,doent matter where it is decribe – sakir Mar 25 '14 at 09:00

1 Answers1

1

The "Object reference not set..." on this line

System.Web.HttpContext.Current.Response.Redirect

means that you call this function when the page is not available any more, probably you did not call this function from a page, but from a thread.

So the redirect did not know where to send their command, and this System.Web.HttpContext.Current is null, and you get that error.

Aristos
  • 66,005
  • 16
  • 114
  • 150