0

I have a very unusual problem. I have an ASP.NET application that uses LINQ-to-SQL a lot, and works fine on most servers. However the application exists on another server on the same network, and after a recent update, whenever I open a new DataContext like this:

LINQDataContext dc = new LINQDataContext();

The dc object is null, so I get object reference errors trying to use it. I cannot replicate this on my development machine, or on the other server which the application exists on, so I'm baffled about why this could be. Any ideas?

Chris
  • 7,415
  • 21
  • 98
  • 190

2 Answers2

1

Something like this can't happen. When you call the new operator on a standard .Net type, there are two possible outcomes:

  1. the constructor returns normally and the value is assigned
  2. the constructor throws an exception, the value is not assigned

Unless you catch the exception right away, and ignore it, you can be sure that the variable has been assigned. This means your problem probably lies somewhere else.

(Technically, there is a third option – the constructor never returns and loops infinitely, but that's not relevant here.)

svick
  • 236,525
  • 50
  • 385
  • 514
  • Actually *in a deliberately crafted* edge case you can make "new" return null for a class, but you can't do it for a data-context. Only mentioned for the insanity of it (it doesn't apply to this question). Ill dig out the example if you want ;) edit: it is here: http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net/ – Marc Gravell Aug 07 '11 at 10:06
1

Since you are using default constructor of your DataContext, then connection string is read from web.config. So we could make a suggestion that application on that server has different config without proper connection string. But there is one more thing that we've faced once in our project. In a new version of ASP location of connection string storage in config file has changed. I don't have code at my fingertips, but you could test my suggestion by using constructor that takes connection string as a parameter. Read connection string from config manually or just hardcode it for testing purpose.

artplastika
  • 1,972
  • 2
  • 19
  • 38