private void btnSave_Click(object sender, EventArgs e) { if (String.IsNullOrWhiteSpace(txTemplateName.Text)) { Controller.HandleError("Name is required", "Validation Error"); return; } if (String.IsNullOrWhiteSpace(txTemplateText.Text)) { Controller.HandleError("Text is required", "Validation Error"); return; } //if not General then can only have one NotificationTemplateType templateType = (NotificationTemplateType)cbTemplateType.SelectedItem; if (templateType != NotificationTemplateType.General) { var existing = _Context.NotificationTemplateSet.Where(a => a.TemplateType == templateType).FirstOrDefault(); if (existing != null) { if (_Item == null || _Item.id != existing.id) { Controller.HandleError("A template is already defined for " + templateType.ToString() + " please edit the existing tempalte", "Validation Error"); return; } } //validate that all tags are valid var tags = NotificationTemplate.GetAllTags(txTemplateText.Text); var allowedTags = NotificationTypeTag.NotificationTags[templateType]; var allowedTagnames = allowedTags.Select(a => NotificationTypeTag.TagName(a)).ToArray(); var tagsNotAllowed = tags.Where(a => !allowedTagnames.Contains(a)).ToList(); if (tagsNotAllowed.Count() > 0) { string errText = string.Empty; foreach (var s in tagsNotAllowed) { errText += s + Environment.NewLine; } Controller.HandleError("The following tags are invalid for this message type " + Environment.NewLine + errText, "Validation Error"); return; } } if (_Item == null) { _Item = new NotificationTemplate(); _Context.NotificationTemplateSet.Add(_Item); } _Item.TemplateName = txTemplateName.Text; _Item.MessageText = txTemplateText.Text; _Item.TemplateType = (NotificationTemplateType)cbTemplateType.SelectedItem; try { bool isNew = _Item.id == 0; _Context.SaveChanges(); if (isNew) { _Data.Insert(0, _Item); } BindDataGrid(); GotoReadOnly(); } catch (DbUpdateException) { Controller.HandleError("Possible duplicate record detected", "Database Error"); } catch (Exception ex2) { Controller.HandleError(ex2.Message); } }