Search code examples
c#sqlsql-serverdatabaseadventureworks

Getting name associated with province(AdventureWorks2014) exception


im doing test using the AdventureWorks2014 microsoft database with visual studio(coding in C#).I am trying to input StateProvinceID and then display the names(Person.FirstName) that are associated with this StateProvinceID.So far I did this:

 static public void provincequery()
    {
        Console.WriteLine("Enter province ID");
        var theid = Convert.ToInt32(Console.ReadLine());
        using (var context = new Model1())
        {
            var queryprovince = from test in context.StateProvince
                         where test.StateProvinceID == theid
                         select test;
            foreach(var disp in queryprovince)
            {
                Console.WriteLine("\nProvince:");
                Console.WriteLine(disp.Name);
                foreach(var test2 in disp.SalesTerritory.Customer)
                {
                    Console.WriteLine(test2.Person.FirstName);
                }

            }
        }


    }

however this keeps returning me an error "An error occurred while executing the command definition. See the inner exception for details." inner exception:

      à System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   à System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   à System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
   à System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   à System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   à System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   à System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   à System.Data.Entity.Core.Objects.ObjectQuery`1.Execute(MergeOption mergeOption)
   à System.Data.Entity.Core.Objects.DataClasses.EntityReference`1.Load(MergeOption mergeOption)
   à System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.DeferredLoad()
   à System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.LoadProperty[TItem](TItem propertyValue, String relationshipName, String targetRoleName, Boolean mustBeNull, Object wrapperObject)
   à System.Data.Entity.Core.Objects.Internal.LazyLoadBehavior.<>c__DisplayClass7`2.<GetInterceptorDelegate>b__2(TProxy proxy, TItem item)
   à System.Data.Entity.DynamicProxies.StateProvince_0A13F786927514ECF26BEB6F7007442E73C1FCAA3A743679986FA051D479F5AA.get_SalesTerritory()
   à ConsoleApplication1.Program.requete2() dans c:\Users\julianp\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:ligne 152
   à ConsoleApplication1.Program.Main(String[] args) dans c:\Users\julianp\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:ligne 33
   à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   à System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   à System.Threading.ThreadHelper.ThreadStart()

I dont really understand why its not working.AdventureWorks schema can be found here:

https://moidulhassan.files.wordpress.com/2014/07/adventureworks2008_schema.gif

thank you


Solution

  • Solved; needed to add ToList() at the end of foreach(var disp in queryprovince).

    So its foreach(var disp in queryprovince.ToList())