CSLA 4 - using LoadProperty in Child_Fetch

CSLA 4 - using LoadProperty in Child_Fetch

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


Mr X posted on Tuesday, January 11, 2011

Hello all,

I have a question regarding database access in the Child_Fetch method. 

I have a root object containing a NVL which is loaded in the DataPortal_Fetch using the following statement (in bold):

private void DataPortal_Fetch(RootEditUserCriteria criteria)

{

//// TODO: load values

using (var ctx = ConnectionManager<SqlConnection>.GetManager("MyConnection"))

{

using (var cm = ctx.Connection.CreateCommand())

{

cm.CommandType = System.Data.CommandType.StoredProcedure;

cm.CommandText = "MyProcedureName_Fetch";

cm.Parameters.AddWithValue("@MyParam", criteria.MyParam);

 

                     using (var dr = new SafeDataReader(cm.ExecuteReader()))

{

dr.Read();

using (BypassPropertyChecks)

{

                                  

Id = dr.GetInt32("ID");

ProfileID = dr.GetInt32("ProfileID");

UserName = dr.GetString("DisplayName");

LoginName = dr.GetString("LoginName");

LastUpdated = dr.GetSmartDate("LastUpdated");

UpdatedBy = dr.GetString("UpdatedBy");

}

}

}

}

 

//Load NVL

LoadProperty(RoleListProperty, Business.NVLSecurityProfile.GetNVLSecurityProfile(criteria.Language));

 

                   

}

 

 

 

 

 

 

 

 

 

 Tthis works fine when I retrieve my root object directly.  However, my root object is also a "switchable object" with dual factory and data access methods.  therefore I also call  

LoadProperty(RoleListProperty, Business.NVLSecurityProfile.GetNVLSecurityProfile(criteria.Language));

 in the Child_Fetch method of the root (when used as a list item) as followed:

private void Child_Fetch(ChildEditUserCriteria Criteria)

 

{

using (BypassPropertyChecks)

{

this.Id = Criteria.ID;

this.UserName = Criteria.UserName;

this.ProfileID = Criteria.ProfileID;

this.LoginName = Criteria.LoginName;

this.LastUpdated = Criteria.LastUpdated;

this.UpdatedBy = Criteria.UpdatedBy;

 

                        //Load NVL

LoadProperty(RoleListProperty, NVLSecurityProfile.GetNVLSecurityProfile(Criteria.Language));

 

}

}

 

 

 

 For some reason, the trace goes as far as calling the database access code in the NVL's DataPortal_Fetch bu breaks when executing the cm.ExecuteReader.

 

private void DataPortal_Fetch(SingleCriteria<NVLSecurityProfile, int> criteria)

{

RaiseListChangedEvents = false;

IsReadOnly = false;

 

//// TODO: load values

using (var ctx = ConnectionManager<SqlConnection>.GetManager("MyConnection"))

{

using (var cm = ctx.Connection.CreateCommand())

{

cm.CommandType = System.Data.CommandType.StoredProcedure;

cm.CommandText = "stp_NameValueList_Fetch";

cm.Parameters.AddWithValue("@ListName", "ROLES");

cm.Parameters.AddWithValue("@Language", criteria.Value);

 

using (var dr = new SafeDataReader(cm.ExecuteReader()))

{

while (dr.Read())

                           {

 

Add(new NameValueListBase<int, string>.NameValuePair(dr.GetInt32("Key"), dr.GetString("Value")));

 

                            }

}

}

}

 

IsReadOnly = true;

RaiseListChangedEvents = true;

}

 

 

 

 

 

 

 

Is this error happening because I "call the database in a Child_Fetch" method in my root object (even through a separate object)?  If so, what would be the simplest way to load the NVL along with the root to eliminate the number of calls to the server?  I considered lazy loading the list within the root's list property but I am not sure how this will impact my existing code (MVVM, databinding).  Actually, at this stage, I am open to any suggestions.

Any help or guidelines would be greatly appreciated.    Thanks,

 

 

JonnyBee replied on Tuesday, January 11, 2011

What type of exception do you get?

Does your connecttion string set MultipleActiveResultSets (MARS) to true?

 

Mr X replied on Tuesday, January 11, 2011

Hi JonnyBee, thanks for the quick reply.

No I do not explicitly set MultipleActiveResultSets anywhere in my code.  The EditUserList DataPortal_Fetch only returns 1 recordset, the list of users. Since the EditUser is a root, I need (maybe not if I can find a better way) to return a list of profiles (NVL) and eventually a list of destinations associated to the user which I expect to retrieve on the root's DataPortal_Fetch.  But the EditUser can also be accessed through a list, therefore the use of its Child_Fetch.  The behaviour of the EditUser should remain the same in both cases.  Maybe I am wrong.

Here is the error message I am getting when I put a break point in my EditUserList client side factory method as such:

 public static void GetEditUserList(int Language, EventHandler<DataPortalResult<EditUserList>> callback)

{

var dp = new DataPortal<EditUserList>();

dp.FetchCompleted += (o, e) =>

{

if (e.Error == null)

               

callback(o, e);

       };

           

dp.BeginFetch(new SingleCriteria<EditUserList,int>(Language));

}

 

#endregion

 

 

 

 

 

 

 

e.Error =

