Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:28:02+00:00 2026-05-28T06:28:02+00:00

I have some strange behavior occurring in an ASP.NET application that I am trying

  • 0

I have some strange behavior occurring in an ASP.NET application that I am trying to fix.

I am getting the following error from code:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

I am familiar with this error and many of it’s causes. All of my typical avenues of trouble shooting have failed.

Here are some of the dynamics:

The server was recently re-built. So it could be a server configuration issue.

This error only happens on a specific web server. When I run the application it locally, and from other servers it is fast. I cannot re-pro perf issues with the proc in MSSMS from my machine.

  • This tells me that it is specific to this server.

When I run The same proc using the OSql command–line utility It works. Fast.

  • This indicates that it is likely something .NET related, NOT db related

Prior to this code executing, I have executed other Stored procedures on this server, and on this same DB.

  • This suggests the proc may be related , but is not a “server X cannot talk to server Y”

I have gotten confirmation From DB owners that the DB has received the command, executed (>1second )it and returned the data.

  • By tracing the code, I see the results are returned and it is not until I try to close the data reader that the error occurs.
  • In fact I see it takes 36 milliseconds to execute the stored procedure and iterate through the result.

It looks like the call to DataReader.Close is what is taking time, and eventually timing out.

I have increased the max Pool from 31 to 100.

Here is a sample of what my code looks like, how it is structured. I have been hacking at it for trouble shooting: I have added the explict close to ensure I know where the error occurs. There may be syntax issues: I have made it generic, and may have introduced bugs in doing so.

    public double  GetMyData()
    {
        double returnValue;
       // Used in logging to see if code was reached & how long it took.
      System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
            using (SqlConnection cn = Connections.GetSqlConnection())
        {
            cn.Open();
            using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
            {

                Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                s.Start();
                SqlDataReader dr= null;
                try
                {
                     dr = cmd.ExecuteReader();
                     s.Stop();
                     Log.Log.WriteTrace("Timer", "ExecuteReader done " + s.ElapsedMilliseconds + "ms ", 0);
                     s.Start();


                    if (dr != null)
                    {
                        if (dr.Read())
                        {
                            returnValue =
                                Conversion.DBNullToDouble(
                                    dr[0]);
                        }
                        s.Stop();
                        Log.Log.WriteTrace("Timer", "dr.read done  (result:" + returnValue + ")" + s.ElapsedMilliseconds + "ms ", 0); // I get Here
                    }
                }catch(Exception ex)
                {
                    Log.Log.PersistException(ex);
                }
                //}
                if(dr!=null && !dr.IsClosed)
                    dr.Close();// This times out
                if (cn != null && cn.State !=ConnectionState.Closed)
                        cn.Close();
                Log.Log.WriteTrace("DONE "),
                                 ;
            }
        }
            return (returnValue);
    }

UPDATE

dr.Close(); takes 2 minutes to execute. Just on this server. Locally it takes less than a second.

UPDATE

Per the accepted answer’s comments: I have a proc that has multiple records. I am taking the fist one. Calling cmd.Cancel() has not fixed, but has drastically reduced the time taken to close the data reader. Exploring this should help me fix the problem. I do not know why this is only happening on this server, as the server is a dev server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:28:03+00:00Added an answer on May 28, 2026 at 6:28 am

    I see few problems in your code.

    1. Try to use the Reader in a using statement it closes and disposes when finishing it.
    2. You are closing 2 times the connection (when using

      if (cn != null && cn.State !=ConnectionState.Closed)
      cn.Close();

      and in the using (SqlConnection cn = Connections.GetSqlConnection()) — this one do it for you at the end of the statement

    3. In your code you are not checking if the DataReader.HasRows So if your Sproc doesn’t return a value it will throw an exception in the if (dr.Read()) so that could be the reason that’s why you are getting sometimes the time out exception

    4. If you only want to retrieve the value of the first column of the first row you should take a look to ExecuteScalar

    Finally
    I would rewrite your code like this (using DataReader)

    public double  GetMyData()
        {
            double returnValue;
           // Used in logging to see if code was reached & how long it took.
          System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
                using (SqlConnection cn = Connections.GetSqlConnection())
            {
                cn.Open();
                using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
                {
    
                    Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                    s.Start();
                    using(SqlDataReader dr = cmd.ExecuteReader())
                    {
    
    
                         s.Stop();
                         Log.Log.WriteTrace("Timer", "ExecuteReader done " + s.ElapsedMilliseconds + "ms ", 0);
                         s.Start();
    
    
                        if (dr != null)
                        {
                            if(dr.HasRows)
                            {
                               if (dr.Read())
                               {
                                  returnValue =
                                    Conversion.DBNullToDouble(
                                        dr[0]);
                               }
                            }
                            s.Stop();
                            Log.Log.WriteTrace("Timer", "dr.read done  (result:" + returnValue + ")" + s.ElapsedMilliseconds + "ms ", 0); // I get Here
                        }
                    }             
                }
            }
                return (returnValue);
        }
    

    Or (with ExecuteScalar)

    public double  GetMyData()
            {
                double returnValue;
               // Used in logging to see if code was reached & how long it took.
              System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); 
                    using (SqlConnection cn = Connections.GetSqlConnection())
                {
                    cn.Open();
                    using (SqlCommand cmd = getSQLCommmand("SomeProcName"))
                    {
    
                        Log.Log.WriteTrace(string.Format("Execute {0} ","SomeProcName"),0);
                        s.Start();
                        try
                        {
                             returnValue = Conversion.DBNullToDouble(cmd.ExecuteScalar());
                             s.Stop();  
                        }   
                        catch(Exception ex)
                        {
                            Log.Log.PersistException(ex);
                        }
    
                    }
                }
                    return (returnValue);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting some strange behavior involving database queries that I have never seen
I'm getting some strange behavior from a listview/the getChildAt method. I have a HashSet,
I've come across some strange behavior trying to get files that start with a
I am getting some strange behavior when trying to convert between Files and URLs,
I have some new strange Eclipse behavior from yesterday. When I want to run
I've experienced rather strange behavior of JSTL forEach tag. I have some bean called
I've been encountering some strange behavior when trying to find a key inside a
I am trying out the (latest) Android SDK, and noticed some strange behavior. I've
DataPager has some strange behavior. So to situate the problem, I have a DataPagerReapeater
I am experiencing some strange behavior with SQL Server CE 3.5 SP2. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.