The transaction is aborted or Failure while attempting to promote transaction.

This error is generally produced while you working on the  Linq query with Entity framework and there was some other system configurations like

Visual studio 2015.

Entity framework version 4.

C# 4.5

But while I checked the Inner exception I got the following message 
"There is already an open DataReader associated with this Command which must be closed first"

So in my case there was some WCF services and from the WCF services I was extracting the data by Linq query . This was code 

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 using (TransactionScope scope = new TransactionScope())
                {
                    using (CentralisedData_DataDataContext db = new                                                               CentralisedData_DataDataContext())
                    {
                         // Some code was there
                      }

                        db.SubmitChanges();

                        foreach (Lessons Date in result)
                                        Date.RefreshIDs();

                        foreach (var item in items)
                        {
                            foreach (var rep in (from rep in db.Teacher
                                            join ab in TeacherA on rep.TeacherAbsenceID equals                                                 ab.Id
                                            where ab.Date == item.Date
                                            select new { rep, ab }))
                            {
                                SetState();  // A method i was just calling
                            }
                        }
                    }
                    scope.Complete();
                }

1
2
3
4
5
Public Void SetState(){
using (CentralisedData_DataDataContext db = new CentralisedData_DataDataContext())
    {                         
 // Some code was there 
 }}
Cause :
  While we are crating one DBContext object in a transaction and inside the same transaction again       we are creating another DBcontext object . So the mean while same time its not possible to read on datareader while another is open .

Solutions:
    Solution 1:

     You can add this "MultipleActiveResultSets=True;" value in your connection string . So the complete connection string would be 

Solution 2 :

  Either you can send a DBconext object in the Parameter to the inner method if you calling any method from the main method .


Hope it will be help  . Please like this if it really help you and add your comments .

Comments

Post a Comment

Popular posts from this blog

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

Unable to load one or more breakpoints in Visual studio.