How to solve multiple active datareader session in mvc3 C#
Tech-Today

How to solve multiple active datareader session in mvc3 C#


This problem commonly happens when you try to execute a query with a return type of IEnumerable then execute a second query with the result.

Example, we have a table Animes:

public IEnumerable GetAnimes() {
return context.Animes;
}

//on the second part of the code we invoke GetAnimes and perform a second sql query;
var animes = GetAnimes(); //line 1
foreach(var anime in animes) { //line 2
context.Animes.Remove(context.Animes.FirstOrDefault(p=>p == anime)); //line 3
}

The above code will certainly fail. And might throw any of the ff errors:
1.) There is already an open DataReader associated with this Command which must be closed first.
2.) New transaction is not allowed because there are other threads running in the session
Why does this happen? It's because of deferred sql execution. Heard about that? If you think line 1 has already execute its query against the database you're wrong, it just prefer the query for execution. So when you try to execute line 3 it fails, because line 1 is still active.
To resolve this issue, make sure to close the first session before executing the next, by simply adding .ToList() call to line 2.
Also you might want to add MultipleActiveResultSets=True to the connectionString setting.




- How To Write Left Join Sql On Entity Framework
In every limitation there is always a work around. For this specific limitation on entity framework 3.5 not having a left join. This is what I came up: For example you want to: SQL: select * from contact a left join country c on a.contactid=c.contactid...

- Database Diagram Support Objects Cannot Be Installed Because This Database Does Not Have A Valid Owner
I encountered the error below when creating a diagram in the Management Studio. "Database diagram support objects cannot be installed because this database does not have a valid owner. To continue, first use the Files page of the Database Properties dialog...

- How To Get Started With Hibernate In Java. Create A Simple Class And Database Schema For Demostration.
Since, I've always been using different database sometimes I get confused how to implement the others. Like hibernate, where the configuration must be properly set. *This help assumes that you have already created a java project in eclipse that has...

- Mysql Engine: Myisam Vs Innodb
MyISAM (http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html) -to create a MyISAM table, add ENGINE = MYISAM; at the end of create table script -supports table locking (this is an issue when you need to backup the whole database) -fast execution...

- How To Add A Customize Product Admin Fields In Zencart By Editing/adding Extra Codes
Problem: How to add a product custom field named length. This to consider (modifications in): 1.) database 2.) scripts ----------------------------------- 1.) Database - a custom column should be add to the products table. Execute this line (for column...



Tech-Today








.