How do I diagnose the Timeout of a SQL command called from my C# web service app

Posted on 2014-03-25
Last Modified: 2014-04-11
I am calling a stored proc in a MS SQL server database that, when run in SSMS, takes 12 seconds.  The SqlCommand object I am using to call the stored proc times out when I call from my C# webservice.  I have up'd the CommandTimeout to 120 seconds but get the same result....

SqlCommand cmd = new SqlCommand(procName, conn) {CommandType = CommandType.StoredProcedure, CommandTimeout = 120};
SqlDataAdapter sda = new SqlDataAdapter(cmd);

Open in new window

How do I go about determining the bottleneck?  

Again, this proc will execute to completion in SSMS in 12 seconds.

Question by:Bruce
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 125 total points
ID: 39954160
First thing I would do is fire up SQL Server Profiler and make sure that what you think your code is sending to the database is actually what your code is sending to the database.
LVL 40

Accepted Solution

lcohan earned 375 total points
ID: 39954165
"Again, this proc will execute to completion in SSMS in 12 seconds."
How much data it returns? could this be the issue?

"How do I go about determining the bottleneck?  "
I suggest you use SQL Profiler to trace any call to that SQL Sproc.

Author Comment

ID: 39954192
It returns just two rows of 12 columns of data.  Small about really...
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.


Author Comment

ID: 39954195
Why wouldn't a 2 minute command timeout eventually consume a 12 second SQL query?
LVL 40

Assisted Solution

lcohan earned 375 total points
ID: 39954215
"Why wouldn't a 2 minute command timeout eventually consume a 12 second SQL query? "

How can you be 100% sure that the "timeout" IS the actual error and not something else and...Are you sure is running against that exact Server database and doing what is supposed to?

Here - start your UI code and run a sp_who2 in a SQL SSMS query OR check SQL Activity Monitor in SSMS to see that indeed you are running that SP against that SQL Database.

just type the command below in a SQL SSMS query and check its results:

LVL 40
ID: 39954241
Do you have more code after what you showed us.

The SqlDataAdapter alone does not execute the command. You need to Fill a DataTable for it to execute the command:

DataTable tbl = New DataTable();

Author Comment

ID: 39956152
SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            return ds;

Open in new window

Per the mention above from lcohon I have have opened SSMS and did an sp_who2 while the Fill command was called...  I watch the Login that my app uses (unique in this environment) and the CPUTime just keeps counting up to the 240 sec timeout limit I set in the code.

I have tried to implement a SQLDataReader, thinking the Forward Only nature of that reader might reduce the overhead in returning the data.  There should be less than 1KB returned (See attached Clients Stats screenshot)  The Reader also times out.


Author Comment

ID: 39995009
Update: We had a similar issue with another SP being called from our web service app.  The sp would run from within SSMS in a short time, but when called from the service it would hang. (Verified in SQL Analyzer, processes would contiuously run (Task State RUNNING) in "SELECT INTO" command) We found that the web service was passing in an empty string as one of the parameters and it was not being handled well in the sp and that was causing the sp to timeout.

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question