public static void AmendEntity(ref Entities selectedEntity, string entityName, string entityDescription) { int intSelectedEntityID; if (selectedEntity == null) { MessageFunctions.InvalidMessage("Please select an Entity to amend from the drop-down list.", "No Entity Selected"); return; } if (!PageFunctions.SqlInputOK(entityName, true, "Entity name")) { return; } else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description")) { return; } try { ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection(); using (existingPtDb) { intSelectedEntityID = selectedEntity.ID; Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName && ent.ID != intSelectedEntityID); if (checkNewName != null) { MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with name '" + entityName + "' already exists.", "Duplicate Name"); return; } Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription && ent.ID != intSelectedEntityID); if (checkNewDescription != null) { MessageFunctions.InvalidMessage("Could not amend Entity. Another Entity with description '" + entityDescription + "' already exists.", "Duplicate Description"); return; } try { try { string nameChange = ""; string originalName = selectedEntity.EntityName; if (originalName != entityName) { nameChange = " to '" + entityName + "'"; } ; Entities changeDbEntity = existingPtDb.Entities.Find(intSelectedEntityID); changeDbEntity.EntityName = entityName; changeDbEntity.EntityDescription = entityDescription; existingPtDb.SaveChanges(); MessageFunctions.SuccessAlert("Entity '" + originalName + "' has been amended" + nameChange + ".", "Entity Amended"); if (changeDbEntity.ID == CurrentEntityID) { UpdateCurrentEntity(ref changeDbEntity); } if (changeDbEntity.ID == MyDefaultEntityID) { UpdateMyDefaultEntity(ref changeDbEntity); } PageFunctions.ShowTilesPage(); } catch (Exception generalException) { MessageFunctions.Error("Error amending database record", generalException); return; } } catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); } } } catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); } }
// Changes public static bool ValidateProduct(ref Products thisProduct, int existingID) { try { ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection(); using (existingPtDb) { string productName = thisProduct.ProductName; if (!PageFunctions.SqlInputOK(productName, true, "Product name")) { return(false); } Products checkNewName = existingPtDb.Products.FirstOrDefault(p => p.ID != existingID && p.ProductName == productName); if (checkNewName == null) { thisProduct.ProductName = productName; } else { string errorText = (existingID > 0) ? "Could not amend Product. Another Product with name '" + productName + "' already exists." : "Could not create new Product. A Product with name '" + productName + "' already exists."; MessageFunctions.InvalidMessage(errorText, "Duplicate Name"); return(false); } string productDescription = thisProduct.ProductDescription; if (!PageFunctions.SqlInputOK(productDescription, true, "Product description")) { return(false); } Products checkNewDescription = existingPtDb.Products.FirstOrDefault(p => p.ID != existingID && p.ProductDescription == productDescription); if (checkNewDescription == null) { thisProduct.ProductDescription = productDescription; } else { string errorText = (existingID > 0) ? "Could not amend product. Another product with description '" + productDescription + "' already exists." : "Could not create new product. A product with description '" + productDescription + "' already exists."; MessageFunctions.InvalidMessage(errorText, "Duplicate Description"); return(false); } return(true); } } catch (SqlException sqlException) { MessageFunctions.Error("SQL error saving changes to the database", sqlException); return(false); } catch (Exception generalException) { MessageFunctions.Error("Error saving changes to the database", generalException); return(false); } }
public static void NewEntity(string entityName, string entityDescription, bool switchTo, bool makeDefault) { int newEntityID; Entities newEntity; if (!PageFunctions.SqlInputOK(entityName, true, "Entity name")) { return; } else if (!PageFunctions.SqlInputOK(entityDescription, true, "Entity description")) { return; } try { ProjectTileSqlDatabase existingPtDb = SqlServerConnection.ExistingPtDbConnection(); using (existingPtDb) { Entities checkNewName = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityName == entityName); if (checkNewName != null) { MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with name '" + entityName + "' already exists.", "Duplicate Name"); return; } Entities checkNewDescription = existingPtDb.Entities.FirstOrDefault(ent => ent.EntityDescription == entityDescription); if (checkNewDescription != null) { MessageFunctions.InvalidMessage("Could not create new Entity. An Entity with description '" + entityDescription + "' already exists.", "Duplicate Description"); return; } try { try { newEntity = new Entities(); newEntity.EntityName = entityName; newEntity.EntityDescription = entityDescription; try { existingPtDb.Entities.Add(newEntity); existingPtDb.SaveChanges(); newEntityID = newEntity.ID; } catch (Exception generalException) { MessageFunctions.Error("Problem creating entity ID", generalException); return; } } catch (Exception generalException) { MessageFunctions.Error("Error creating database record", generalException); return; } try { Staff currentUser = MyStaffRecord; AllowEntity(newEntityID, currentUser.ID); } catch (Exception generalException) { MessageFunctions.Error("Error providing access to the new database", generalException); return; } try { existingPtDb.SaveChanges(); string switched = ". Use the 'Change Current Entity' function to log into it if you wish to work in this Entity."; if (switchTo) { UpdateCurrentEntity(ref newEntity); switched = " and you are now logged into it."; } if (makeDefault) { SetDefaultEntity(ref newEntity); } MessageFunctions.SuccessAlert("Entity '" + entityName + "' has been created" + switched, "New Entity Created"); PageFunctions.ShowTilesPage(); } catch (SqlException sqlException) { MessageFunctions.Error("SQL error saving changes to the database", sqlException); return; } catch (Exception generalException) { MessageFunctions.Error("Error saving changes to the database", generalException); return; } } catch (Exception generalException) { MessageFunctions.Error("Error creating new database", generalException); } } } catch (Exception generalException) { MessageFunctions.Error("Error checking new database details", generalException); } }