Linq to SQL NullReferenceException's: A random needle in a haystack!

Posted by Shane on Stack Overflow See other posts from Stack Overflow or by Shane
Published on 2010-04-13T22:26:55Z Indexed on 2010/04/13 22:33 UTC
Read the original article Hit count: 391

I'm getting NullReferenceExeceptions at seemly random times in my application and can't track down what could be causing the error.

I'll do my best to describe the scenario and setup.

Any and all suggestions greatly appreciated!

  • C# .net 3.5 Forms Application, but I use the WebFormRouting library built by Phil Haack (http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx) to leverage the Routing libraries of .net (usually used in conjunction with MVC) - intead of using url rewriting for my urls.

  • My database has 60 tables. All Normalized. It's just a massive application. (SQL server 2008)

  • All queries are built with Linq to SQL in code (no SP's). Each time a new instance of my data context is created. I use only one data context with all relationships defined in 4 relationship diagrams in SQL Server.

  • the data context gets created a lot. I let the closing of the data context be handled automatically. I've heard arguments both sides about whether you should leave to be closed automatically or do it yourself. In this case I do it myself.

    • It doesnt seem to matter if I'm creating a lot of instances of the data context or just one. For example, I've got a vote-up button. with the following code, and it errors probably 1 in 10-20 times.

      protected void VoteUpLinkButton_Click(object sender, EventArgs e)
      

      { DatabaseDataContext db = new DatabaseDataContext();

      StoryVote storyVote = new StoryVote();
      storyVote.StoryId = storyId;
      storyVote.UserId = Utility.GetUserId(Context);
      storyVote.IPAddress = Utility.GetUserIPAddress();
      storyVote.CreatedDate = DateTime.Now;
      storyVote.IsDeleted = false;
      
      
      db.StoryVotes.InsertOnSubmit(storyVote);
      db.SubmitChanges();
      
      
      // If this story is not yet published, check to see if we should publish it.  Make sure that
      // it is already approved.
      if (story.PublishedDate == null && story.ApprovedDate != null)
      {
          Utility.MakeUpcommingNewsPopular(storyId);
      }
      
      
      // Refresh our page.
      Response.Redirect("/news/" + category.UniqueName + "/"
          + RouteData.Values["year"].ToString() + "/"
          + RouteData.Values["month"].ToString() + "/"
          + RouteData.Values["day"].ToString() + "/"
          + RouteData.Values["uniquename"].ToString());
      

      }

The last thing I tried was the "Auto Close" flag setting on SQL Server. This was set to true and I changed to false. Doesnt seem to have done the trick although has had a good overall effect.

Here's a detailed that wasnt caught. I also get slighly different errors when caught by my try/catch's.

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Util.StringUtil.GetStringHashCode(String s) at System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) at System.Web.UI.WebControls.TextBox.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.forms_news_detail_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

HELP!!!

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about nullreferenceexception