System.ObjectDisposedException: Cannot access a closed resource set

Last post 07-02-2009 10:53 AM by jasscat. 0 replies.

Sort Posts:

  • System.ObjectDisposedException: Cannot access a closed resource set

    07-02-2009, 10:53 AM
    • Member
      14 point Member
    • jasscat
    • Member since 12-02-2008, 10:01 AM
    • Posts 25

    I have a local resource file in my application that I'm accessing using strongly typed properties created with the StronglyTypedResourceBuilder.  I access this resource file a couple of different ways.

    At first, I'm creating a DataTable of resource keys/values like this (using the strongly typed Resource Manager property of the resource class) :

    IDictionaryEnumerator _reader = resourceset.GetEnumerator();
                
    while (_reader.MoveNext())
        
    {
                    DataRow dr
    = dt.NewRow();
                    
    dr["Name"] = _reader.Key as string;
                    
    dr["Value"] = _reader.Value as string ?? string.Empty;
                    
    dt.Rows.Add(dr);
        
    }
                resourceset.Close
    ();

    Later, I'm accessing an individual resource property like this:

     MyImage.ImageUrl = MyResourceClass.MyResourceProperty;
    When I access the individual resource property, I get the following error:

    System.ObjectDisposedException: Cannot access a closed resource set
    Obviously, the thing to try is to remove the "resourceset.Close()" statement from the first statment, and that does the trick. But the code is based
     on code examples I've found in various sources, and they all have a while loop followed by a close.

    My guess is that the resource class handles things a bit differently than some examples I've seen (which may have been based
    on pre-Asp .Net 2.0 methods to access resources). From what I can tell, in Asp .Net 2.0 and later, a resource manager is allocated
    for each resource the first time a resource is requested, and resource assemblies are loaded into the application domain on first
    access, and remain there until the application domain is unloaded.

    So, does this mean that one should never close the resource file? Are there problems associated with not closing the
    resource file? And, if one follows the examples from the web, closing the resource manager, how does one check
    for a closed resource and then open the resource if it's closed?


Page 1 of 1 (1 items)