Geetha Gubendran

Geetha Gubendran

  • NA
  • 11
  • 11.3k

System.InvalidOperationException Timeout expired

Jun 19 2014 9:51 AM
I have written a small app to test MVC. In my application, I created MVC project and used View and controller from the MVC project itself but for the model, I created a class library project and a class within that project will query the database using ADO.net and get the details and store it in an object and the object would be passed to MVC application.

In this application only, I am not able to fetch the data from the database. I used localdb from the sql server bundled with VS2013.

The exception that I got is as follows:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=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=System.Data
StackTrace:
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectiIonFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)

But when I created a separate console application and tested the database access. It works perfectly fine for the same credentials that I use for my MVC app . I get back the response from the database.

The code in my MVC app is

string CS = "Data Source=(localdb)\\Projects;Initial Catalog=Sample;Integrated Security=True";

SqlConnection connection = new SqlConnection(CS);

Console.WriteLine("Connection Test:" + connection.ToString());
SqlCommand cmd = new SqlCommand("Select Id, Name, Gender, City, DateOfBirth from tblEmployee", connection);
// cmd.CommandType = CommandType.StoredProcedure;
connection.Open();


SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.ID = Convert.ToInt32(rdr["ID"]);
employee.Name = rdr["name"].ToString();
employee.Gender = rdr["gender"].ToString();
employee.City = rdr["city"].ToString();
employee.DateOfBirth = Convert.ToDateTime(rdr["dateofbirth"]);

Employees.Add(employee);

}

connection.Close();