{Csla.DataPortalException: DataPortal.Fetch failed (ChildDataPortal.Fetch failed on the server)
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.DataPortal.Fetch[T](Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 170
   at Business.NVLSecurityProfile.GetNVLSecurityProfile(Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 22
   at Business.EditUser.Child_Fetch(ChildEditUserCriteria Criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 365
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 126
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 155
   at Csla.DataPortal.FetchChild[T](Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 842
   at Business.EditUser.GetEditUserChild(Int32 ID, String UserName, String LoginName, Int32 ProfileId, SmartDate LastUpdated, String UpdatedBy, Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 41
   at Business.EditUserList.DataPortal_Fetch(SingleCriteria`2 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUserList\EditUserList.Server.cs:line 46
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.Server.Hosts.Silverlight.SilverlightRequestProcessor.Fetch(SilverlightCriteriaRequest request) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\Hosts\Silverlight\SilverlightRequestProcessor.cs:line 146
Csla.Reflection.CallMethodException: DataPortal_Fetch method call failed
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.DataPortal.Fetch[T](Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 170
   at Business.NVLSecurityProfile.GetNVLSecurityProfile(Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 22
   at Business.EditUser.Child_Fetch(ChildEditUserCriteria Criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 365
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 126
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 155
   at Csla.DataPortal.FetchChild[T](Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 842
   at Business.EditUser.GetEditUserChild(Int32 ID, String UserName, String LoginName, Int32 ProfileId, SmartDate LastUpdated, String UpdatedBy, Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 41
   at Business.EditUserList.DataPortal_Fetch(SingleCriteria`2 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUserList\EditUserList.Server.cs:line 46
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
Csla.DataPortalException: ChildDataPortal.Fetch failed on the server
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.DataPortal.Fetch[T](Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 170
   at Business.NVLSecurityProfile.GetNVLSecurityProfile(Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 22
   at Business.EditUser.Child_Fetch(ChildEditUserCriteria Criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 365
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 126
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 155
   at Csla.DataPortal.FetchChild[T](Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 842
   at Business.EditUser.GetEditUserChild(Int32 ID, String UserName, String LoginName, Int32 ProfileId, SmartDate LastUpdated, String UpdatedBy, Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 41
   at Business.EditUserList.DataPortal_Fetch(SingleCriteria`2 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUserList\EditUserList.Server.cs:line 46
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
Csla.Reflection.CallMethodException: Child_Fetch method call failed
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.DataPortal.Fetch[T](Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 170
   at Business.NVLSecurityProfile.GetNVLSecurityProfile(Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 22
   at Business.EditUser.Child_Fetch(ChildEditUserCriteria Criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 365
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.ChildDataPortal.Fetch(Type objectType, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\ChildDataPortal.cs:line 126
Csla.DataPortalException: DataPortal.Fetch failed (There is already an open DataReader associated with this Command which must be closed first.)
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
   at Csla.DataPortal.Fetch(Type objectType, Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 245
   at Csla.DataPortal.Fetch[T](Object criteria) in C:\Visual Studio Projects\csla\40\Source\Csla\DataPortal.cs:line 170
   at Business.NVLSecurityProfile.GetNVLSecurityProfile(Int32 Language) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 22
   at Business.EditUser.Child_Fetch(ChildEditUserCriteria Criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\User\EditUser\EditUser.Server.cs:line 365
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
Csla.Reflection.CallMethodException: DataPortal_Fetch method call failed
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 549
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 413
   at Csla.Reflection.LateBoundObject.CallMethod(String method, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\LateBoundObject.cs:line 93
   at Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in C:\Visual Studio Projects\csla\40\Source\Csla\Server\SimpleDataPortal.cs:line 133
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
   at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
   at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at Business.NVLSecurityProfile.DataPortal_Fetch(SingleCriteria`2 criteria) in C:\CSLA\CSLA Projects\CSLA 4.0.1 Projects\ARDAppMaintenance\Business.Server\Profiles\NVL\NVLSecurityProfile.Server.cs:line 52
   at lambda_method(Closure , Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in C:\Visual Studio Projects\csla\40\Source\Csla\Reflection\MethodCaller.cs:line 545
}

 

 

I just spotted this message (in bold above in the error description).   This should not be the case since both the EditUserList and the NVL have their own statement (see below).  However, I suspect CreateCommand does not create a new command if one is currently opened (as described in the message above). 

using (var cm = ctx.Connection.CreateCommand())

 And since the DataReader (or the command) is opened by the parent, the child can not reuse it. This may be the reason why I should not use Child_Fetch.  So if I can not use the Child_Fetch of the EditUser (Switchable Root object with dual factory and data access methods) to access the server to load my root's child lists, what is the work around? Can I force the use of DataPortal_Fetch of my root object by its parent list? 

Mr X

 

JonnyBee replied on Tuesday, January 11, 2011

The problem is that you are reusing the same connection and that connection already has an open DataReader.

So you should set MultipleActiveresultSets=true in your connection string.

 

Mr X replied on Wednesday, January 12, 2011

This was indeed the reason the Child_Fetch LoadProperty() was generating this error.  it works fine now.  Many thanks, your help was very valuable (especially since this is a SQL setting and not CSLA implementation). 

Problem solved.

Copyright (c) Marimer LLC