title: How to prevent 'query timeout expired'? (SQLNCLI11 error '80040e31')
body: <p>I have a connection to a MS SQL Server 2012 database in classic ASP (VBScript). This is my connection string:</p>
  
  <pre class="lang-none prettyprint-override"><code>Provider=SQL Server Native Client 11.0;Server=localhost;
  Database=databank;Uid=myuser;Pwd=mypassword;
  </code></pre>
  
  <p>When I execute this SQL command:</p>
  
  <pre class="lang-sql prettyprint-override"><code>UPDATE [info] SET [stamp]='2014-03-18 01:00:02',
  [data]='12533 characters goes here',
  [saved]='2014-03-18 01:00:00',
  [confirmed]=0,[ip]=0,[mode]=3,[rebuild]=0,
  [updated]=1,[findable]=0 
  WHERE [ID]=193246;
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>Microsoft SQL Server Native Client 11.0 
  error '80040e31'
  Query timeout expired   
  /functions.asp, line 476
  </code></pre>
  
  <p>The SQL query is pretty long, the data field is updated with 12533 characters. The ID column is indexed so finding the post with ID 193246 should be fast.</p>
  
  <p><strong>When I execute the exact same SQL expression (copied and pasted) on SQL Server Management Studio it completes successfully in no time. No problem what so ever.</strong> So there isn't a problem with the SQL itself. I've even tried using a ADODB.Recordset object and update via that (no self-written SQL) but I still get the same timeout error.</p>
  
  <p>If I go to Tools > Options > Query Execution in the Management Studio I see that execution time-out is set to 0 (infinite). Under Tools > Options > Designers I see that transaction time-out is set to 30 seconds, which should be plenty enough since the script and database is on the same computer ("localhost" is in the connection string).</p>
  
  <p>What is going on here? Why can I execute the SQL in the Management Studio but not in my ASP code?</p>
  
  <hr>
  
  <p><strong>Edit:</strong> Tried setting the 30 sec timeout in the Designers tab to 600 sec just to make sure, but I still get the same error (happens after 30 sec of page loading btw).</p>
  
  <p>Here is the code that I use to execute the SQL on the ASP page:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Set Conn = Server.CreateObject("ADODB.Connection")
  Conn.Open "Provider=SQL Server Native Client 11.0;
  Server=localhost;Database=databank;Uid=myuser;Pwd=mypassword;"
  Conn.Execute "UPDATE [info] SET [stamp]='2014-03-18 01:00:02',
  [data]='12533 characters goes here',[saved]='2014-03-18 01:00:00',
  [confirmed]=0,[ip]=0,[mode]=3,[rebuild]=0,[updated]=1,[findable]=0 
  WHERE [ID]=193246;"
  </code></pre>
  
  <hr>
  
  <p><strong>Edit 2:</strong> Using <code>Conn.CommandTimeout = 0</code> to give infinite execution time for the query does nothing, it just makes the query execute forever. Waited 25 min and it was still executing.</p>
  
  <p>I then tried to separate the SQL into two SQL statements, the long data update in one and the other updates in the other. It still wouldn't update the long data field, just got timeout.</p>
  
  <p>I tried this with two additional connection strings:</p>
  
  <pre class="lang-none prettyprint-override"><code>Driver={SQL Server};Server=localhost;Database=databank;Uid=myuser;Pwd=mypassword;
  Driver={SQL Server Native Client 11.0};Server=localhost;Database=databank;Uid=myuser;Pwd=mypassword;
  </code></pre>
  
  <p>Didn't work. I even tried changing the data to 12533 A's just to see if the actual data was causing the problem. Nope, same problem.</p>
  
  <p>Then I found out something interesting: I tried to execute the short SQL first, before the long update of the data field. It ALSO got query timeout exception...</p>
  
  <p>But why? It has so little stuff to update in it (the whole SQL statement is less than 200 characters). Will investigate further.</p>
  
  <hr>
  
  <p><strong>Edit 3:</strong> I thought it might have been something to do with the login but I didn't find anything that looked wrong. I even tried changing the connection string to use the sa-account but even that didn't work, still getting "Query timeout expired".</p>
  
  <p>This is driving me mad. There is no solution, no workaround and worst of all no ideas!</p>
  
  <hr>
  
  <p><strong>Edit 4:</strong> Went to Tools > Options > Designers in the Management Studio and ticked off the "Prevent saving changes that require table re-creation". It did nothing.</p>
  
  <p>Tried changing the "data" column data type from "nvarchar(MAX)" to the inferior "ntext" type (I'm getting desperate). It didn't work.</p>
  
  <p>Tried executing the smallest change on the post I could think of:</p>
  
  <pre class="lang-sql prettyprint-override"><code>UPDATE [info] SET [confirmed]=0 WHERE [ID]=193246;
  </code></pre>
  
  <p>That would set a bit column to false. Didn't work. I tried executing the exact same query in the Management Studio and it worked flawlessly.</p>
  
  <p>Throw me some ideas if you have got them because I'm running out for real now.</p>
  
  <hr>
  
  <p><strong>Edit 5:</strong> Have now also tried the following connection string:</p>
  
  <pre class="lang-none prettyprint-override"><code>Provider=SQLOLEDB.1;Password=mypassword;Persist Security Info=True;User ID=myuser;Initial Catalog=databank;Data Source=localhost
  </code></pre>
  
  <p>Didn't work. Only tried to set confirmed to false but still got a time out.</p>
  
  <hr>
  
  <p><strong>Edit 6:</strong> Have now attempted to update a different post in the same table:</p>
  
  <pre class="lang-sql prettyprint-override"><code>UPDATE [info] SET [confirmed]=0 WHERE [ID]=1;
  </code></pre>
  
  <p>It also gave the timeout error. So now we know it isn't post specific.</p>
  
  <p>I am able to update posts in other tables in the same "databank" database via ASP. I can also update tables in other databases on localhost.</p>
  
  <p>Could there be something broken with the [info] table? I used the MS Access wizard to auto move data from Access to MS SQL Server 2012, it created columns of data type "ntext" and I manually went and changed that to "nvarchar(MAX)" since ntext is deprecated. Could something have broken down? It did require me to re-create the table when I changed the data type.</p>
  
  <p>I have to get some sleep but I will be sure to check back tomorrow if anybody has responded to me. Please do, even if you only have something encouraging to say.</p>
  
  <hr>
  
  <p><strong>Edit 7:</strong> Quick edit before bed. Tried to define the provider as "SQLNCLI11" in the connection string as well (using the DLL name instead of the actual provider name). It makes no difference. Connection is created just as fine but the timeout still happens.</p>
  
  <p>Also I'm not using MS SQL Server 2012 Express (as far as I know, "Express" wasn't mentioned anywhere during installation). It's the full thing.</p>
  
  <p>If it helps, here's the "Help" > "About..." info that is given by the Management Studio:</p>
  
  <pre class="lang-none prettyprint-override"><code>Microsoft SQL Server Management Studio: 11.0.2100.60  
  Microsoft Analysis Services Client Tools: 11.0.2100.60  
  Microsoft Data Access Components (MDAC): 6.3.9600.16384  
  Microsoft MSXML: 3.0 5.0 6.0   
  Microsoft Internet Explorer: 9.11.9600.16521  
  Microsoft .NET Framework: 4.0.30319.34011  
  Operating System: 6.3.9600
  </code></pre>
  
  <hr>
  
  <p><strong>Edit 8 (also known as the "programmers never sleep" edit):</strong></p>
  
  <p>After trying some things I eventually tried to close the database connection and reopening it right before executing the SQL statements. It worked all of a sudden. What the...?</p>
  
  <p>I have had my code inside a subroutine and it turns out that outside of it the post that I was trying to update was already opened! So the reason for the timeout was that the post or the whole table <strong>was locked by the very same connection that tried to update it</strong>. So the connection (or CPU thread) was waiting for a lock that would never unlock.</p>
  
  <p>Hate it when it turns out to be so simple after trying so hard.</p>
  
  <p>The post had been opened outside the subroutine by this simple code:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Set RecSet = Conn.Execute("SELECT etc")
  </code></pre>
  
  <p>I just added the following before calling the subroutine.</p>
  
  <pre class="lang-vb prettyprint-override"><code>RecSet.Close
  Set RecSet = Nothing
  </code></pre>
  
  <p>The reason why this never crossed my mind is simply because this was allowed in MS Access but now I have changed to MS SQL Server and it wasn't so kind (or sloppy, rather). The created RecSet by Conn.Execute() had never created a locked post in the database before but now all of a sudden it did. Not too strange since the connection string and the actual database had changed.</p>
  
  <p>I hope this post saves someone else some headache if you are migrating from MS Access to MS SQL Server. Though I can't imagine there are that many Access users left in the world nowadays.</p>