using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Configuration; using System.Data.OleDb; using System.Data; using System.Text; using System.Threading; using System.Globalization; public partial class Search : System.Web.UI.Page { private DataSet ds = new DataSet(); protected void Page_Load(object sender, EventArgs e) { if (Request.IsAuthenticated == false) { Response.Redirect("~/Account/Login.aspx"); } if (!Page.IsPostBack) { LoadDefaultValuesFromDatabase(); } } private void LoadDefaultValuesFromDatabase() { string connectionString = WebConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString; OleDbConnection oleDBConn = new OleDbConnection(connectionString); string sql = "SELECT curSingleRate, " + "curCoupleRate, " + "curFamilyRate, " + "intBatchRenewalYear " + "FROM BERPPlanAdministration"; OleDbCommand cmd = new OleDbCommand(sql, oleDBConn); oleDBConn.Open(); OleDbDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Session["SingleRate"] = (decimal)reader[0]; Session["CoupleRate"] = (decimal)reader[1]; Session["FamilyRate"] = (decimal)reader[2]; Session["RenewalYear"] = (Int16)reader[3]; } reader.Close(); oleDBConn.Close(); } protected void btnSearch_Click(object sender, EventArgs e) { this.lblError.Visible = false; Page.Validate(); if (Page.IsValid) { this.gvAccountSearch.SelectedIndex = -1; this.gvAccountSearch.PageIndex = 0; this.txtBoxAmountPaidByCash.Text = "0"; this.txtBoxAmountPaidByCheque.Text = "0"; this.txtBoxChequeNumber.Text = ""; this.txtboxBatchDetailsComments.Text = ""; this.pnlRenewal.Visible = false; this.rbCash.Checked = false; this.rbCheque.Checked = false; this.rbCashCheque.Checked = false; } } protected void btnClear_Click(object sender, EventArgs e) { txtBoxAccountID.Text = ""; txtBoxLastName.Text = ""; txtBoxFirstName.Text = ""; txtBoxTelephone.Text = ""; ddlJamatkhana.SelectedIndex = 0; this.gvAccountSearch.PageIndex = 0; this.lblError.Visible = false; this.txtBoxAmountPaidByCash.Text = "0"; this.txtBoxAmountPaidByCheque.Text = "0"; this.txtBoxChequeNumber.Text = ""; this.txtboxBatchDetailsComments.Text = ""; this.pnlRenewal.Visible = false; this.rbCash.Checked = false; this.rbCheque.Checked = false; this.rbCashCheque.Checked = false; } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { if (txtBoxAccountID.Text != string.Empty) { args.IsValid = true; } else if ((txtBoxLastName.Text != string.Empty) || (txtBoxFirstName.Text != string.Empty) || (ddlJamatkhana.SelectedIndex != 0)) { args.IsValid = true; } else if (txtBoxTelephone.Text != string.Empty) { args.IsValid = true; } else { args.IsValid = false; } } protected void gvAccountSearch_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.btnSearch_Click(sender, e); this.gvAccountSearch.PageIndex = e.NewPageIndex; } protected string FormatPostalCode(object objPostalCode, object objProvID) { //For all non-Canadian provinces, do not reformat the postalcodes. int iProvID = (int)objProvID; //Assume all Provincial IDs greater than 13 are non-Canadian if (iProvID > 13) return (string)objPostalCode; //Else if this is a Canadian postal code, then reformat with hyphen StringBuilder sb = new StringBuilder(); try { string sPostalCode = (string)objPostalCode; sb.Append(sPostalCode[0]); sb.Append(sPostalCode[1]); sb.Append(sPostalCode[2]); sb.Append("-"); sb.Append(sPostalCode[3]); sb.Append(sPostalCode[4]); sb.Append(sPostalCode[5]); } catch { sb.Append(""); } return sb.ToString(); } protected string FormatPhoneNumber(object objPhoneNumber) { StringBuilder sb = new StringBuilder(); try { string sPhoneNumber = (string)objPhoneNumber; sb.Append("("); sb.Append(sPhoneNumber[0]); sb.Append(sPhoneNumber[1]); sb.Append(sPhoneNumber[2]); sb.Append(") "); sb.Append(sPhoneNumber[3]); sb.Append(sPhoneNumber[4]); sb.Append(sPhoneNumber[5]); sb.Append("-"); sb.Append(sPhoneNumber[6]); sb.Append(sPhoneNumber[7]); sb.Append(sPhoneNumber[8]); sb.Append(sPhoneNumber[9]); } catch { sb.Append(""); } return sb.ToString(); } protected void dvAccountDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { //Get the culture property of the thread. CultureInfo ci = Thread.CurrentThread.CurrentCulture; //Create TextInfo object. TextInfo ti = ci.TextInfo; e.NewValues["AccountUpdateDate"] = DateTime.Now; string strAddress = (string)e.NewValues["AccountStreetAddress"]; e.NewValues["AccountStreetAddress"] = ti.ToTitleCase(strAddress.Trim()); string strPostalCode = (string)e.NewValues["AccountPostalCode"]; e.NewValues["AccountPostalCode"] = strPostalCode.Trim().ToUpper(); } protected void adsAccount_Updated(object sender, SqlDataSourceStatusEventArgs e) { lblError.Visible = true; if (e.AffectedRows > 0) { lblError.Text = "Account details have been successfully updated."; } else { lblError.Text = "ERROR: Failed to update the the Account details. Please try again."; } } protected void gvAccountSearch_SelectedIndexChanged(object sender, EventArgs e) { if (Context.User.IsInRole("BerpReaders")) { this.pnlRenewal.Visible = false; } else { //this.pnlRenewal.Visible = true; this.pnlRenewal.Visible = false; } this.lblError.Visible = false; int selectedIndex = gvAccountSearch.SelectedIndex; // You can retrieve the key field from the SelectedDataKey property int accountID = (int)gvAccountSearch.SelectedDataKey.Values["AccountID"]; int previousRenewalYear = GetPreviousRenewalYearForSelectedAccount(accountID); int count = GetFamilyCountForSelectedAccount(accountID); if (count == 1) { this.lblCategory.Text = "SINGLE"; if (Session["SingleRate"] == null) { this.lblAmountDue.Text = "0"; } else { this.lblAmountDue.Text = Convert.ToDecimal(Session["SingleRate"]).ToString(); ; //Convert.ToDecimal(Session["SingleRate"]).ToString("C"); } } else if (count == 2) { this.lblCategory.Text = "COUPLE"; if (Session["CoupleRate"] == null) { this.lblAmountDue.Text = "0"; } else { this.lblAmountDue.Text = Convert.ToDecimal(Session["CoupleRate"]).ToString(); } } else if (count > 2) { this.lblCategory.Text = "FAMILY"; if (Session["FamilyRate"] == null) { this.lblAmountDue.Text = "0"; } else this.lblAmountDue.Text = Convert.ToDecimal(Session["FamilyRate"]).ToString(); } else { this.lblCategory.Text = "Unknown"; if (Session["SingleRate"] == null) { this.lblAmountDue.Text = "0"; } else this.lblAmountDue.Text = Convert.ToDecimal(Session["SingleRate"]).ToString(); } this.lblRenewalYear.Text = Convert.ToInt16(Session["RenewalYear"]).ToString(); this.lblPreviousRenewalYear.Text = previousRenewalYear.ToString(); this.txtBoxAmountPaidByCash.Text = "0"; // Convert.ToDecimal(0).ToString("C"); this.txtBoxAmountPaidByCheque.Text = "0"; // Convert.ToDecimal(0).ToString("C"); this.txtBoxChequeNumber.Text = ""; this.txtboxBatchDetailsComments.Text = ""; this.rbCash.Checked = false; this.rbCheque.Checked = false; this.rbCashCheque.Checked = false; this.lblError.Text = ""; } private int GetFamilyCountForSelectedAccount(int accountID) { int familyCount = 0; string connectionString = WebConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString; OleDbConnection oleDBConn = new OleDbConnection(connectionString); string sql = "SELECT COUNT(*) FROM Members WHERE AccountID = " + accountID.ToString() + " AND Members.MemberStatusID = 1"; OleDbCommand cmd = new OleDbCommand(sql, oleDBConn); oleDBConn.Open(); familyCount = (int)cmd.ExecuteScalar(); oleDBConn.Close(); return familyCount; } private int GetPreviousRenewalYearForSelectedAccount(int accountID) { int renewalYear = 0; string connectionString = WebConfigurationManager.ConnectionStrings["AccessDB"].ConnectionString; OleDbConnection oleDBConn = new OleDbConnection(connectionString); string sql = "SELECT AccountRenewalYear FROM Accounts WHERE AccountID = " + accountID.ToString(); OleDbCommand cmd = new OleDbCommand(sql, oleDBConn); oleDBConn.Open(); renewalYear = (short)cmd.ExecuteScalar(); oleDBConn.Close(); return renewalYear; } protected void ChequeNumberCustomValidator_ServerValidate(object source, ServerValidateEventArgs args) { if (txtBoxAmountPaidByCheque.Text == string.Empty) { args.IsValid = false; } else if (txtBoxChequeNumber.Text == string.Empty) { decimal cheque = Convert.ToDecimal(txtBoxAmountPaidByCheque.Text); if (cheque > 0.0M) args.IsValid = false; else if (cheque == 0.0M) args.IsValid = true; } else { args.IsValid = true; } } protected void TotalAmountCustomValidator_ServerValidate(object source, ServerValidateEventArgs args) { decimal cash = Convert.ToDecimal(txtBoxAmountPaidByCash.Text); decimal cheque = Convert.ToDecimal(txtBoxAmountPaidByCheque.Text); decimal total = Convert.ToDecimal(lblAmountDue.Text); if ( (txtBoxAmountPaidByCash.Text == string.Empty) || (txtBoxAmountPaidByCheque.Text == string.Empty) ) { args.IsValid = false; } if (cash + cheque == 0.0M) //(cash + cheque < total) { args.IsValid = false; } else { args.IsValid = true; } } protected void btnAddToCart_Click(object sender, EventArgs e) { Page.Validate(); if (Page.IsValid) { //Get the culture property of the thread. CultureInfo ci = Thread.CurrentThread.CurrentCulture; //Create TextInfo object. TextInfo ti = ci.TextInfo; Account selectedAccount = new Account(); selectedAccount.iAccountID = (int)gvAccountSearch.SelectedDataKey.Values["AccountID"]; selectedAccount.dAmountDue = Convert.ToDecimal(this.lblAmountDue.Text); selectedAccount.dAmountPaidCash = Convert.ToDecimal(this.txtBoxAmountPaidByCash.Text); selectedAccount.dAmountPaidCheque = Convert.ToDecimal(this.txtBoxAmountPaidByCheque.Text); // You can retrieve other data directly from the Cells coll // as long as you know the column offset. if (gvAccountSearch.SelectedRow.Cells[2].Text != " ") selectedAccount.mLastName = gvAccountSearch.SelectedRow.Cells[2].Text; else gvAccountSearch.SelectedRow.Cells[2].Text = ""; if (gvAccountSearch.SelectedRow.Cells[3].Text != " ") selectedAccount.mMiddleName = gvAccountSearch.SelectedRow.Cells[3].Text; else selectedAccount.mMiddleName = ""; if (gvAccountSearch.SelectedRow.Cells[4].Text != " ") selectedAccount.mFirstName = gvAccountSearch.SelectedRow.Cells[4].Text; else selectedAccount.mFirstName = ""; if (this.txtBoxChequeNumber.Text != string.Empty) { selectedAccount.iChequeNumber = Convert.ToInt32(this.txtBoxChequeNumber.Text.Trim()); } if (this.txtboxBatchDetailsComments.Text != string.Empty) { selectedAccount.sComments = ti.ToTitleCase(txtboxBatchDetailsComments.Text.Trim()); } CartItem cartitem = new CartItem(); cartitem.cartAccount = selectedAccount; this.AddToCart(cartitem); } } private void AddToCart(CartItem cartitem) { SortedList cart = this.GetCart(); int iAccountID = cartitem.cartAccount.iAccountID; this.lblError.Visible = true; if (cart.ContainsKey(iAccountID)) { // Account already exists in the batch this.lblError.Text = "Account ID " + iAccountID.ToString() + " was not added to the batch as it already exists."; } else { cart.Add(iAccountID, cartitem); this.lblError.Text = "Account ID " + iAccountID.ToString() + " was successfully added to the batch."; } } private SortedList GetCart() { if (Session["Cart"] == null) { Session.Add("Cart", new SortedList()); } return (SortedList)Session["Cart"]; } protected void adsMembers_Updated(object sender, SqlDataSourceStatusEventArgs e) { lblError.Visible = true; if (e.AffectedRows > 0) { lblError.Text = "Member details have been successfully updated."; } else { lblError.Text = "ERROR: Failed to update the the Member details. Please try again."; } } protected void CustomValidatorRenewalYear_ServerValidate(object source, ServerValidateEventArgs args) { int prevRenewalYear = Convert.ToInt32(lblPreviousRenewalYear.Text); int currentRenewalYear = Convert.ToInt32(lblRenewalYear.Text); if (lblRenewalYear.Text == string.Empty) { args.IsValid = false; } if (prevRenewalYear == currentRenewalYear) { args.IsValid = false; } else { args.IsValid = true; } } protected void gvAccountMembers_RowUpdating(object sender, GridViewUpdateEventArgs e) { //Get the culture property of the thread. CultureInfo ci = Thread.CurrentThread.CurrentCulture; //Create TextInfo object. TextInfo ti = ci.TextInfo; string strLast = (string)e.NewValues["MemberLastName"]; e.NewValues["MemberLastName"] = ti.ToTitleCase(strLast.Trim()); string strMiddle = (string)e.NewValues["MemberMiddleName"]; if ((strMiddle != null) && (strMiddle.Length != 0)) e.NewValues["MemberMiddleName"] = ti.ToTitleCase(strMiddle.Trim()); string strFirst = (string)e.NewValues["MemberFirstName"]; e.NewValues["MemberFirstName"] = ti.ToTitleCase(strFirst.Trim()); } protected void gvAccountMembers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.DataItem != null) { if ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit) ) ) { DataRowView drv = (DataRowView)e.Row.DataItem; RangeValidator rv = (RangeValidator)e.Row.FindControl("RangeValidatorMemberBirthYear"); StringBuilder sb = new StringBuilder(); sb.Append("Birth Year must range from "); int start = DateTime.Now.Year - 110; sb.Append(start.ToString()); sb.Append(" to "); sb.Append(DateTime.Now.Year); sb.Append("."); rv.ErrorMessage = sb.ToString(); } } } protected void gvAccountMembers_RowEditing(object sender, GridViewEditEventArgs e) { this.lblError.Visible = false; } protected void dvAccountDetails_ItemCommand(object sender, DetailsViewCommandEventArgs e) { this.lblError.Visible = false; } protected void adsAccountSearch_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { if (!IsPostBack) { e.Cancel = true; } else if ((txtBoxAccountID.Text == "") && (txtBoxLastName.Text == "") && (txtBoxFirstName.Text == "") && (txtBoxTelephone.Text == "") && (ddlJamatkhana.SelectedIndex == 0)) { e.Cancel = true; } } protected void dvAccountDetails_DataBound(object sender, EventArgs e) { if (Context.User.IsInRole("BerpReaders")) { dvAccountDetails.AutoGenerateEditButton = false; } } protected void gvAccountMembers_DataBound(object sender, EventArgs e) { if (Context.User.IsInRole("BerpReaders")) { gvAccountMembers.AutoGenerateEditButton = false; } } protected void rbCash_CheckedChanged(object sender, EventArgs e) { this.txtBoxAmountPaidByCash.Text = this.lblAmountDue.Text; this.txtBoxAmountPaidByCheque.Text = "0"; this.txtBoxChequeNumber.Text = ""; } protected void rbCheque_CheckedChanged(object sender, EventArgs e) { this.txtBoxAmountPaidByCash.Text = "0"; this.txtBoxAmountPaidByCheque.Text = this.lblAmountDue.Text; this.txtBoxChequeNumber.Text = ""; } protected void rbCashCheque_CheckedChanged(object sender, EventArgs e) { int halfAmount = Convert.ToInt32(this.lblAmountDue.Text)/2; this.txtBoxAmountPaidByCash.Text = halfAmount.ToString(); this.txtBoxAmountPaidByCheque.Text = halfAmount.ToString(); this.txtBoxChequeNumber.Text = ""; } }