Problem with Datareader and 'using' statement

Problem with Datareader and 'using' statement

Old forum URL: forums.lhotka.net/forums/t/12178.aspx


Coderforlife posted on Thursday, October 10, 2013

I am trying to implement Editable rootlist  stereotype in my project, my list (collection) class is TestList.cs and my child object is TestEdit.cs, I have also a TestDal.cs class, I am following Rocky's samples and encapsulated invoke for data access.

Inside my Testlist.cs I have this method

 private void DataPortal_Fetch()
        {
            var rlce = RaiseListChangedEvents;
            RaiseListChangedEvents = false;

            using (var dalManager = DataAccess.DalFactory.GetManager())
            {
                var dal = dalManager.GetProvider<DataAccess.ITestDal>();
                Csla.Data.SafeDataReader data = new Csla.Data.SafeDataReader(dal.Fetch());
                while (data.Read())

                {
                    Add(DataPortal.FetchChild<TestEdit>(data));
                }

            }
            RaiseListChangedEvents = rlce;
        }

 

While inside my TestDal.cs I have


       public IDataReader Fetch()
        {
           
            using (var ctx = ConnectionManager<SqlConnection>.GetManager("MyDBConnection"))
            {
             
                    var cm = ctx.Connection.CreateCommand();
                    cm.CommandType = System.Data.CommandType.StoredProcedure;
                    cm.CommandText = "usp_tblBlahSelecttAll";

                    return cm.ExecuteReader();
               
            }
        }

 

When I get the datareader back in DataPortal_Fetch (inside Testlist.cs) , the datareader is already closed. I  removed the 'using' statement from the Fetch method inside TestDal.cs and used just

var ctx = ConnectionManager<SqlConnection>.GetManager("MyDBConnection"));

and then it worked!

 but I have two questions.

1) If I remove the 'using'  statement as stated above, where will my datareader get closed?

2) I am wondering if there is a typo in the sample code (specifically the example EncapsulatedInvoke for 'DataAcess' samples)? or I might not be following it correctly?

 

Thanks for your time.

Coder

 

Coderforlife replied on Thursday, October 10, 2013

I have a few more questions about this stereotype

1) How do we get a specific child object ? back in CSLA 1.x I used to write my own methods in the collection (list) class something like getchild(int ChildID).

2)Rocky has discussed three techniques of adding a child to a list but how about removing a child object?

Thanks

Coder

 

comp1mp replied on Thursday, October 10, 2013

"2)Rocky has discussed three techniques of adding a child to a list but how about removing a child object?"

Simply call remove on the list object with a reference to the child object - MyList.Remove(MyChild)

CSLA will call Child_DeleteSelf on the child object when it is saved.


Copyright (c) Marimer LLC