I have a website which from time to time, loses access to the database. The website still runs on the IIS, and I can hit the front page without problems.

I haven't encountered this error in development or test, only in live. 

> 
> My first logged exception, seems always to be a InvalidCastException, with the following stacktrace.
> 
> 
> 

```
06-03-2013 09:06:26 
The exception was: Specified cast is not valid.
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at Read_Question(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Data.Linq.EntityRef`1.get_Entity()
at DB.TempQuestion.get_Question() in xxxx\DBLinq.designer.cs:line 9808
at xxxx.TestController.BuildBarHelperForPersonAssignment(PersonAssignment pa) in xxxx\TestController.cs:line 751
at xxxx.TestController.BuildMovieModel(TempMovie tm, PersonAssignment pa) in xxxx\TestController.cs:line 792
at xxxx.TestController.ShowMovie(TempMovie tm, PersonAssignment pa) in xxxxx\TestController.cs:line 636
at xxxx.TestController.Index(String id, FormCollection form, Nullable`1 currentid, String type, Int32[] selectedTest) in xxxx\TestController.cs:line 313

```

After this I get all sorts of InvalidCastExceptions and "The server failed to resume the transaction" errors.

It's pretty clear where the error happens. My issue is why this is happening and why it causes the application to lose access to the database?

I think the error is somewhere before all of this.

> 
> My application setup:
> 
> 
> 

ASP.NET MVC2, LINQ2SQL, MsSQL Server 2008 R2, IIS 7.5 (ASP 2.0)

> 
> My Controller setup:
> 
> 
> 

I fear i need to redesign my controllers, they pretty much all look like this.

```
[Authorize(Roles = "xxxxx")]
Public class ABController : Controller
{
    private readonly DBLinqDataContext _db = new DBLinqDataContext();

    public ActionResult Index()
    {
        ...
    }
}

```

Could this error happen because of not-closed DBLinqDataContext's?

Could adding the following overridden method maybe help?

```
    protected override void Dispose(bool disposing)
    {
        _db.Dispose();
        base.Dispose(disposing);
    }

```

Let me know if you need more information!