private void SaveData() { try { ADAM.DataBase.ADAMConnectionString Mdb = new DataBase.ADAMConnectionString(); ADAM.DataBase.Job dr = new DataBase.Job(); dr.JobCode = long.Parse(txtJobCode.Text); dr.JobName = txtJobName.Text; Mdb.Jobs.Add(dr); Mdb.SaveChanges(); Response.Redirect("~/MainData/webJobsData.aspx"); } catch { Response.Write("<script>alert('خطأ أثناء الحفظ من فضلك تأكد من ادخال البيانات بشكل صحيح او الاتصال بقاعدة البيانات')</script>"); } }
private static bool CheckJobIsValid(DataBase.Job job, INinjaFactoryData db) { var unfinishedJobsCountOfSameNinja = db.Jobs .Where(j => j.NinjaId == job.NinjaId) .Where(j => j.IsSuccessfull.HasValue == false) .Count(); if (unfinishedJobsCountOfSameNinja > 0) { throw new ArgumentException("A ninja can not take two jobs at the same time. Job '" + job.Name + "' was not imported"); } else { return(true); } }
/// <summary> /// Imports data in database /// </summary> /// <param name="files"> excel files containing import data </param> /// <param name="db"> the database to import the data in </param> private static void ImportExcelFilesIntoDb(string[] files, INinjaFactoryData db) { string sheetName = "Sales"; foreach (var filePath in files) { DataTable excelTable = ReadExcelFile(filePath, sheetName); for (int i = 2; i < excelTable.Rows.Count - 1; i++) { var row = excelTable.Rows[i]; int ninjaId = int.Parse(row.ItemArray[0].ToString()); int clientId = int.Parse(row.ItemArray[1].ToString()); string briefDescription = row.ItemArray[2].ToString(); double jobPrice = double.Parse(row.ItemArray[3].ToString()); DataBase.Job job = new DataBase.Job() { NinjaId = ninjaId, ClientId = clientId, Name = briefDescription, Price = (decimal)jobPrice, StartDate = DateTime.Now, IsDeleted = false }; try { CheckJobIsValid(job, db); ImportJobToDb(job, db); } catch (ArgumentException ae) { MessageBox.Show(ae.Message); } } } }
private static void ImportJobToDb(DataBase.Job job, INinjaFactoryData db) { db.Jobs.Add(job); db.SaveChanges(); }