protected void btnAdd_Click(object sender, EventArgs e) { #region ServerSide Validation String strErrorMessage = ""; if (txtCategoryName.Text == "") { strErrorMessage += "Enter CategoryName"; } if (strErrorMessage != "") { lblMessage.Text = strErrorMessage; divMessage.Visible = true; return; } #endregion ServerSide Validation #region Collect FormData CategoryENT entCategory = new CategoryENT(); if (txtCategoryName.Text != "") { entCategory.CategoryName = txtCategoryName.Text.Trim(); } #endregion Collect FormData CategoryBAL balCategory = new CategoryBAL(); if (Request.QueryString["CategoryID"] == null) { if (balCategory.Insert(entCategory)) { ClearControls(); lblMessage.Text = "Add SuccessFully"; divMessage.Visible = true; } else { lblMessage.Text = balCategory.Message; divMessage.Visible = true; } } else { entCategory.CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]); if (balCategory.Update(entCategory)) { ClearControls(); Response.Redirect("~/AdminPanel/Category/CategoryList.aspx"); } else { lblMessage.Text = balCategory.Message; divMessage.Visible = true; } } }
public CategoryENT SelectByPK(SqlInt32 CategoryID) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Category_SelectByPK"; objCmd.Parameters.AddWithValue("@CategoryID", CategoryID); #endregion Prepare Command #region ReadData and Set Controls CategoryENT entCategory = new CategoryENT(); using (SqlDataReader objSDR = objCmd.ExecuteReader()) { while (objSDR.Read()) { if (!objSDR["CategoryID"].Equals(DBNull.Value)) { entCategory.CategoryID = Convert.ToInt32(objSDR["CategoryID"]); } if (!objSDR["CategoryName"].Equals(DBNull.Value)) { entCategory.CategoryName = Convert.ToString(objSDR["CategoryName"]); } } } return(entCategory); #endregion ReadData and Set Controls } catch (SqlException sqlex) { Message = sqlex.InnerException.Message.ToString(); return(null); } catch (Exception ex) { Message = ex.InnerException.Message.ToString(); return(null); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }
private void FillControls(SqlInt32 CategoryID) { CategoryBAL balCategory = new CategoryBAL(); CategoryENT entCategory = new CategoryENT(); entCategory = balCategory.SelectByPK(CategoryID); if (!entCategory.CategoryName.IsNull) { txtCategoryName.Text = entCategory.CategoryName.Value.ToString(); } }
public Boolean Update(CategoryENT entCategory) { CategoryDAL dalCategory = new CategoryDAL(); if (dalCategory.Update(entCategory)) { return(true); } else { Message = dalCategory.Message; return(false); } }
public Boolean Insert(CategoryENT entCategory) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Category_Insert"; objCmd.Parameters.Add("@CategoryID", SqlDbType.Int, 4).Direction = ParameterDirection.Output; objCmd.Parameters.Add("@CategoryName", SqlDbType.VarChar).Value = entCategory.CategoryName; #endregion Prepare Command objCmd.ExecuteNonQuery(); if (objCmd.Parameters["@CategoryID"] != null) { entCategory.CategoryID = Convert.ToInt32(objCmd.Parameters["@CategoryID"].Value); } return(true); } catch (SqlException sqlex) { Message = sqlex.InnerException.Message.ToString(); return(false); } catch (Exception ex) { Message = ex.InnerException.Message.ToString(); return(false); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }
public Boolean Update(CategoryENT entCategory) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Category_Update"; objCmd.Parameters.AddWithValue("@CategoryID", entCategory.CategoryID); objCmd.Parameters.AddWithValue("@CategoryName", entCategory.CategoryName); #endregion Prepare Command objCmd.ExecuteNonQuery(); return(true); } catch (SqlException sqlex) { Message = sqlex.InnerException.Message.ToString(); return(false); } catch (Exception ex) { Message = ex.InnerException.Message.ToString(); return(false); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }