Sequence contains no elements when register

Dec 5, 2008 at 6:25 AM
 When I had dirty username from cookies or when I try to register with email/password for clean database/browser settings I receive the following exception.

    System.InvalidOperationException was unhandled by user code
  Message="Sequence contains no elements"
  Source="System.Core"
  StackTrace:
       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
       at lambda_method(ExecutionScope )
       at System.Linq.EnumerableExecutor`1.Execute()
       at System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       at System.Linq.Queryable.First[TSource](IQueryable`1 source)
       at Dropthings.Business.Activities.GetUserSettingActivity.<Execute>b__1(IQueryable`1 query) in Z:\Dropthings\trunk\DashboardBusiness\Activities\UserActivities\GetUserSettingActivity.cs:line 73
       at Dropthings.DataAccess.DatabaseHelper.<>c__DisplayClass26`3.<GetQueryResult>b__25(DropthingsDataContext data) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 327
       at Dropthings.DataAccess.DatabaseHelper.InDataContext[T](SubsystemEnum subsystem, Func`2 f) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 102
       at Dropthings.DataAccess.DatabaseHelper.GetQueryResult[TSource,TArg0,TReturnType](SubsystemEnum subsystem, TArg0 arg0, Func`3 func, Func`2 returnExpected) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 325
       at Dropthings.Business.Activities.GetUserSettingActivity.<Execute>b__0(UserSetting newSetting) in Z:\Dropthings\trunk\DashboardBusiness\Activities\UserActivities\GetUserSettingActivity.cs:line 70
       at Dropthings.DataAccess.DatabaseHelper.<>c__DisplayClass1d`1.<Insert>b__1c(DropthingsDataContext data) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 280
       at Dropthings.DataAccess.DatabaseHelper.InDataContext[T](SubsystemEnum subsystem, Func`2 f) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 102
       at Dropthings.DataAccess.DatabaseHelper.Insert[TSource](SubsystemEnum subsystem, Action`1 populate) in Z:\Dropthings\trunk\DashboardDataAccess\DatabaseHelper.cs:line 276
       at Dropthings.Business.Activities.GetUserSettingActivity.Execute(ActivityExecutionContext executionContext) in Z:\Dropthings\trunk\DashboardBusiness\Activities\UserActivities\GetUserSettingActivity.cs:line 65
       at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)
       at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)
       at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)
  InnerException:

Code fragment

   protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            using( new TimedLog(UserGuid.ToString(), "Activity: Get User Setting") )
            {
                var userSetting = DatabaseHelper.GetSingle<UserSetting, Guid>(DatabaseHelper.SubsystemEnum.User,
                    this.UserGuid, LinqQueries.CompiledQuery_GetUserSettingByUserGuid);

                if (userSetting == default(UserSetting))
                {
                    // No setting saved before. Create default setting
                    
                    userSetting = DatabaseHelper.Insert<UserSetting>(DatabaseHelper.SubsystemEnum.User,
                        (newSetting) =>
                        {
                            newSetting.UserId = UserGuid;
                            newSetting.CreatedDate = DateTime.Now;
                            newSetting.CurrentPageId = DatabaseHelper.GetQueryResult<int, Guid, int>(
                                DatabaseHelper.SubsystemEnum.Page,
                                this.UserGuid, LinqQueries.CompiledQuery_GetPageIdByUserGuid,
                                (query) => query.First<int>());
                        });                        
                }
Jan 6, 2009 at 1:59 AM
I get the same error in the same place. Has anyone figured this out?
Jan 9, 2009 at 2:56 AM
yes same error for me... help needed .
Jan 9, 2009 at 8:58 AM
i solved this its very easy...
Jan 9, 2009 at 7:02 PM
Dushan,

Could you share how you solved this ? I'm sure it'll serve someone else
Jan 21, 2009 at 4:38 AM
I've waited  for more recent release hoping the error will go away. It is still there for the latest and greatest 24354. I managed to overcome that just manually adding records to table while debugging , but ended up with only 3 widgets available and exception when try to add any of them -  null value for getter 

        public int WidgetZoneId
        {
            get { return (int)base.GetValue(WidgetZoneIdProperty); }
            set { base.SetValue(WidgetZoneIdProperty, value); }
        }
WidgetZone table doesn't have any records.  Is it expected ?

Back to originaL PROBLEM . The comment was added "No setting saved before. Create default setting" , so I am confused with the code below.

GetUserSettingActivity.cs

                if (userSetting == default(UserSetting))
                {
                    // No setting saved before. Create default setting
                    
                    userSetting = DatabaseHelper.Insert<UserSetting>(DatabaseHelper.SubsystemEnum.User,
                        (newSetting) =>
                        {
                            newSetting.UserId = UserGuid;
                            newSetting.CreatedDate = DateTime.Now;
                            newSetting.CurrentPageId = DatabaseHelper.GetQueryResult<int, Guid, int>(
                                DatabaseHelper.SubsystemEnum.Page,
                                this.UserGuid, LinqQueries.CompiledQuery_GetPageIdByUserGuid,
                                (query) => query.First<int>());
                        });                        
                }
in LinqQueries.cs
        public static readonly Func<DropthingsDataContext, Guid, IQueryable<int>> CompiledQuery_GetPageIdByUserGuid =
            CompiledQuery.Compile<DropthingsDataContext, Guid, IQueryable<int>>((dc, userGuid) =>
                from page in dc.Pages
                where page.UserId == userGuid
                select page.ID
            );

output window

SELECT [t0].[ID]
FROM [dbo].[Page] AS [t0]
WHERE [t0].[UserId] = @p0
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [6bcb0b75-928f-43d6-ab31-3630e92db0cb]


Why this code fragment expects in page table a  row for this user GUID to exist ?  It has not been created yet.
I am surprised how it works for everyone else given all private information was cleared in a browser and the latest  dropthings.mdf was attached. Probably not relevant , but I use developer edition of SQL Server, not SQL Express.
Jan 21, 2009 at 5:51 AM
It started to work with 24448 , perhaps , the problem  was specific for  24354.