I have two functions that execute two ajax calls. If I call one function and not the other the code executes normally. But if I call both of them at the same time I get two Internal Server errors messages. One for each function I presume.
This is my code:
$(document).ready(function(){
CategoryChangeState(@Model.CatId , subcategoryId);
SubategoryChangeState(@Model.SubcatId);
})
public ActionResult ReturnListOfSubcategories( FormCollection collection ) {
string categoryId = collection["result"];
var subcategories = ProductManagerHelperClass.ReturnSubcategories(categoryId);
return Json(subcategories);
}
public ActionResult ReturnListOfBrands() {
var brands = ProductManagerHelperClass.ReturnBrands();
return Json(brands);
}
function CategoryChangeState(value , editPage) {
.....
$.ajax({
type: "POST",
url: "/ProductManager/ReturnListOfSubcategories",
data: { result: value },
datatype: "json",
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
...
}
function SubategoryChangeState(value) {
....
$.ajax({
type: "POST",
url: "/ProductManager/ReturnListOfBrands",
datatype: "json",
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
....
}
Why am I getting these errors and how can I solve it?
EDIT
While debugging I discovered that in this portion of code:
public static Dictionary<string , string> ReturnSubcategories(string categoryId)
{
int catId = int.Parse(categoryId);
var subcategories = (from s in dataContext.SubCategories
where s.CatId == catId
select new
{
s.SubCatId,
s.SubCatName
}).ToDictionary(x => x.SubCatId.ToString(), x => x.SubCatName);
return subcategories;
}
The linq query throws an exception:
InvalidOperationException ExecuteReader requires an open and available Connection. The connection's current state is closed.
Again this exception is only thrown if I call both functions