private void btnImport_Click(object sender, EventArgs e) { try { int id = Access.SearchCurrentMonthData(new DateTime(dp.Value.Year, dp.Value.Month, 1)); if (id == -1) { Access.AddInSaveMonth(Properties.Settings.Default.id, new DateTime(dp.Value.Year, dp.Value.Month, 1)); foreach (DataGridViewRow row in dg.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value == null) { cell.Value = ""; } if (cell.OwningColumn.HeaderText == "Class" && (cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString()))) { cell.Value = "XXX"; } } } for (int i = 0; i < dg.Rows.Count; i++) { Access.AddInDataBaseFile(Properties.Settings.Default.id, dg.Rows[i].Cells[0].Value.ToString(), dg.Rows[i].Cells[1].Value.ToString(), dg.Rows[i].Cells[2].Value.ToString(), dg.Rows[i].Cells[3].Value.ToString(), dg.Rows[i].Cells[4].Value.ToString(), dg.Rows[i].Cells[5].Value.ToString(), dg.Rows[i].Cells[6].Value.ToString(), dg.Rows[i].Cells[7].Value.ToString(), dg.Rows[i].Cells[8].Value.ToString(), dg.Rows[i].Cells[9].Value.ToString(), dg.Rows[i].Cells[10].Value.ToString(), dg.Rows[i].Cells[11].Value.ToString(), "Helloo"); } Access.GetNextId(); } else { bool IsDuplicate = Access.IdExistsInDatabaseFile(id); if (IsDuplicate) { if (MessageBox.Show("Database file already exists. Do you want to replace it?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes) { Access.DeletePreviousDatabaseRecord(id); foreach (DataGridViewRow row in dg.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value == null) { cell.Value = ""; } if (cell.OwningColumn.HeaderText == "Class" && (cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString()))) { cell.Value = "XXX"; } } } for (int i = 0; i < dg.Rows.Count; i++) { Access.AddInDataBaseFile(id, dg.Rows[i].Cells[0].Value.ToString(), dg.Rows[i].Cells[1].Value.ToString(), dg.Rows[i].Cells[2].Value.ToString(), dg.Rows[i].Cells[3].Value.ToString(), dg.Rows[i].Cells[4].Value.ToString(), dg.Rows[i].Cells[5].Value.ToString(), dg.Rows[i].Cells[6].Value.ToString(), dg.Rows[i].Cells[7].Value.ToString(), dg.Rows[i].Cells[8].Value.ToString(), dg.Rows[i].Cells[9].Value.ToString(), dg.Rows[i].Cells[10].Value.ToString(), dg.Rows[i].Cells[11].Value.ToString(), "Helloo"); } } } else { foreach (DataGridViewRow row in dg.Rows) { foreach (DataGridViewCell cell in row.Cells) { if (cell.Value == null) { cell.Value = ""; } if (cell.OwningColumn.HeaderText == "Class" && (cell.Value == null || String.IsNullOrEmpty(cell.Value.ToString()))) { cell.Value = "XXX"; } } } for (int i = 0; i < dg.Rows.Count; i++) { Access.AddInDataBaseFile(id, dg.Rows[i].Cells[0].Value.ToString(), dg.Rows[i].Cells[1].Value.ToString(), dg.Rows[i].Cells[2].Value.ToString(), dg.Rows[i].Cells[3].Value.ToString(), dg.Rows[i].Cells[4].Value.ToString(), dg.Rows[i].Cells[5].Value.ToString(), dg.Rows[i].Cells[6].Value.ToString(), dg.Rows[i].Cells[7].Value.ToString(), dg.Rows[i].Cells[8].Value.ToString(), dg.Rows[i].Cells[9].Value.ToString(), dg.Rows[i].Cells[10].Value.ToString(), dg.Rows[i].Cells[11].Value.ToString(), "Helloo"); } } } Manager.Show("Import successfull", Notification.Type.Success); Properties.Settings.Default.DatabaseLastUpdated = DateTime.Now; Properties.Settings.Default.Save(); } catch (Exception ex) { Manager.Show("Import failed", Notification.Type.Error); if (MessageBox.Show("Couldn't import data from excel files due to:\n" + ex.Message, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes) { btnImport_Click(sender, e); } } }