public static bool UpdateOneMessageTemplateCategory(MessageTemplateCategoryType cat) { IDbCommand cmd = DataFactory.CreateCommand(null); cmd.CommandText = @"Update [MessageTemplateCategory] set ParentCategoryId=@ParentCategoryId, CategoryName=@CategoryName, CategoryDescription=@CategoryDescription where CategoryId=@CategoryId"; DataFactory.AddCommandParam(cmd, "@ParentCategoryId", DbType.Int32, cat.ParentCategoryId); DataFactory.AddCommandParam(cmd, "@CategoryName", DbType.String, StringUtil.GetSafeString(cat.CategoryName)); DataFactory.AddCommandParam(cmd, "@CategoryDescription", DbType.String, StringUtil.GetSafeString(cat.CategoryDescription)); DataFactory.AddCommandParam(cmd, "@CategoryId", DbType.Int32, StringUtil.GetSafeInt(cat.CategoryId)); bool result = DataFactory.ExecuteCommandNonQuery(cmd); return result; }
// Insert one message template category. public static bool InsertOneMessageTemplateCategory(MessageTemplateCategoryType cat, out int newCatId) { newCatId = 0; IDbCommand cmd = DataFactory.CreateCommand(null); cmd.CommandText = @"Insert into [MessageTemplateCategory] (ParentCategoryId, CategoryName, CategoryDescription) values (@ParentCategoryId, @CategoryName, @CategoryDescription)"; DataFactory.AddCommandParam(cmd, "@ParentCategoryId", DbType.Int32, cat.ParentCategoryId); DataFactory.AddCommandParam(cmd, "@CategoryName", DbType.String, StringUtil.GetSafeString(cat.CategoryName)); DataFactory.AddCommandParam(cmd, "@CategoryDescription", DbType.String, StringUtil.GetSafeString(cat.CategoryDescription)); bool result = DataFactory.ExecuteInsertCommand(cmd, out newCatId); return result; }
public static MessageTemplateCategoryType GetMessageTemplateCategory(String categoryName) { String sql_getCategory = String.Format("select * from [MessageTemplateCategory] where CategoryName='{0}'", categoryName); DataTable dt = DataFactory.ExecuteSqlReturnTable(sql_getCategory); if (dt.Rows.Count == 0) return null; DataRow dr = dt.Rows[0]; MessageTemplateCategoryType categoryType = new MessageTemplateCategoryType(); categoryType.CategoryId = StringUtil.GetSafeInt(dr["CategoryId"]); categoryType.ParentCategoryId = StringUtil.GetSafeInt(dr["ParentCategoryId"]); categoryType.CategoryName = StringUtil.GetSafeString(dr["CategoryName"]); return categoryType; }
// When user done with editing the message template category. // Two possible cases: // 1) Add a new category, or // 2) Modify an existed category. private void treeViewMessageTemplate_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { if (e.Label == null || e.Label.Trim().Length == 0) { e.CancelEdit = true; e.Node.BeginEdit(); return; } String categoryName = e.Label; MessageTemplateCategoryType categoryType = (MessageTemplateCategoryType)e.Node.Tag; if (categoryType != null) { // Modify an existed category. if (categoryName == categoryType.CategoryName) return; categoryType.CategoryName = categoryName; MessageTemplateCategoryDAL.UpdateOneMessageTemplateCategory(categoryType); this.treeViewMessageTemplate.LabelEdit = false; this.treeViewMessageTemplate.SelectedNode = e.Node; return; } // Add a new category. categoryType = new MessageTemplateCategoryType(); categoryType.ParentCategoryId = -1; categoryType.CategoryName = categoryName; categoryType.CategoryDescription = ""; // Check if there is any category with the same category name. MessageTemplateCategoryType existedCategoryType = MessageTemplateCategoryDAL.GetMessageTemplateCategory(categoryName); if (existedCategoryType != null) { MessageBox.Show("已存在此消息模板主题,请输入不同模板主题!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); e.CancelEdit = true; e.Node.BeginEdit(); return; } // Insert the new category to database and return the new category id. int newCatId = 0; MessageTemplateCategoryDAL.InsertOneMessageTemplateCategory(categoryType, out newCatId); categoryType.CategoryId = newCatId; e.Node.EndEdit(false); e.Node.Tag = categoryType; this.treeViewMessageTemplate.SelectedNode = e.Node; this.treeViewMessageTemplate.LabelEdit = false; }
// Load all message template categories. private void LoadAllMessageTemplateCategories() { DataTable dtCategories = MessageTemplateCategoryDAL.GetAllMessageTemplateCategories(); this.treeViewMessageTemplate.Nodes.Clear(); foreach (DataRow dr in dtCategories.Rows) { int categoryId = StringUtil.GetSafeInt(dr["CategoryId"]); String categoryName = StringUtil.GetSafeString(dr["CategoryName"]); MessageTemplateCategoryType categoryType = new MessageTemplateCategoryType(); categoryType.CategoryId = categoryId; categoryType.CategoryName = categoryName; TreeNode newNode = new TreeNode(); newNode.Tag = categoryType; newNode.Text = categoryName; newNode.Name = categoryId.ToString(); this.treeViewMessageTemplate.Nodes.Add(newNode); } }
private void LoadAllMessageTemplates() { this.treeViewMessageTemplates.Nodes.Clear(); DataTable dtMessageCategories = MessageTemplateCategoryDAL.GetAllMessageTemplateCategories(); DataTable dtMessageTemplates = MessageTemplateDAL.GetAllMessageTemplates(); foreach (DataRow drCategory in dtMessageCategories.Rows) { int categoryId = StringUtil.GetSafeInt(drCategory["CategoryId"]); String categoryName = StringUtil.GetSafeString(drCategory["CategoryName"]); MessageTemplateCategoryType catType = new MessageTemplateCategoryType(); TreeNode catNode = new TreeNode(); catNode.Tag = catType; catNode.Text = categoryName; catNode.Name = categoryId.ToString(); this.treeViewMessageTemplates.Nodes.Add(catNode); } foreach (DataRow drMessageTemplate in dtMessageTemplates.Rows) { int templateId = StringUtil.GetSafeInt(drMessageTemplate["TemplateId"]); int categoryId = StringUtil.GetSafeInt(drMessageTemplate["TemplateCategoryId"]); String templateName = StringUtil.GetSafeString(drMessageTemplate["TemplateName"]); String templateContent = StringUtil.GetSafeString(drMessageTemplate["TemplateContent"]); MessageTemplateType templateType = new MessageTemplateType(); templateType.TemplateId = templateId; templateType.TemplateCategoryId = categoryId; templateType.TemplateName = templateName; templateType.TemplateContent = templateContent; TreeNode msgNode = new TreeNode(); msgNode.Tag = templateType; msgNode.Text = templateName; TreeNode[] catNodes = this.treeViewMessageTemplates.Nodes.Find(categoryId.ToString(), false); if (catNodes.Length > 0) catNodes[0].Nodes.Add(msgNode); } this.treeViewMessageTemplates.ExpandAll(); }