private void Preferences_Load(object sender, EventArgs e) { string dbpath, pathbackup; dbpath = Settings.Default.DatabasePath; dbpathpref.Text = dbpath; pathbackup = Settings.Default.BackupLocation; backupdbpathpref.Text = pathbackup; if (pathbackup == "") { warningbackup.Text = "* Please mention a path for database to Backup, prevent you to protect the data from system failure."; } if (dbpath == "") { var filepath = new FilePath(); filepath.ShowDialog(); } else { FilePath createDBfolder = new FilePath(); string db = Settings.Default.DatabasePath + "\\" + Global.ROOT_DATA_FOLDER + "\\" + Global.DB_NAME_PREFIX + ".sdf"; // If Database file not exist then create a database file createDBfolder.CreateRootFolder(Settings.Default.DatabasePath); if (!File.Exists(db)) { Boolean flagdb; string flagtable; dboperation operation = new dboperation(); flagdb = operation.createDatabase(Settings.Default.DatabasePath); SqlCeConnection conn = null; conn = operation.dbConnection(Settings.Default.DatabasePath); if (conn.State == System.Data.ConnectionState.Open && flagdb == true) { flagtable = operation.createTable(conn); if (flagtable != "s") { MessageBox.Show("An error occured to create table in the database " + flagtable); } operation.closeDBConnection(conn); } else { MessageBox.Show("An error occured to build connection with databases"); } } else { // MessageBox.Show("Database File already Exist"); } } if (dbpathpref.Text == "" && backupdbpathpref.Text == "") { backupDBbtn.Enabled = false; } }
private void savedestinationbtn_Click(object sender, EventArgs e) { if (!this.ValidateChildren(ValidationConstraints.Enabled)) { System.Media.SystemSounds.Exclamation.Play(); return; } Settings.Default.DatabasePath = browsedbpath.Text; Settings.Default.BackupLocation = browsebackupdbpath.Text; if (!CreateRootFolder(browsedbpath.Text)) { return; } try { Boolean flag; Settings.Default.Save(); dboperation operation = new dboperation(); SqlCeConnection conn = null; if (!File.Exists(Settings.Default.DatabasePath + "\\" + Global.ROOT_DATA_FOLDER + "\\" + Global.DB_NAME_PREFIX + ".sdf")) { flag = operation.createDatabase(Settings.Default.DatabasePath); } else { throw new Exception("Database already exist in selected path"); } if (flag) { conn = operation.dbConnection(Settings.Default.DatabasePath); string tabflag = operation.createTable(conn); if (tabflag == "s") { MessageBox.Show("Database Created Successfully"); } else { MessageBox.Show("Error creating database"); } } else { MessageBox.Show("An error Occured to create the Database or Database already Exist"); } } catch (Exception ex) { string message = "An error occurred to save the settings." + "\nThe error text is as follows: \n" + Global.getExceptionText(ex); SystemSounds.Hand.Play(); MessageBox.Show(message, "Error Occurred", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.Close(); }