Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I inherited the below code:

C#
public partial class myWebForm : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
        var username = User.Identity.Name;

               SqlConnection MyConnection = new SqlConnection("server=myServer;database=myDatabase;Trusted_Connection=True;");

               SqlDataAdapter MyDataAdapter = new SqlDataAdapter("SP_CheckPermissions", MyConnection);

               MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

               MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@loginname", SqlDbType.VarChar, 40));

               MyDataAdapter.SelectCommand.Parameters["@loginname"].Value = (loginname);

               MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@loginrole", SqlDbType.VarChar, 40));

               MyDataAdapter.SelectCommand.Parameters["@loginrole"].Direction = ParameterDirection.Output;

               DataSet DS = new DataSet();

               MyConnection.Open();

               MyDataAdapter.Fill(DS, "LoginRole");

               Session.Add("Role", DS);

               string loginrole = null;

               loginrole = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();

               string role1 = "Librarian";
               string role2 = "Principals";
               string role3 = "Grade1";
               string role4 = "Grade2";
               string role5 = "Grade3";
               string role6 = "Guidance";


               if (loginrole == role1)
               {
                   Server.Transfer("Librarian.aspx", true);
               }
               else if (loginrole == role2)
               {
                   Server.Transfer("Principals.aspx", true);
               }
               else if (loginrole == role3)
               {
                   Server.Transfer("Grade1.aspx", true);
               }
               else if (loginrole == role4)
               {
                   Server.Transfer("Grade2.aspx", true);
               }
               else if (loginrole == role5)
               {
                   Server.Transfer("Grade3", true);
               }
               else if (loginrole == role6)
               {
                   Server.Transfer("Guidance.aspx", true);
               }

               else
               {
                   Server.Transfer("NewUserPage.aspx", true);
               }


               MyConnection.Close();
           }
       }


For some reason, I am now getting this timeout error message:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.]
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +6372097
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +6372430
   System.Data.SqlClient.SqlConnection.Open() +300
   MonogramFoods.WebForm1.Page_Load(Object sender, EventArgs e) +549
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

 This only happened when I added the Guidance.aspx page.  The code behind that page looks like this:

public partial class Guidance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                var username = User.Identity.Name;

            }


        }

    }


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1031 


Other than including a specific number of connection tries when I connect to my database, what's causing this error by just adding an additional landing page to this code?

What I have tried:

Google search, msdn search, various blogs, debugging the code
Posted
Updated 23-Jan-17 10:34am

1 solution

Wrap your SqlConnection and SqlDataAdapter objects in using blocks to ensure that they always get cleaned up properly:
using (SqlConnection MyConnection = new SqlConnection("server=myServer;database=myDatabase;Trusted_Connection=True;"))
using (SqlDataAdapter MyDataAdapter = new SqlDataAdapter("SP_CheckPermissions", MyConnection))
{
    ...
}

As it stands, the Server.Transfer call executes, and terminates the current thread before the MyConnection.Close call, which leaves the connection open. The using block will ensure that the connection is always closed.

Also, you don't need to call Open / Close when you're using a DataAdapter.

And you should look at storing your connection string in the web.config file, so that you don't have to recompile your code to change it.
How to: Read Connection Strings from the Web.config File[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900