/// <summary> /// Converts a record on the USERS table to a User object /// </summary> /// <param name="row">A row from the USERS table</param> /// <returns>A fully-populated instance of User</returns> private User ConvertRecordToUser(DataRow row) { User result = new User(); int userId; if (int.TryParse(row["Id"].ToString(), out userId)) result.Id = userId; result.FirstName = row["FirstName"].ToString(); result.LastName = row["LastName"].ToString(); result.Address = row["Address"].ToString(); result.City = row["City"].ToString(); result.State = row["State"].ToString(); result.ZIP = row["ZIP"].ToString(); return result; }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.Params["id"] != null) { //* Fetch the user info from the database and populate the form int userId = int.Parse(Request.Params["id"]); User user = DBFacadeProvider.GetDBFacade().GetUserById(userId); this.txtFirstName.Text = user.FirstName; this.txtLastName.Text = user.LastName; this.txtAddress.Text = user.Address; this.txtCity.Text = user.City; this.txtState.Text = user.State; this.txtZip.Text = user.ZIP; this.hdnUserId.Value = user.Id.ToString(); } } else { //* the user is submitting a new user or changes to an existing user // that need to be stored in the DB User user = new User(); user.FirstName = this.txtFirstName.Text; user.LastName = this.txtLastName.Text; user.Address = this.txtAddress.Text; user.City = this.txtCity.Text; user.State = this.txtState.Text; user.ZIP = this.txtZip.Text; //* Check the form for a userId int userId; if (!string.IsNullOrEmpty(this.hdnUserId.Value) && int.TryParse(this.hdnUserId.Value, out userId)) user.Id = userId; DBFacadeProvider.GetDBFacade().SaveOrUpdate(user); Response.Redirect("AddressBook.aspx"); } }
/// <summary> /// Persists a User object. If the object has an Id, it updates the record, if not it creates a new record /// </summary> /// <param name="user">The user to save or update</param> /// <returns>The user object along with any changes that may have been made in the save/update process</returns> public User SaveOrUpdate(User user) { DataRow row = null; if (user.Id == -1) { row = this._dataSet.Tables[USER_TABLE_NAME].NewRow(); row["FirstName"] = user.FirstName; row["LastName"] = user.LastName; row["Address"] = user.Address; row["City"] = user.City; row["State"] = user.State; row["ZIP"] = user.ZIP; this._dataSet.Tables[USER_TABLE_NAME].Rows.Add(row); row.AcceptChanges(); row.EndEdit(); } else { DataRow[] rows = this._dataSet.Tables[USER_TABLE_NAME].Select("Id = " + user.Id.ToString()); if (rows.Length == 0) throw new ApplicationException("The Id of the User object could not be found in the database"); row = rows[0]; row["FirstName"] = user.FirstName; row["LastName"] = user.LastName; row["Address"] = user.Address; row["City"] = user.City; row["State"] = user.State; row["ZIP"] = user.ZIP; row.AcceptChanges(); row.EndEdit(); } this._dataSet.WriteXml(XML_FILE_NAME, XmlWriteMode.WriteSchema); //* Get a brand-new copy of the User straight from the database int Id = (int)row["Id"]; return this.GetUserById(Id); }