public bool UpdateOrAddCostPerDayByVehicleId(DriveBy driveBy) { bool success = false; try { // Establish DB connection VehicleDB db = new VehicleDB(connectionString); db.Connection.Open(); //Query. Check if there is any post for the date. var queryResult = (from v in db.CostPerDays where v.VehicleId == driveBy.VehicleId && v.Date.Date == driveBy.PassedAt.Date select v).SingleOrDefault(); //Handle result if (queryResult == null) { //Add. CostPerDays costPerdays = new CostPerDays { VehicleId = driveBy.VehicleId, Date = driveBy.PassedAt, CostThisDay = driveBy.PassageCost }; db.CostPerDays.InsertOnSubmit(costPerdays); db.SubmitChanges(); db.Connection.Close(); success = true; } else { //Update. int newCostForTheDay = queryResult.CostThisDay + driveBy.PassageCost; queryResult.CostThisDay = newCostForTheDay; db.SubmitChanges(); db.Connection.Close(); success = true; } } catch (Exception ex) { success = false; } return(success); }
public bool AddCurrentDriveBy(DriveBy driveBy) { bool driveByAdded = false; try { // Establish DB connection VehicleDB db = new VehicleDB(connectionString); db.Connection.Open(); //Query DriveBys driveBys = new DriveBys { VehicleId = driveBy.VehicleId, PassageCost = driveBy.PassageCost, PassedAt = driveBy.PassedAt }; // Add the new object. db.DriveBys.InsertOnSubmit(driveBys); // Submit the change to the database. db.SubmitChanges(); //Close DB Connection db.Connection.Close(); driveByAdded = true; } catch (Exception ex) { driveByAdded = false; } return(driveByAdded); }