예제 #1
0
    //This function will find the superhero vehicle using the SuperHero primary key in the tblSuperHero, which is a foreign key in the tblSuperCars
    Boolean DisplaySuperHeroVehicle(Int32 ForeignSuperHeroIDKeyIn)
    {
        try
        {
            //declare a string to store the SuperVehicleID
            string SuperVehicleID = "";
            //declare a string to store the SuperVehicleMake
            string SuperVehicleMake = "";
            //declare a string to store the SuperVehicleModel
            string SuperVehicleModel = "";
            //declare a string to store the SuperVehicleColor
            string SuperVehicleColor = "";
            //declare a string to store the SuperVehicleLitreEngine
            string SuperVehicleLitreEngine = "";
            //declare a string to store the SuperVehicleSunroof
            string SuperVehicleSunroof = "";
            //declare a DateTime variable to store SuperVehicleRegistrationDateTime
            DateTime SuperVehicleRegistrationDateTime = Convert.ToDateTime("01 / 01 / 0001");
            //declare a string to store the SuperVehicleRegistrationDate
            string SuperVehicleRegistrationDate = "";
            //create the connection to access the database
            clsDataConnection mDatabase = new clsDataConnection();
            //create an instance of the clsSuperVehicleCollection class to work with
            clsSuperVehicleCollection aVehicleCollection = new clsSuperVehicleCollection();
            //invoke the function to get the vehicle data from the database and fill the SuperVehicleList object
            aVehicleCollection.VehicleSearchByParameter(ForeignSuperHeroIDKeyIn);
            //check the count of vehicles found in the database that match for the SuperHeroID is  1
            //each superhero has only one vehicle so there should only be one SuperVehicle entry in the DataTable at index=0
            //get the SuperVehicleID and store this in the SuperVehicleID string variable
            SuperVehicleID = Convert.ToString(aVehicleCollection.SuperVehicleList[0].VehicleID);
            //get the SuperVehicleMake and store this in the SuperVehicleMake string variable
            SuperVehicleMake = Convert.ToString(aVehicleCollection.SuperVehicleList[0].VehicleMake);
            //get the SuperVehicleModel and store this in the SuperVehicleModel string variable
            SuperVehicleModel = Convert.ToString(aVehicleCollection.SuperVehicleList[0].VehicleModel);
            //get the SuperVehicleColor and store this in the SuperVehicleColor string variable
            SuperVehicleColor = Convert.ToString(aVehicleCollection.SuperVehicleList[0].VehicleColor);
            //get the SuperVehicleLitreEngine and store this in the SuperVehicleLitreEngine string variable
            SuperVehicleLitreEngine = Convert.ToString(aVehicleCollection.SuperVehicleList[0].VehicleLitreEngine);
            //if the SuperVehicleSunroof boolean value is true then it has a Sunroof else sunroof is blank
            if (aVehicleCollection.SuperVehicleList[0].VehicleSunroof)
            {
                SuperVehicleSunroof = " with sunroof, ";
            }
            else
            {
                SuperVehicleSunroof = ", ";
            }

            //get the DateTime of the VehicleRegistrationDate
            SuperVehicleRegistrationDateTime = aVehicleCollection.SuperVehicleList[0].VehicleRegistrationDate;
            //convert the registration date to a suitable format and stores it back in SuperVehicleRegistration
            //the clsSuperHero class contains a method to format the date
            clsSuperHero dateSuperHero = new clsSuperHero();
            //format the datetime variable and store it in the string variable
            SuperVehicleRegistrationDate = dateSuperHero.FormatDate(SuperVehicleRegistrationDateTime);
            //Concatenate the results and display the string in the lable
            lblDefaultSuperCarDetails.Text = "Vehicle ID [" + SuperVehicleID + "] " + "A " + SuperVehicleColor + ", " + SuperVehicleLitreEngine + "Litre, " + SuperVehicleMake + " " + SuperVehicleModel + SuperVehicleSunroof + "registered on " + SuperVehicleRegistrationDate;
            //return true the code works
            return(true);
        }
        catch
        {
            //if the code doesnt work return false
            return(false);
        }
    }
