Click here to Skip to main content
15,173,640 members
Articles / Programming Languages / SQL
Posted 22 May 2008


25 bookmarked

Change The Default CommandTimeout of LINQ DataContext

Rate me:
Please Sign up or sign in to vote.
4.96/5 (19 votes)
22 May 2008CPOL2 min read
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

Comments and Discussions

GeneralAlso solves this error message... Pin
Tate Antrim26-Jun-15 9:35
professionalTate Antrim26-Jun-15 9:35 
QuestionGood One Pin
Tittle Joseph14-Dec-13 8:54
MemberTittle Joseph14-Dec-13 8:54 
QuestionNice One! Pin
Nitesh Kejriwal20-Sep-12 6:13
professionalNitesh Kejriwal20-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
Cajetan D'souza12-Apr-11 6:37
MemberCajetan D'souza12-Apr-11 6:37 
GeneralMy vote of 5 Pin
Win Thu31-Oct-10 18:25
MemberWin Thu31-Oct-10 18:25 
GeneralAwesome! Pin
dwatkins@dirq.net26-Oct-10 6:15
Memberdwatkins@dirq.net26-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
Kiran Bheemarti10-Dec-09 5:10
MemberKiran Bheemarti10-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.