protected void btnProfileSave_Click(object sender, EventArgs e) { ///[--week10--]this function handles the click event of the Save button ///it create instance of the clsSuperHeroCollection class and calls it SuperHero clsSuperHeroCollection theSuperHeroCollection = new clsSuperHeroCollection(); //declares the variables to store the form data for new SuperHero string SuperHeroID = lblProfileSuperHeroID.Text; //get the nickname from forms textbox and store in associated string variable string Nickname = tbxProfileName.Text; //get the gender from forms dropdown list and store in associated string variable string Gender = ddlProfileGender.Text; //get the height from forms textbox and store it in the associated string variable string Height = tbxProfileHeight.Text; //get the weight from forms textbox and store it in the associated string variable string Weight = tbxProfileWeight.Text; //get the BirthDate from forms textbox and store it in associated string variable string BirthDate = tbxProfileBirthDate.Text; //get the City drop down list SelectedValue and store it in associated string variable Int32 CityNo = Convert.ToInt32(ddlProfileCity.SelectedValue); //get city name from ddl and assign it to the citName string variable string CityName = Convert.ToString(ddlProfileCity.SelectedItem); //string variable to store concatenated error messages string CheckNoValidationErrors = ""; //DateTime type variable declaration and initialisation to be used to store The BirthDate in DateTime format DateTime TheBirthDate = Convert.ToDateTime("01/01/0001"); //int type variable declaration and initialisation of variable to store calculated age (calculated using current age and date of birth) Int32 Age = 0; // int type variable declaration and initialisation of variable to store integer value of weight Int32 Weight_kg = 0; //Decimal variable to store the heights decimal value decimal Height_m = 0; //declaration of variables to store boolean value for speed Boolean Speed = cbxProfileSpeed.Checked; //declaration of variables to store boolean value for strength Boolean Strength = cbxProfileStrength.Checked; //declaration of variables to store boolean value for flight Boolean Flight = cbxProfileFlight.Checked; //declaration of variables to store boolean value for teleportation Boolean Teleportation = cbxProfileTeleportation.Checked; //declaration of variables to store boolean value for invisibility Boolean Invisibility = cbxProfileInvisibility.Checked; //declaration of variables to store boolean value for telekenisis Boolean Telekenisis = cbxProfileTelekenisis.Checked; //declaration of variables to store boolean value for pychokenisis Boolean Psychokenisis = cbxProfilePsychokenesis.Checked; //create and instance of the clsSuperHero clsSuperHero aSuperHero = new clsSuperHero(); /// validate the data from the form using the ValidateSuperHeroData function /// and returned validation error messages and store in CheckNoValidationErrors string CheckNoValidationErrors = aSuperHero.ValidateSuperheroData(SuperHeroID, Nickname, Height, Weight, BirthDate, CityName); //If theres no validation errors then do this section of code: if (CheckNoValidationErrors == "") { //If there are no validation errors then do some calculations or conversions that we need //find age using birthday and current date Age = aSuperHero.CalculateAge(Convert.ToDateTime(BirthDate), DateTime.Today); //Converts age from integer to string and displays the age as text lblProfileCalculatedAge.Text = Convert.ToString(Age); //convert the weight into integer and store in the weight_kg integer variable Weight_kg = Convert.ToInt32(Weight); //convert the height in to integer and store in height_m decimal variable Height_m = Convert.ToDecimal(Height); //convert the birth date into a datatime variable and store in TheBirthDate DateTime variable TheBirthDate = Convert.ToDateTime(BirthDate); //convert SuperHeroID in to an integer and store in integer variable theSuperHeroID Int32 theSuperHeroID = Convert.ToInt32(SuperHeroID); //Check the label for SuperHeroID has no entered number (==0). if (theSuperHeroID == 0) { //[03/02/2019] assign the values to the variables of the ThisSuperHero property. //No need to include the SuperHeroID as we are creating a new entry, the ID is automatically assigned in tblSuperHero //put the Nickname into the Nickname property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Nickname = Nickname; //put the Gender into the Gender property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Gender = Gender; //put the Height_m into the Height_m property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Height_m = Height_m; //put the Weight_kg into the Weight_kg property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Weight_kg = Weight_kg; //put the TheBirthDate into the TheBirthDate property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.BirthDate = TheBirthDate; //put the Age into the Age property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Age = Age; //put the CityNo into the CityNo property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.CityNo = CityNo; //put the Speed into the Speed property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Speed = Speed; //put the Strength into the Strength property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Strength = Strength; //put the Flight into the Flight property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Flight = Flight; //put the Teleportation into the Teleportation property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Teleportation = Teleportation; //put the Invisibility into the Invisibility property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Invisibility = Invisibility; //put the Telekenisis into the Telekenisis property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Telekenisis = Telekenisis; //put the Psychokenisis into the Psychokenisis property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Psychokenisis = Psychokenisis; try { //try this code to see if form fields have correct data type and no empty fields to cause errors //Following code creates a new entry in the database if no value SuperHeroID is in the textbox txtProfileSuperHeroID //Use onSuperHero ot invoke the Add() method of the object by passing it the data from the form, store return value in Sucess if the Boolean success = theSuperHeroCollection.Add(); //check if the above value is true if (success == true) { //[23/01/2019] //int count = 0; //while(count <= 10000000) ///this code was suppose to provide a delay but did not work in that way. //The idea was to display the message "saved successfully" for an instance so it could be seen by user before redirection. //{ //lblProfileMessage.Text = "Saved Successfully"; //MessageBox.Show("Saved successfully"); //this messagebox code did not work //} //if the superhero was added successfully then send to confirmation page Response.Redirect("SaveConfirmation.aspx"); } else { //if the superhero was not added then display an error lblProfileMessage.Text = "Error1:~ Save Failed.\n " + CheckNoValidationErrors; //[25/01/1029] had to add the ValidationErrors on the end; } } catch { //if the above try statement fails then sidplay an error lblProfileMessage.Text = "Error2:~ Failed to execute Add(). "; } } //[22/01/2019] if theSuperHeroID is not 0 then do this if (theSuperHeroID > 0) { //[03/02/2019] assign the values to the variables of the ThisSuperHero property, this time include the SuperHeroID, //this is used to select the row to update in the tblSuperHero table //put the SuperHeroID into the SuperHeroID property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.SuperHeroID = theSuperHeroID; //put the Nickname into the Nickname property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Nickname = Nickname; //put the Gender into the Gender property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Gender = Gender; //put the Height_m into the Height_m property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Height_m = Height_m; //put the Weight_kg into the Weight_kg property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Weight_kg = Weight_kg; //put the TheBirthDate into the TheBirthDate property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.BirthDate = TheBirthDate; //put the Age into the Age property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Age = Age; //put the CityNo into the CityNo property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.CityNo = CityNo; //put the Speed into the Speed property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Speed = Speed; //put the Strength into the Strength property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Strength = Strength; //put the Flight into the Flight property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Flight = Flight; //put the Teleportation into the Teleportation property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Teleportation = Teleportation; //put the Invisibility into the Invisibility property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Invisibility = Invisibility; //put the Telekenisis into the Telekenisis property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Telekenisis = Telekenisis; //put the Psychokenisis into the Psychokenisis property of the ThisSuperHero object theSuperHeroCollection.ThisSuperHero.Psychokenisis = Psychokenisis; try { ///[24/01/2019] This code is similar to the Add function. It has been ammended to update an entry. ///This time it uses Update funtion instead of Add Boolean success = theSuperHeroCollection.Update(); //check if the value of success is true if (success == true) { //[23/01/2019] //int count = 0; //while(count <= 10000000) ///this code was suppose to provide a delay but did not work in that way. //The idea was to display the message "saved successfully" for an instance so it could be seen by user before redirection. //{ //lblProfileMessage.Text = "Saved Successfully"; //MessageBox.Show("Saved successfully"); //this messagebox code did not work //} //since above did not work decided to send to confirm page Response.Redirect("SaveConfirmation.aspx"); } else { //if sucess is false the superheor wasnt updated the display an error lblProfileMessage.Text = "Error3:~ Save Failed.\n " + CheckNoValidationErrors; //[25/01/1029] had to add the ValidationErrors on the end } } catch { //if the try statement fails display an error lblProfileMessage.Text = "Error4:~ Failed to execute Update()."; } } else { //if SuperHeroID is less than 0 then display an error lblProfileMessage.Text = "Error5:~ Invalid SuperHero ID."; } } else { //if there are validation errors then display the errror messages lblProfileMessage.Text = CheckNoValidationErrors; } }