locked
already an open datareader RRS feed

  • Question

  • I get this error in Azure but not on my dev computer: there is already an open datareader associated with this command.

    When debugging Azure the error happens in a WCF RIA projet at this line (in a recursive method):

    				if (!channel.ChannelCollection.IsLoaded)
    					channel.ChannelCollection.Load();

    Have you an idea to resolve this exception?

    Thanks


    Yves Pflieger

    Thursday, April 16, 2015 2:57 PM

Answers

All replies

  • Can you show the code for the full method?

    Also, this may not apply but I am sharing it anyway, I have had to wrap the code in my method in this when I connected to more than one datasource:

    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) { ... put code here

    }



    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com


    Thursday, April 16, 2015 4:54 PM
  • My code here (WCf-RIA) running on Azure
    public class WCF_RIA_Service : DomainService
    	{
    		#region Database connection
    
    		private ApplicationData m_context;
    
    		public ApplicationData Context
    		{
    			get
    			{
    				if (m_context == null)
    				{
    					string connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["_IntrinsicData"].ConnectionString;
    
    					EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
    					builder.Metadata = "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
    					builder.Provider = "System.Data.SqlClient";
    					builder.ProviderConnectionString = connString;
    
    					this.m_context = new ApplicationData(builder.ConnectionString);
    				}
    				return this.m_context;
    			}
    		}
    		#endregion
    
    		public enum ChannelEnum { National = 1, Province = 2, HealthCarePlan = 3, Retail = 4 };
    
    
    		[Query(IsDefault = true)]
    		public IQueryable<FlatChannel> GetAllChannels()
    		{
    			int idIncrement = 1;
    			List<FlatChannel> liste = new List<FlatChannel>();
    
    			// pour chaque Channel de niveau National, je crée l'arbre correspondant
    			foreach (var item in this.Context.ChannelSet.Where(channel => channel.Level_ == 0))
    			{
    				ParcourtArbre(liste, item, ref idIncrement);
    			}
    
    			Context.Dispose();
    			return liste.AsQueryable<FlatChannel>();
    		}
    
    		[Query(IsDefault = false)]
    		public IQueryable<FlatChannel> GetSingleFlatChannelTree(int? idLOC)
    		{
    			try
    			{
    				int idIncrement = 1;
    				List<FlatChannel> liste = new List<FlatChannel>();
    
    				//this.Context.ContextOptions.LazyLoadingEnabled = false;
    
    				// Je cherche le Channel National (le père de l'arbre) correspondant au LOC désiré
    				ParcourtArbre(liste, this.Context.ChannelSet.Where(channel => channel.LOC.Id == idLOC).FirstOrDefault(), ref idIncrement);
    
    				Context.Dispose();
    				return liste.AsQueryable<FlatChannel>();
    
    			}
    			catch (Exception)
    			{
    				
    				throw;
    			}
    		}
    
    
    		private bool ParcourtArbre(List<FlatChannel> list, Channel channel, ref int idIncrement)
    		{
    			Contract.Requires(list != null);
    			Contract.Requires(channel != null);
    			Contract.Ensures(idIncrement == Contract.OldValue(idIncrement) + 1);
    			const char Espace = ' ';
    			bool succes = true;
    			if (channel == null)
    				return false;
    			else
    			{
    				// espace particulier pour être vraiment affiché
    				list.Add(new FlatChannel { ID = idIncrement++, 
    										   IDChannel = channel.Id, 
    										   IDLOC = channel.LOC_Channel,    // = channel.LOC.Id
    										   Name = new string(Espace, (int)channel.Level_ * 3) + channel.Name, 
    										   Type = channel.Type,
    										   Path = channel.Path_, 
    										   Level = channel.Level_ });
    
    				// Attention: channel.ChannelCollection est vide ainsi que channel.ChannelParent
    				if (!channel.ChannelCollection.IsLoaded)
    				{
    					channel.ChannelCollection.Load();
    				}
    
    				foreach (Channel chan in channel.ChannelCollection)
    				{
    					succes &= ParcourtArbre(list, chan, ref idIncrement);
    				}
    			}
    
    			if (!succes)
    				return false;
    			return succes;
    		}
    
    		// Override the Count method in order for paging to work correctly
    		protected override int Count<T>(IQueryable<T> query)
    		{
    			return query.Count();
    		}
    
    	}


    Yves Pflieger

    Notice (important)
    The error occurs when calling GetAllChannels() method but runs well when calling GetSingleFlatChannelTree(int? idLOC) !
    • Edited by Yves Pflieger Thursday, April 16, 2015 5:19 PM important detail
    Thursday, April 16, 2015 5:05 PM
  • The difference I see is that

    Context.Dispose();

    Is called right after the call to "ParcourtArbre" in GetSingleFlatChannelTree(int? idLOC) but it is not called each time it is run in GetAllChannels().

    Meaning, in GetAllChannels() you are calling "ParcourtArbre" multiple times in a loop. However, not sure if that is the cause...  


    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com

    Thursday, April 16, 2015 5:38 PM
  • If I put a Context.Dispose() in the foreach loop foreach (var item in this.Context.ChannelSet.Where(channel => channel.Level_ == 0)) I get a : Calling 'Read' when the data reader is closed is not a valid operation. error

    Yves Pflieger

    Thursday, April 16, 2015 5:53 PM
  • As the GetSingleFlatChannelTree method runs correctly on Azure, I can live without the GetAllChannels method. So we can forget this problem.

    Thanks, Yves


    Yves Pflieger

    Thursday, April 16, 2015 6:09 PM
  • Ok, but this may work:

    foreach (var item in this.Context.ChannelSet.Where(channel => channel.Level_ == 0).toList())


    Unleash the Power - Get the LightSwitch 2013 HTML Client / SharePoint 2013 book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Yves Pflieger Friday, April 17, 2015 8:48 AM
    Thursday, April 16, 2015 6:53 PM
  • Bingo. Good idea. The .ToList() was the right idea.Thanks again!

    Yves


    Yves Pflieger

    Friday, April 17, 2015 8:48 AM
  • You rock !! Same problem as Yves resolved in a minute !! Thank's for your amazing work Michael (great job with your book to ...) I hope lightswitch a long life ! Best wishes Frederic
    Monday, April 27, 2015 6:40 AM