public bool Upgrade(DataAccessLayer oldData)
        {
            _upgradeData = oldData;
            bool success = false;

            try
            {
                if (_upgradeData != null)
                {
                    if (!_upgradeData.IsOpen)
                        _upgradeData.OpenDAL();

            #if !(PocketPC || WindowsCE || Mobile)
                    Values.UpdateStatusProgress();
            #endif

                    if (_upgradeData.IsOpen)
                    {
                        success = UpgradeProject() &&
                            UpgradeMeta() &&
                            UpgradePolys() &&
                            UpgradeGroups() &&
                            UpgradePoints() &&
                            UpgradeNmea();
                    }
                }
            }
            catch (Exception ex)
            {
                TtUtils.WriteError(ex.Message, "DataAccessUpgrader:Upgrade", ex.StackTrace);
                return false;
            }
            finally
            {
                if (_upgradeData != null && _upgradeData.IsOpen)
                    _upgradeData.CloseDB();
                _upgradeData = null;
            }

            return success;
        }
        private void NewTtFile()
        {
            string fileName;

            if(data != null)
                data.CloseDB();
            FileLoaded = false;

            #if (PocketPC || WindowsCE || Mobile)
            SelectFileDialogEx saveFileDialog1 = new SelectFileDialogEx(null, Values.DefaultSaveFolder);
            //Kb.SetKeyboardToRegularOverride();  //set regular keyboard
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            #else
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "TwoTrails 2 Files|*.tt2";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            #endif
            {
                TtUtils.ShowWaitCursor();
                fileName = saveFileDialog1.FileName;

                if (fileName.Length > 4)
                {
                    if (fileName.Substring(fileName.Length - 4, 4) != ".tt2")
                        fileName += ".tt2";
                }
                else
                {
                    fileName += ".tt2";
                }

                data = NewOpenLogic.NewTwoTrailsFile(fileName);
                if (data != null)
                {
                    _FileName = fileName;

            #if (PocketPC || WindowsCE || Mobile)
                    tbTtFileName.Text = System.IO.Path.GetFileName(_FileName);
            #else
                    ChangeTitle(_FileName);
            #endif
                    data.OpenDAL();
                    tabControl1.SelectedIndex = 1;

                    Values.CurrentDbVersion = true;
                    FileLoaded = true;
                    Values.Settings.ProjectOptions.AddToRecent(_FileName, data.GetProjectID());
                    TtUtils.HideWaitCursor();

            #if !(PocketPC || WindowsCE || Mobile)
                    Values.UpdateStatusText(String.Format("File {0} Loaded.", Path.GetFileName(_FileName)));
                    pnlInfo.Visible = true;
                    UpdateInfo(File.GetCreationTime(_FileName));
                    ChangeTitle(Path.GetFileName(_FileName));
            #else
                        tbTtFileName.Text = System.IO.Path.GetFileName(fileName);
            #endif

                    using (ProjectInfoForm form = new ProjectInfoForm(data))
                    {
                        form.ShowDialog();
            #if!(PocketPC || WindowsCE || Mobile)
                        UpdateInfo();
            #endif
                    }

                    Values.DataExport.FolderName = System.IO.Path.GetFileNameWithoutExtension(_FileName);
                }
                else
                {
            #if!(PocketPC || WindowsCE || Mobile)
                    UpdateInfo();
            #endif
                }
            }
        }
        private void OpenTtFile(string filename)
        {
            if (filename == "Recent Items")
                return;

            bool skip = false;
            try
            {
                TtUtils.ShowWaitCursor();
                _FileName = filename;
                Values.CurrentTtFileName = _FileName;

            #if !(PocketPC || WindowsCE || Mobile)
                FileAttributes attributes = File.GetAttributes(_FileName);

                if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    attributes = attributes & ~FileAttributes.ReadOnly;

                    File.SetAttributes(_FileName, attributes);
                }
            #endif

                if (data != null && data.IsOpen)
                    data.CloseDB();
                data = new DataAccessLayer(_FileName);
                data.OpenDAL();
                Values.CurrentDbVersion = false;
                FileLoaded = true;

                Values.DataExport.FolderName = System.IO.Path.GetFileNameWithoutExtension(_FileName);

                TtUtils.HideWaitCursor();

                try
                {
                    if (data.DalVersion != TwoTrailsSchema.SchemaVersion)
                    {
                        bool closefile = false;

                        if (data.DalVersion < TwoTrailsSchema.SchemaVersion)
                        {
            #if !(PocketPC || WindowsCE || Mobile)
                            pnlInfo.Visible = false;
            #endif
                            if (MessageBox.Show(String.Format("Old Project Version ({1}). Would you like to upgrade to version {0}?", TwoTrailsSchema.SchemaVersion, data.DalVersion), "Old Project Version", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                            {
                                UpgradeTtFile(_FileName);
                            }
                            else
                            {
                                closefile = true;
                            }
                        }
                        else
                        {
                            MessageBox.Show(
                                String.Format("This project file ({0}) requires a newer version of TwoTrails to open.",
                                data.DalVersion), "incompatible file", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                            closefile = true;
                        }

                        if (closefile)
                        {
                            if (data != null && data.IsOpen)
                            {
                                data.CloseDB();
                            }

                            FileLoaded = false;
                        }

                        skip = true;
                    }
                    else
                    {
                        Values.CurrentDbVersion = true;

                    #if !(PocketPC || WindowsCE || Mobile)
                        Values.UpdateStatusText(String.Format("File {0} Loaded.", Path.GetFileName(_FileName)));
                        pnlInfo.Visible = true;
                        UpdateInfo(File.GetCreationTime(_FileName));
                        ChangeTitle(System.IO.Path.GetFileName(filename));
                    #else
                        tbTtFileName.Text = System.IO.Path.GetFileName(filename);
                    #endif
                    }
                }
                catch
                {
                    MessageBox.Show("Unable to get Project Version");
                    skip = true;
                }

                if(!skip)
                {
                    tabControl1.SelectedIndex = 1;

                    cboRecOpen.Items.Clear();
                    cboRecOpen.Items.Add(Values.Settings.ProjectOptions.RecOpen[0].File);
                    Values.Settings.ProjectOptions.AddToRecent(filename, data.GetProjectID());

                    foreach (RecentProject rec in Values.Settings.ProjectOptions.RecOpen)
                    {
                        if (rec.Name != "___")
                            cboRecOpen.Items.Add(System.IO.Path.GetFileName(rec.File));
                    }

                    cboRecOpen.SelectedIndex = 0;

                    #if (PocketPC || WindowsCE || Mobile)
                    btnRecOpen.Enabled = true;
                    #endif
                    cboRecOpen.Enabled = true;
                    Values.Settings.WriteProjectSettings();
                    Values.GPSA.LogFilename = Path.Combine(System.IO.Path.GetDirectoryName(filename), "GPS_Log.txt");
                }
            }
            catch (SQLiteException sqlEx)
            {
            #if !(PocketPC || WindowsCE || Mobile)
                pnlInfo.Visible = false;
            #endif
                switch (sqlEx.ResultCode)
                {
                    case SQLiteErrorCode.NotADb:
                        MessageBox.Show("Database error, not a TwoTrails File");
                        TtUtils.WriteError("SQLiteErrorCode.NotADatabase: Database error, not a TwoTrails File", "MainFormLogic");
                        break;
                    default:
                        MessageBox.Show(sqlEx.Message);
                        break;
                }

                #if (PocketPC || WindowsCE || Mobile)
                tbTtFileName.Text = "";
                #else
                ChangeTitle("");
                #endif
            }
            catch (Exception ex)
            {
            #if !(PocketPC || WindowsCE || Mobile)
                pnlInfo.Visible = false;
            #endif
                MessageBox.Show(ex.Message);
                #if (PocketPC || WindowsCE || Mobile)
                tbTtFileName.Text = "";
                #else
                ChangeTitle("");
                #endif
            }
            finally
            {
                TtUtils.HideWaitCursor();
            }
        }
        private void UpgradeTtFile(string filename)
        {
            DataAccessLayer oldData = null;

            string oldFile = System.IO.Path.GetDirectoryName(filename) + "\\" + System.IO.Path.GetFileNameWithoutExtension(filename) + ".old.tt2";

            try
            {
            #if !(PocketPC || WindowsCE || Mobile)
            tsProg.Value = 0;
            tsProg.Visible = true;
            #endif

                FileLoaded = false;

                TtUtils.WriteEvent("Database upgrade started.", true);

                if (data != null && data.IsOpen)
                    data.CloseDB();

                if (File.Exists(oldFile))
                {
                    if (MessageBox.Show(String.Format("File '{0}' already exists. Would you like to overwrite it?", System.IO.Path.GetFileNameWithoutExtension(oldFile)), "", MessageBoxButtons.YesNo, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        File.Delete(oldFile);
                    }
                    else
                    {
                        int count = 2;
                        while (File.Exists(oldFile))
                        {
                            oldFile = System.IO.Path.GetDirectoryName(filename) + "\\" + System.IO.Path.GetFileNameWithoutExtension(filename) + String.Format(".old_{0}.tt2", count);
                            count++;
                        }
                    }
                }

                TtUtils.ShowWaitCursor();

            #if !(PocketPC || WindowsCE || Mobile)
                Values.UpdateStatusText(String.Format("Upgrading file {0}.", Path.GetFileName(_FileName)));
            #endif
                TtUtils.ShowWaitCursor();
                string fileNoPath = System.IO.Path.GetFileName(filename);
                string oldFileNoPath = System.IO.Path.GetFileName(oldFile);

                Application.DoEvents();

            #if !(PocketPC || WindowsCE || Mobile)
                Values.UpdateStatusProgress();
            #endif

                File.Move(filename, oldFile);
                TtUtils.WriteEvent(String.Format("File Renamed: {0} to {1}.", fileNoPath, oldFileNoPath), true);
                oldData = new DataAccessLayer(oldFile);
                oldData.OpenDAL();

            #if !(PocketPC || WindowsCE || Mobile)
                Values.UpdateStatusProgress();
            #endif
                data = NewOpenLogic.NewTwoTrailsFile(filename);
                if (data == null)
                    throw new Exception(String.Format("Failed to create TtFile ", filename));
                data.OpenDAL();

                bool upgraded = data.Upgrade(oldData);
                TtUtils.WriteEvent("Upgrade proccess finished.", true);
                TtUtils.HideWaitCursor();

            #if !(PocketPC || WindowsCE || Mobile)
                Values.UpdateStatusProgress();
            #endif

                if (upgraded)
                {
                    Values.CurrentDbVersion = true;
                    FileLoaded = true;

                    oldData.CloseDB();

                    TtUtils.WriteMessage(String.Format("{0} Upgraded to version {1}.", fileNoPath, TwoTrailsSchema.SchemaVersion));
                    MessageBox.Show("File upgraded successfully.");
                    tabControl1.SelectedIndex = 1;
                    Values.Settings.ProjectOptions.AddToRecent(filename, data.GetProjectID());

            #if !(PocketPC || WindowsCE || Mobile)
                    UpdateInfo(File.GetCreationTime(_FileName));
                    Values.UpdateStatusText(String.Format("File {0} Upgraded.", Path.GetFileName(_FileName)));
                    tsProg.Visible = false;
                    ChangeTitle(Path.GetFileName(_FileName));
            #else
                    tbTtFileName.Text = Path.GetFileName(_FileName);
            #endif
                }
                else
                {
                    if (oldData != null && oldData.IsOpen)
                        oldData.CloseDB();
                    if (data != null && data.IsOpen)
                        data.CloseDB();

                    oldData = null;
                    data = null;

                    GC.Collect();

                    File.Delete(filename);
                    File.Move(oldFile, filename);
                    TtUtils.WriteMessage("File failed to upgrade.");

            #if !(PocketPC || WindowsCE || Mobile)
                    tsProg.Value = 0;
            #endif

                    MessageBox.Show("File failed to upgrade.");
                }
            }
            catch (Exception ex)
            {
                TtUtils.WriteError(ex.Message, "MainFormLogic:TtFileUpgrade");
                if (oldData != null && oldData.IsOpen)
                    oldData.CloseDB();
                if (data != null && data.IsOpen)
                    data.CloseDB();

                MessageBox.Show("An error has occured. Please see error log for details.");
            #if !(PocketPC || WindowsCE || Mobile)
                Values.UpdateStatusText("Upgrade Error.");
            #endif
            }
            finally
            {
                TtUtils.HideWaitCursor();

            #if !(PocketPC || WindowsCE || Mobile)
            tsProg.Visible = false;
            #endif
            }
        }