protected void lnkSaveProductCategory_OnCommand(object sender, CommandEventArgs e) { //clear previous messages lblMessage.Text = ""; Ref_ProductCategory newProdCategory = new Ref_ProductCategory(); newProdCategory.CategoryName = (fviewProdCategory.FindControl("txtProductCategoryName") as TextBox).Text.Trim().ToUpper(); newProdCategory.Description = (fviewProdCategory.FindControl("txtProductCategoryDescription") as TextBox).Text.Trim(); newProdCategory.CreatedBy = User.Identity.Name.ToString(); //check if the category name already exist //get existing categories Ref_ProductCategoryList existingCategories = Ref_ProductCategoryManager.GetList(); // find a match IEnumerable <Ref_ProductCategory> query = from cat in existingCategories where cat.CategoryName == newProdCategory.CategoryName select cat; // if there is no match, then proceed with add if (!query.Any()) { //add the new product category Ref_ProductCategoryManager.Save(newProdCategory); //clear the inputs (fviewProdCategory.FindControl("txtProductCategoryName") as TextBox).Text = ""; (fviewProdCategory.FindControl("txtProductCategoryDescription") as TextBox).Text = ""; } else { lblMessage.Text = "Category name" + newProdCategory.CategoryName + " already exist."; lblMessage.ForeColor = System.Drawing.Color.Red; (fviewProdCategory.FindControl("txtProductCategoryName") as TextBox).Focus(); } PopulateGridview(); gviewProductCategory.Focus(); }
public static Ref_ProductCategoryList GetList() { Ref_ProductCategoryList aList = null; MyDBConnection myConn = new MyDBConnection(); SqlConnection conn = new SqlConnection(); SqlDataReader dr; SqlCommand cmd = null; string sql = "Select * from AquaOne.dbo.ProductCategory"; try { // Open the connection conn = myConn.OpenDB(); cmd = new SqlCommand(sql, conn); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); if (dr.HasRows) { aList = new Ref_ProductCategoryList(); while (dr.Read()) { aList.Add(FillDataRecord(dr)); } } cmd.Dispose(); dr.Close(); } finally { myConn.CloseDB(conn); } return(aList); }