using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.OleDb; namespace Reservation { /// /// Update form. /// Methods created or modified: public class Update : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox tbName; protected System.Web.UI.WebControls.TextBox tbMail; protected System.Web.UI.WebControls.TextBox tbAddress; protected System.Web.UI.WebControls.TextBox tbCity; protected System.Web.UI.WebControls.TextBox tbZip; protected System.Web.UI.WebControls.TextBox tbRegion; protected System.Web.UI.WebControls.TextBox tbLatitude; protected System.Web.UI.WebControls.TextBox tbLongitude; protected System.Web.UI.WebControls.Button btnUpdate; protected System.Web.UI.WebControls.Label lblStatus; protected System.Web.UI.WebControls.DropDownList ddState; protected System.Web.UI.WebControls.DropDownList ddCountry; /// /// Id of the logged user /// private string id; public Update() { Page.Init += new System.EventHandler(Page_Init); } /// /// Checks credentials. /// Restores account data and populates the form. /// private void Page_Load(object sender, System.EventArgs e) { id = (string)Context.Session["userid"]; if (id == null) { Context.Session.Add("from", "Update.aspx"); Context.Response.Redirect("Login.aspx"); return; } short type = (short)Context.Session["type"]; if (type > 1) { Context.Session.Add("status", "Log-in with a customer or proxy user id"); Context.Session.Add("from", "Update.aspx"); Context.Response.Redirect("Login.aspx"); return; } if (!IsPostBack) { OleDbConnection odc = new OleDbConnection(Global.connectionString); odc.Open(); OleDbCommand odcmd = new OleDbCommand("select * from users where id='" + id.PadRight(10, ' ') + "'", odc); OleDbDataReader odr = odcmd.ExecuteReader(); if (odr.Read()) { char[] blank = new char[] { ' ' }; tbName.Text = (string)odr["name"]; tbMail.Text = (string)odr["mail"]; tbAddress.Text = (string)odr["address"]; tbCity.Text = (string)odr["city"]; ddState.DataSource = Global.stateAl; ddState.DataBind(); tbZip.Text = ((string)odr["zip"]).TrimEnd(blank); ddCountry.DataSource = Global.countryAl; ddCountry.DataBind(); setDropDown((string)odr["state"], (string)odr["country"], blank); tbRegion.Text = ((string)odr["region"]).TrimEnd(blank); tbLatitude.Text = ((string)odr["latitude"]).TrimEnd(blank); tbLongitude.Text = ((string)odr["longitude"]).TrimEnd(blank); lblStatus.Text = id + " displayed"; } odr.Close(); odc.Close(); } } /// /// Set the state and country drop down lists /// /// State or province code /// Country code /// For trim private void setDropDown(string state, string country, char[] blank) { string st = state.TrimEnd(blank); ddState.SelectedIndex = 0; if (st.Length > 0) { string sta = null; if (Global.statePerCode.Contains(st)) sta = (string)Global.statePerCode[st]; else { if (Global.provincePerCode.Contains(st)) sta = (string)Global.provincePerCode[st]; } for (int i = 0; i < Global.stateAl.Count; ++ i) { string s = (string)Global.stateAl[i]; if (s.Equals(sta)) { ddState.SelectedIndex = i; break; } } } string countryName = (string)Global.countryPerCode[country]; for (int i = 0; i < Global.countryAl.Count; ++ i) { string c = (string)Global.countryAl[i]; if (c.Equals(countryName)) { ddCountry.SelectedIndex = i; break; } } } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnUpdate.Click += new System.EventHandler(this.OnUpdate); this.Load += new System.EventHandler(this.Page_Load); } #endregion /// /// Event triggered when the user clicks on the update button. /// Updates the users database. /// private void OnUpdate(object sender, System.EventArgs e) { string st = ddState.SelectedItem.Value; string state = ""; if (!st.Equals("N/A")) { if (Global.states.Contains(st)) state = (string)Global.states[st]; else state = (string)Global.provinces[st]; } string c = ddCountry.SelectedItem.Value; string country = (string)Global.countries[c]; OleDbConnection odc = new OleDbConnection(Global.connectionString); odc.Open(); OleDbCommand odcmd = new OleDbCommand("update users set name='" + tbName.Text + "', mail='" + tbMail.Text + "', address='" + tbAddress.Text + "', city='" + tbCity.Text + "', state='" + state + "', zip='" + tbZip.Text + "', country='" + country + "', region='" + tbRegion.Text + "', latitude='" + tbLatitude.Text + "', longitude='" + tbLongitude.Text + "' where id='" + id.PadRight(10, ' ') + "'", odc); odcmd.ExecuteNonQuery(); odc.Close(); lblStatus.Text = id + " updated"; } } }