Hi Guys,
I have created small test web application which makes use of LINQ to SQL. I have ObjectDataSource and GridView. GridView's Datasource is ObjectDataSource. Now this ObjectDataSource uses one class(Method Name:GetAllTasks() as mentioned below) called MyTasks to populate all the task from Tasks table in SQL using Linq to SQL. It makes call to stored procedure GetAllMyTasks().
I have following method which works perfectly.
public static IEnumerable<GetAllMyTasksResult> GetAllTasks()
{
MyTasksDataContext db = new MyTasksDataContext();
var tasks = db.GetAllMyTasks().Select(x => x);
return tasks;
}
Now if i try to replace above code with following code just to make use of using key word to create Disposable MyTasksDataContext object. it gives me error saying "Invalid attempt to call Read when reader is closed.". Is there anything which i am missing here.
public static IEnumerable<GetAllMyTasksResult> GetAllTasks()
{
using (MyTasksDataContext db = new MyTasksDataContext())
{
var tasks = db.GetAllMyTasks().Select(x => x);
return tasks;
}
}
Can anyone please give me reason behind this? I know that my MyTasksDataContext object is calling its dispose method.