//This function is used to populate the City drop down list
    Int32 DisplayCities()
    {
        //create and instance of city collection class
        clsCityCollection ListofCities = new clsCityCollection();

        //[02/02/2019] initially tried a stored procedure tha used an integer primary key parameter in the clsCityCollection
        //class to search city, but this limited me to a single result for each search, so had to use a different type of stored procedure.
        //This other stored procedure was written to select either a number(primary key) or everything. The value was a comparison string
        //Thus the value passed was safe as long as there were no numbers in the cityname field of tblCity table.
        //Pass in the search parameter to select all the in the database
        ListofCities.CitySearchParameter(-1);
        //string variable to store the cities primary key value
        string CityNo;
        //string variable to store the name of the city
        string City;
        //integer variable to store the index of the loop, starts at 0
        Int32 CityPointer = 0;

        //starting from 0, then while the CityPointer is not more than the Count of
        //the number of rows of Cities
        while (CityPointer < ListofCities.Count)
        {
            //get the primary key number value of the city from the database
            CityNo = Convert.ToString(ListofCities.CityList[CityPointer].CityNo);
            //get the city name from the database
            City = Convert.ToString(ListofCities.CityList[CityPointer].CityName);
            //create a list item object with the information we just retrieved
            ListItem CityListItem = new ListItem(City, CityNo);
            //add the CityListItem object row to the list
            ddlProfileCity.Items.Add(CityListItem);
            //increment the CityPointer to point to the next record to get
            CityPointer++;
        }
        //return the count for the number of cities in the data table
        return(ListofCities.Count);
    }
示例#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);
    }