Sepehr Estaki
Sepehr Estaki

Reputation: 351

MVC C# ExecuteReader requires an open and available Connection. The connection's current state is open. error

I have added this to my MVC RouteConfig:

routes.MapRoute(
name: "categories",
url: "{sub}",
defaults: new { controller = "cat", action = "Index" }
, constraints: new { sub = new MyCatConstraint() }
);

and this is my MyCatConstraint Class:

 public class MyCatConstraint : IRouteConstraint
    {



        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
             KhanoumiDbContext db = new KhanoumiDbContext();



            if (values.ContainsKey(parameterName))
            {
                string thisCat = values["sub"].ToString();

                    return db.tbl_Category.Any(c => c.Cat_Name_En == thisCat);


            }
            return false;
        }

    }

After add these sometimes I get this error :

ExecuteReader requires an open and available Connection. The connection's current state is open.

would you please tell me what should I do to solve this issue ?

Upvotes: 0

Views: 2221

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Wrap KhanoumiDbContext db = new KhanoumiDbContext() inusing statement.

Upvotes: 2

Related Questions