예제 #2
0
    Int32 DisplaySuperHeros(string SearchParameter)
    {
        //clear the listbox
        lstDefaultDetails.Items.Clear();
        //string variable to display "Super Speed"
        string SuperSpeed = "";
        //string variable to display "Super Strength"
        string SuperStrength = "";
        //string variable to display "Super Flight"
        string SuperFlight = "";
        //string variable to display "Super Teleportation"
        string SuperTeleportation = "";
        //string variable to display "Super Invisibility"
        string SuperInvisibility = "";
        //string variable to display "Super Telekenisis"
        string SuperTelekenisis = "";
        //string variable to display "Super Psychokenisis"
        string SuperPsychokenisis = "";
        //variable to store the primary key
        Int32 SuperHeroID;
        //string variable for the Nickname
        String Nickname;
        //string variable for the Gender
        String Gender;
        //decimal variable to store Height_m
        Decimal Height;
        //integer variable to store Weight_kg
        Int32 Weight_kg;
        //DateTime variable to store birthdate
        DateTime BirthDate;
        //[27/02/2019] This line was added after the FormatDate function was created, so date can be displayed nicely in DD/MM/YYYY
        //string variable stores the formated date to be displayed
        String DisplayDate;
        //foreign key reference for City name in the SuperHeros Database (stored as a string value, thats why CityNo is a string)
        Int32 CityNo;
        //string variable to store the City name that will be retrieved from the City database using the CityNo
        string CityName;
        //Int32 Age;          // variable for the Age is obscelete as the age is now calculated from the date
        //boolean value to store the boolean for speed in data base
        Boolean Speed;
        //boolean value to store the boolean for strength
        Boolean Strength;
        //boolean value to store the boolean for flight
        Boolean Flight;
        //boolean value to store the boolean for teleportation
        Boolean Teleportation;
        //boolean value to store the boolean for invisibility
        Boolean Invisibility;
        //boolean value to store the boolean for telekenisis
        Boolean Telekenisis;
        //boolean value to store the boolean for psychokenisis
        Boolean Psychokenisis;
        //integer variable to store age which will be calculated from the BirthDate and CurrentDate
        Int32 CurrentAge;
        //create an instance of the clsSuperHeroCollection class to give us access to data we need.
        ///The list is populated when we create an intsance of the class.
        ///create a SuperHeroCollection object, to get access to methods
        clsSuperHeroCollection newSuperHeroCollection = new clsSuperHeroCollection();

        //execute the search method in the object, passing the search parameter
        //returns the number of records in newSuperHeroCollection and assigned to recordCount variable
        newSuperHeroCollection.SearchSuperHeros(SearchParameter);
        //declare integet to store the record count
        Int32 recordCount;

        recordCount = newSuperHeroCollection.Count;
        //initialise the Variable to store the index of the loop (used to point to SuperHero data we want)
        Int32 Pointer = 0;
        //declare a new instance of the newSuperHero so that we can use it to access functions for calculate age and format date
        clsSuperHero newSuperHero = new clsSuperHero();

        //while loop that traverses the rows and gets the dat in each (named) column
        while (Pointer < recordCount)
        {
            /// obtain the desired data from the populated superHeroList from the instance of clsSuperHeroCollection
            /// newSuperHeroCollection is the instance of clsSuperHeroCollection to will
            //Get primary key from the data table
            SuperHeroID = newSuperHeroCollection.SuperHeroList[Pointer].SuperHeroID;
            //get Nickname from the data table
            Nickname = newSuperHeroCollection.SuperHeroList[Pointer].Nickname;
            //get Gender from the data table
            Gender = newSuperHeroCollection.SuperHeroList[Pointer].Gender;
            //get the weight from the data table
            Weight_kg = newSuperHeroCollection.SuperHeroList[Pointer].Weight_kg;
            //get the height from the datatable
            Height = newSuperHeroCollection.SuperHeroList[Pointer].Height_m;
            //get the birthdate from the data table
            BirthDate = newSuperHeroCollection.SuperHeroList[Pointer].BirthDate;
            //[27/01/2019] Formats and assigns date to the DisplayDate string.
            DisplayDate = newSuperHero.FormatDate(BirthDate);
            //[27/01/2019] Following code is obscelete as the age can be calculated directly
            //Age = newSuperHeroCollection.SuperHeroList[Pointer].Age; // get age
            //calculates the current age based on BirthDate and CurrentDate
            CurrentAge = newSuperHero.CalculateAge(BirthDate, DateTime.Today);
            //[02/02/2019] Adding the clsCityCollection and clsCity classes and having a seperate database for cities for the cities drop down list (in Profile.aspx)
            //meant SelectedValue for the Cities drop down list is passed to the database table for SuperHeros rather than the name.
            //the CityNo is being used as a foriegn key in the SuperHeros database and therefore when copied from the database a number appears in the listbox in Default.aspx.
            //However this number can be converted back to the city name by creating an instance of the clsCityCollection, which can then be used to copy city from the datatable.
            //Get the foreign key CityNo (now stored in City) from the SuperHeros database and convert it to an integer
            CityNo = newSuperHeroCollection.SuperHeroList[Pointer].CityNo;
            //create an instance of the clsCityCollection to work with
            clsCityCollection theCities = new clsCityCollection();
            //Pass in the search parameter (to populate the the list array object called CitiesList)
            theCities.CitySearchParameter(CityNo);
            //Get the CityName to be displayed based on the CityNo
            CityName = theCities.CityList[0].CityName;
            //get the boolean value of speed thats in the datatable
            Speed = newSuperHeroCollection.SuperHeroList[Pointer].Speed;
            //if the value is true, assign some text, else assign nothing
            if (Speed == true)
            {
                SuperSpeed = ", Super Speed";
            }
            else
            {
                SuperSpeed = "";
            }
            //get the boolean value of Strength thats in the datatable
            Strength = newSuperHeroCollection.SuperHeroList[Pointer].Strength;
            //if the value is true, assign some text, else assign nothing
            if (Strength == true)
            {
                SuperStrength = ", Super Strength";
            }
            else
            {
                SuperStrength = "";
            }
            //get the boolean value of Flight thats in the datatable
            Flight = newSuperHeroCollection.SuperHeroList[Pointer].Flight;
            //if the value is true, assign some text, else assign nothing
            if (Flight == true)
            {
                SuperFlight = ", Flight";
            }
            else
            {
                SuperFlight = "";
            }
            //get the boolean value of Teleportation thats in the datatable
            Teleportation = newSuperHeroCollection.SuperHeroList[Pointer].Teleportation;
            //if the value is true, assign some text, else assign nothing
            if (Teleportation == true)
            {
                SuperTeleportation = ", Teleportation";
            }
            else
            {
                SuperTeleportation = "";
            }
            //get the boolean value of Invisibility thats in the datatable
            Invisibility = newSuperHeroCollection.SuperHeroList[Pointer].Invisibility;
            //if the value is true, assign some text, else assign nothing
            if (Invisibility == true)
            {
                SuperInvisibility = ", Invisibility";
            }
            else
            {
                SuperInvisibility = "";
            }
            //get the boolean value of Telekenisis thats in the datatable
            Telekenisis = newSuperHeroCollection.SuperHeroList[Pointer].Telekenisis;
            //if the value is true, assign some text, else assign nothing
            if (Telekenisis == true)
            {
                SuperTelekenisis = ", Telekenisis";
            }
            else
            {
                SuperTelekenisis = "";
            }
            //get the boolean value of Psychokenisis thats in the datatable
            Psychokenisis = newSuperHeroCollection.SuperHeroList[Pointer].Psychokenisis;
            //if the value is true, assign some text, else assign nothing
            if (Psychokenisis == true)
            {
                SuperPsychokenisis = ", Psychokenisis";
            }
            else
            {
                SuperPsychokenisis = "";
            }

            ///List item is defined: public ListItem (string text, string value);
            ///https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listitem.-ctor?view=netframework-4.7.2#System_Web_UI_WebControls_ListItem__ctor

            ///Initialise a new instance of list item with the concatenated string of data we want to display as the first parameter in ListItem instance
            ///The value of list item is the primary key, we convert to string and add as the second parameter to ListItem instance
            ///Each List item will be displayed in the format Nickname, gender, age.
            ListItem newListItem = new ListItem("Superhero ID [" + SuperHeroID.ToString() + "] " + Nickname + ": " + Gender + ", " + Height.ToString() + "m tall" + ", " + Weight_kg + "kg" + ", " + "born " + DisplayDate + ", " + CurrentAge.ToString() + " years old, " + " lives in " + CityName + SuperFlight + SuperInvisibility + SuperSpeed + SuperStrength + SuperTelekenisis + SuperTeleportation + SuperPsychokenisis, SuperHeroID.ToString());

            ///add the item to the list menu
            lstDefaultDetails.Items.Add(newListItem);

            /// increment pointer so it points to next superhero in the collection class
            Pointer++;
        }
        return(recordCount);
    }
    //This function is used to populate the form using a SuperHeroID passed in as a parameter.
    //The parameter is then passed to the SelectSuperHero() function which resides in the clsSuperHero class.
    //The SelectSuperHero() function executes a Search and finds the superhero data and loads the properties
    // in the clsSuperHero class, these properties are then loaded in to the form by DisplaySuperHero
    void DisplaySuperHero(Int32 SuperHeroIDPassedIn)
    {
        clsSuperHero nSuperHero = new clsSuperHero();

        nSuperHero.SelectSuperHero(SuperHeroIDPassedIn);
        //gets the SuperHeroID from the Data table converts to string and assigns to textbox on form
        lblProfileSuperHeroID.Text = Convert.ToString(nSuperHero.SuperHeroID);
        //gets the Nickname from the Data table converts to string and assigns to textbox on form
        tbxProfileName.Text = Convert.ToString(nSuperHero.Nickname);
        //gets the Gender from the Data table converts to string and assigns to Gender string variabl
        string GenderCheck = Convert.ToString(nSuperHero.Gender);

        //gets the Height from the Data table converts to string and assigns to textbox on form
        tbxProfileHeight.Text = Convert.ToString(nSuperHero.Height_m);
        //gets the Weight from the Data table converts to string and assigns to textbox on form
        tbxProfileWeight.Text = Convert.ToString(nSuperHero.Weight_kg);
        //Formats the date (using FormatDate function) and converts it to string then assigns to the BirthDate textbox
        tbxProfileBirthDate.Text = Convert.ToString(nSuperHero.FormatDate(nSuperHero.BirthDate));
        //gets the City from the Data table converts to string and assigns to textbox on form
        ddlProfileCity.SelectedValue = Convert.ToString(nSuperHero.CityNo);
        //calculates age and assigns it to the label for Age
        lblProfileCalculatedAge.Text = Convert.ToString(nSuperHero.CalculateAge(nSuperHero.BirthDate, DateTime.Today));
        //gets the boolean value of speed and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileSpeed.Checked = Convert.ToBoolean(nSuperHero.Speed);
        //gets the boolean value of strength and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileStrength.Checked = Convert.ToBoolean(nSuperHero.Strength);
        //gets the boolean value of flight and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileFlight.Checked = Convert.ToBoolean(nSuperHero.Flight);
        //gets the boolean value of teleportation and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileTeleportation.Checked = Convert.ToBoolean(nSuperHero.Teleportation);
        //gets the boolean value of invisibility and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileInvisibility.Checked = Convert.ToBoolean(nSuperHero.Invisibility);
        //gets the boolean value of telekenisis and assigns it (uses it to check or uncheck the checkbox)
        cbxProfileTelekenisis.Checked = Convert.ToBoolean(nSuperHero.Telekenisis);
        //gets the boolean value of psychokenisis and assigns it (uses it to check or uncheck the checkbox)
        cbxProfilePsychokenesis.Checked = Convert.ToBoolean(nSuperHero.Psychokenisis);
        //Use if statements to check the gender and use selected Index of the drop down list to select it (what will be displayed when page loads)
        //if gender value returned is unassigned then to select this in the ddl selected index value must =0
        if (GenderCheck == "Unassigned")
        {
            ddlProfileGender.SelectedIndex = 0;
        }
        //if gender value returned is Male then to select this in the ddl selected index value must =1
        if (GenderCheck == "Male")
        {
            ddlProfileGender.SelectedIndex = 1;
        }
        //if gender value returned is Female then to select this in the ddl selected index value must =2
        if (GenderCheck == "Female")
        {
            ddlProfileGender.SelectedIndex = 2;
        }
        //if gender value returned is Neutral then to select this in the ddl selected index value must =3
        if (GenderCheck == "Neutral")
        {
            ddlProfileGender.SelectedIndex = 3;
        }
        //if gender value returned is Transgender then to select this in the ddl selected index value must =4
        if (GenderCheck == "Transgender")
        {
            ddlProfileGender.SelectedIndex = 4;
        }
        //if gender value returned is Non-binary then to select this in the ddl selected index value must =5
        if (GenderCheck == "Non-binary")
        {
            ddlProfileGender.SelectedIndex = 5;
        }
    }