public void LoadData(bool forceSync)
        {
            SelectTaskFromDatabase();

            try
            {
                if (forceSync)
                {
                    using (Database db = new Database())
                    {
                        if (db.DatabaseExists() == true)
                        {
                            // Create the database.
                            db.DeleteDatabase();
                        }
                    }
                }

                if (forceSync || !_Tasks.Any())
                {
                    LoadProjects();
                }

            }
            catch (SystemException _ex)
            {
                _ConnectionStatus = _ex.Message;
            }

        }
        void _prjClient_GetEmployeeWorkCompleted(object sender, GetEmployeeWorkCompletedEventArgs e)
        {
            DateTime _today = DateTime.Today;
            DateTime _yesterday = DateTime.Today.AddDays(-1);
            List<int> _recentUsedTaskID = new List<int>();
            Dictionary<int, string> _recentUsedComments = new Dictionary<int, string>();
            foreach (WorkUnit u in e.Result.GetEmployeeWorkResult.Return.OrderBy(w => w.StartDateTime))
            {
                _recentUsedTaskID.Add(u.TaskID);
                if (!String.IsNullOrWhiteSpace(u.Description))
                {
                    _recentUsedComments[u.TaskID] = u.Description;
                }

                if (u.StartDateTime.Date == _today) {
                    App.RegistrationViewModel.TodayRegistrations.Add(u);
                }
                if (u.StartDateTime.Date == _yesterday)
                {
                    App.RegistrationViewModel.YesterdayRegistrations.Add(u);
                }

            }

            _RecentTasks.Clear();
            foreach (int taskID in _recentUsedTaskID.Distinct())
            {
                var _task = this._allTasks.FirstOrDefault(t => t.ID == taskID);
                if (_task != null) _RecentTasks.Add(_task);
            }
            using (Database db = new Database())
            {
                if (db.DatabaseExists() == false)
                {
                    // Create the database.
                    db.CreateDatabase();
                }

                foreach (var _usedTask in _RecentTasks) 
                { 
                    var taskInDB = from WPTask t in db.tasksTable where t.ID == _usedTask.ID 
                                   select t;

                    foreach (var t in taskInDB) {
                        t.RecentUsed = true;
                        string _comment;
                        if (_recentUsedComments.TryGetValue(t.ID, out _comment))
                        {
                            t.RecentComment = _comment;
                        }                         
                    }
                }
                db.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            }
            
            IsDataLoaded = true;
            LoadInProgress = false;
        }
        private void SelectTaskFromDatabase()
        {
            using (Database db = new Database())
            {
                if (db.DatabaseExists() == false)
                {
                    // Create the database.
                    db.CreateDatabase();
                }
                try
                {
                    var taskInDB = from WPTask t in db.tasksTable
                                   select t;
                    Tasks.Clear();
                    _allTasks.Clear();
                    foreach (WPTask t in taskInDB) {
                        Tasks.Add(t);
                        _allTasks.Add(t);
                    }
                   
                    foreach (var _T in _Tasks.OrderByDescending(t => t.StartDate).Take(10).OrderBy(t => t.CustomerName).ThenBy(t => t.ProjectName).ThenBy(t => t.SortOrder))
                    {
                        _NewestTasks.Add(_T);
                    }
                    

                    var _recentTaskInDB = from WPTask t in db.tasksTable where t.RecentUsed
                            select t;

                    RecentTasks.Clear();                    
                    foreach (WPTask t in _recentTaskInDB) {
                        _RecentTasks.Add(t);
                    }

                    // _RecentTasks = _allTasks;
                    if (Tasks.Any())
                    {
                        IsDataLoaded = true;
                    }
                }
                catch (Exception)
                {


                }
            }
        }
        void _prjClient_GetTasksAllocatedToEmployeeCompleted(object sender, GetTasksAllocatedToEmployeeCompletedEventArgs e)
        {
            // get all task that is not parent task
            var _childTasks = e.Result.GetTasksAllocatedToEmployeeResult.Return.Where(t => !t.IsParent);

            // construct the all Task list
            this._allTasks.Clear();
            Tasks.Clear();
            foreach (var _T in _childTasks.OrderBy(t => t.Details.CustomerHeader.Name).ThenBy(t => t.Details.ProjectHeader.Name).ThenBy(t => t.SortOrder))
            {
                var _t = FromAPItoDomain(_T);
                this._allTasks.Add(_t);
                Tasks.Add(_t);
            }

            using (Database db = new Database())
            {
                if (db.DatabaseExists() == false)
                {
                    // Create the database.
                    db.CreateDatabase();
                }
                else {
                    db.tasksTable.DeleteAllOnSubmit(db.tasksTable);
                }

                db.tasksTable.InsertAllOnSubmit(this._allTasks);
                db.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            }

            _NewestTasks.Clear();
            // constuct the list of the 7 newest task
            foreach (var _T in _childTasks.OrderByDescending(t => t.StartDate).Take(7).OrderBy(t => t.Details.CustomerHeader.Name).ThenBy(t => t.Details.ProjectHeader.Name).ThenBy(t => t.SortOrder))
            {
                _NewestTasks.Add(FromAPItoDomain(_T));
            }
            
            IsDataLoaded = true;
            LoadInProgress = false;
            LoadLastWeeksRegistrationsFromServer();
        }
 // Code to execute on Unhandled Exceptions
 private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
 {
     MessageBox.Show(e.ExceptionObject.Message);
     
      using (Database db = new Database())
     {
         if (db.DatabaseExists())
         {
             // Create the database.
             db.DeleteDatabase();
         }
     }
     
     if (System.Diagnostics.Debugger.IsAttached)
     {
         // An unhandled exception has occurred; break into the debugger
         System.Diagnostics.Debugger.Break();
     }
 }