public CountryENT SelectByPK(SqlInt32 CountryID) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Country_SelectByPK"; objCmd.Parameters.AddWithValue("@CountryID", CountryID); #endregion Prepare Command #region ReadData and Set Controls CountryENT entCountry = new CountryENT(); using (SqlDataReader objSDR = objCmd.ExecuteReader()) { while (objSDR.Read()) { if (!objSDR["CountryID"].Equals(DBNull.Value)) { entCountry.CountryID = Convert.ToInt32(objSDR["CountryID"]); } if (!objSDR["CountryName"].Equals(DBNull.Value)) { entCountry.CountryName = Convert.ToString(objSDR["CountryName"]); } if (!objSDR["CountryCode"].Equals(DBNull.Value)) { entCountry.CountryCode = Convert.ToString(objSDR["CountryCode"]); } } } return(entCountry); #endregion ReadData and Set Controls } catch (SqlException sqlex) { Message = sqlex.Message.ToString(); return(null); } catch (Exception ex) { Message = ex.Message.ToString(); return(null); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }
protected void btnSave_Click(object sender, EventArgs e) { #region Server Side Validation lblError.Text = ""; if (txtCountryName.Text.Trim().ToUpper() == "") { lblError.Text += "Enter Country Name <br/>"; } if (txtCountryCode.Text.Trim().ToUpper() == "") { lblError.Text += "Enter Country Code <br/>"; } if (lblError.Text != "") { return; } #endregion Server Side Validation #region Collecting Data CountryENT entCountry = new CountryENT(); if (txtCountryCode.Text.Trim().ToUpper() != "" && txtCountryName.Text.Trim().ToUpper() != "") { entCountry.CountryName = txtCountryName.Text.Trim().ToUpper(); entCountry.CountryCode = txtCountryCode.Text.Trim().ToUpper(); } CountryBAL balCountry = new CountryBAL(); #endregion Collecting Data if (Request.QueryString["CountryID"] == null) { #region insertingData if (balCountry.Insert(entCountry)) { Response.Redirect("~/AdminPanel/Country/CountryList.aspx"); } else { lblError.Text = balCountry.Message; } #endregion insertingData } else { #region updatingData entCountry.CountryID = Convert.ToInt32(Request.QueryString["CountryID"]); if (balCountry.Update(entCountry)) { Response.Redirect("~/AdminPanel/Country/CountryList.aspx"); } else { lblError.Text = balCountry.Message; } #endregion updatingData } }
public Boolean Update(CountryENT entCountry) { CountryDAL dalCountry = new CountryDAL(); if (dalCountry.Update(entCountry)) { return(true); } else { Message = dalCountry.Message; return(false); } }
public Boolean Insert(CountryENT entCountry) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Country_Insert"; objCmd.Parameters.Add("@CountryID", SqlDbType.Int).Direction = ParameterDirection.Output; objCmd.Parameters.AddWithValue("@CountryName", SqlDbType.VarChar).Value = entCountry.CountryName; objCmd.Parameters.AddWithValue("@CountryCode", SqlDbType.VarChar).Value = entCountry.CountryCode; #endregion Prepare Command objCmd.ExecuteNonQuery(); if (objCmd.Parameters["@CountryID"] != null) { entCountry.CountryID = Convert.ToInt32(objCmd.Parameters["@CountryID"].Value); } return(true); } catch (SqlException sqlex) { Message = sqlex.Message.ToString(); return(false); } catch (Exception ex) { Message = ex.Message.ToString(); return(false); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }
public Boolean Update(CountryENT entCountry) { using (SqlConnection objConn = new SqlConnection(ConnectionString)) { objConn.Open(); using (SqlCommand objCmd = objConn.CreateCommand()) { try { #region Prepare Command objCmd.CommandType = CommandType.StoredProcedure; objCmd.CommandText = "PR_Country_UpdateByPK"; objCmd.Parameters.AddWithValue("@CountryID", entCountry.CountryID); objCmd.Parameters.AddWithValue("@CountryName", entCountry.CountryName); objCmd.Parameters.AddWithValue("@CountryCode", entCountry.CountryCode); #endregion Prepare Command objCmd.ExecuteNonQuery(); return(true); } catch (SqlException sqlex) { Message = sqlex.Message.ToString(); return(false); } catch (Exception ex) { Message = ex.Message.ToString(); return(false); } finally { if (objConn.State == ConnectionState.Open) { objConn.Close(); } } } } }
private void fillControls(SqlInt32 CountryID) { CountryENT entCountry = new CountryENT(); CountryBAL balCountry = new CountryBAL(); entCountry = balCountry.SelectByPK(CountryID); if (entCountry != null) { if (!entCountry.CountryName.IsNull) { txtCountryName.Text = entCountry.CountryName.Value.ToString(); } if (!entCountry.CountryCode.IsNull) { txtCountryCode.Text = entCountry.CountryCode.Value.ToString(); } } else { lblError.Text = balCountry.Message; } }