Click here to Skip to main content
11,930,222 members (40,095 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked

Change The Default CommandTimeout of LINQ DataContext

, 22 May 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An article that shows a simple way of changing the default value of the DataContext CommandTimeout


At times, the execution of a LINQ to SQL query may take a longer time and exceed the default value of the DataContext class's CommandTimeout property. The following article will present a way to achieve an application-wide CommandTimeout for your DataContext class.

The Problem

LINQ to SQL codes that invoke a long running database query/stored procedure end up with a System.Data.SqlClient.SqlException: Timeout expired Exception.

The Solution

The default value of the DataContext class's CommandTimeout is set to 30 seconds. Any database queries taking a longer time to complete than 30 seconds will throw a System.Data.SqlClient.SqlException: Timeout expired Exception.

One solution to this problem is to set the value of the CommandTimeout before each time a LINQ to SQL DataContext object is created and such a query is invoked. But the problem with this approach is, it will introduce code duplication and related issues.

We actually want to be able to change the default value of the CommandTimeout property and we want to do it efficiently. Fortunately enough, the Visual Studio 2008 auto generated DataContext subclasses provide an easy way to achieve this target using extensibility through partial methods.

The C# Code

Usually, an auto generated DataContext subclass has a few partial method declarations like the following:

#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
//Other DataContext specific methods

We are interested in the method called OnCreated() here. If you take a look into the constructors of the DataContext subclass, you will notice that this partial method is called from all the overloaded versions of the constructors in the end.

For those who are new to Partial Methods, please see this MSDN documentation. For a brief, partial methods are defined as extensibility points in a class. So, if there is no definition other than the declaration for that partial method [in another partial class], the call is just ignored. However, if there is an implementation, it will be invoked as a regular method.

So, we are going to define the body of the OnCreated() method in another partial class with the same full name as that of the auto generated class and set the desired timeout value there in the following way:

partial class SampleDBDataContext : System.Data.Linq.DataContext
    partial void OnCreated()
        //Put your desired timeout here.
        this.CommandTimeout = 3600;

        //If you do not want to hard code it, then take it 
        //from Application Settings / AppSettings
        //this.CommandTimeout = Settings.Default.CommandTimeout;

This is it! This is simple because:

  1. It remains so silent that the clients of the DataContext class do not need to take care of the CommandTimeout value.
  2. The change is only at a single place, which means no code duplication.
  3. Placing this method in a separate file in a partial class makes sure this change will persist although the auto generated class may be overwritten when a change takes place in the DBML file.


  • 22nd May, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Other ThoughtWorks
Canada Canada
Read my blog at

You may also be interested in...

Comments and Discussions

GeneralAlso solves this error message... Pin
t8r26-Jun-15 9:35
membert8r26-Jun-15 9:35 
QuestionGood One Pin
Tittle Joseph14-Dec-13 8:54
memberTittle Joseph14-Dec-13 8:54 
QuestionNice One! Pin
Nitesh Luharuka20-Sep-12 6:13
memberNitesh Luharuka20-Sep-12 6:13 
GeneralMy vote of 5 Pin
peteSJ16-Mar-12 9:19
memberpeteSJ16-Mar-12 9:19 
GeneralGood Job Pin
shahwolf18-Jan-12 13:53
membershahwolf18-Jan-12 13:53 
Generalnice one Pin
lollypop5517-Oct-11 15:45
memberlollypop5517-Oct-11 15:45 
GeneralMy vote of 5 Pin
maniurzy5-Jul-11 10:20
membermaniurzy5-Jul-11 10:20 
GeneralMy vote of 5 Pin
Cajetan D'souza12-Apr-11 6:48
memberCajetan D'souza12-Apr-11 6:48 
GeneralGreat work Pin
Member 475968512-Apr-11 6:37
memberMember 475968512-Apr-11 6:37 
GeneralMy vote of 5 Pin
Win Thu31-Oct-10 18:25
memberWin Thu31-Oct-10 18:25 
GeneralAwesome! Pin
dirq26-Oct-10 6:15
memberdirq26-Oct-10 6:15 
GeneralRe: Awesome! Pin
S. M. SOHAN26-Oct-10 6:30
memberS. M. SOHAN26-Oct-10 6:30 
GeneralSQLClient command timeout Pin
BondRidhoo11-May-10 2:40
memberBondRidhoo11-May-10 2:40 
GeneralCommandTimeout type Pin
jeremy.wiebe16-Mar-10 4:38
memberjeremy.wiebe16-Mar-10 4:38 
GeneralRe: CommandTimeout type Pin
S. M. SOHAN16-Mar-10 5:30
memberS. M. SOHAN16-Mar-10 5:30 
GeneralHelpful Pin
bigRahn14-Jan-10 9:30
memberbigRahn14-Jan-10 9:30 
GeneralRe: Helpful Pin
S. M. SOHAN14-Jan-10 10:11
memberS. M. SOHAN14-Jan-10 10:11 
GeneralNice one, thanks Pin
bakirankumar10-Dec-09 5:10
memberbakirankumar10-Dec-09 5:10 
GeneralNice. Pin
leforgeyahoo3-Feb-09 5:16
memberleforgeyahoo3-Feb-09 5:16 
GeneralRe: Nice. Pin
S. M. SOHAN3-Feb-09 5:51
memberS. M. SOHAN3-Feb-09 5:51 
GeneralRe: Nice. Pin
leforgeyahoo3-Feb-09 7:17
memberleforgeyahoo3-Feb-09 7:17 
GeneralRe: Nice. Pin
SLaxman19-Aug-09 8:28
memberSLaxman19-Aug-09 8:28 
GeneralRe: Nice. Pin
SLaxman19-Aug-09 9:27
memberSLaxman19-Aug-09 9:27 
GeneralNice one... thanks Pin
PCoffey2-Feb-09 8:46
memberPCoffey2-Feb-09 8:46 
GeneralRe: Nice one... thanks Pin
S. M. SOHAN3-Feb-09 1:33
memberS. M. SOHAN3-Feb-09 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 22 May 2008
Article Copyright 2008 by S. M. SOHAN
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid