Beispiel #1
0
        private void Btn_habitat_get_Click(object sender, RoutedEventArgs e)
        {
            //If a text box is blank, do not include it in the search.
            try
            {
                bool   hasParams = false;
                int    idVal, humiVal, tempVal;
                string habName;

                //Create the default query that will select all of the habitats
                string defaultQuery = "SELECT * FROM HABITAT;";

                //Create a separate query that can be used for more specific
                //searching.
                string query = "SELECT * FROM HABITAT WHERE ";

                //Only add to the Where statement if the field is filled
                //by something
                if (txt_habitat_id.Text != "")
                {
                    hasParams = true;
                    idVal     = int.Parse(txt_habitat_id.Text);
                    query    += "habitat_id = " + idVal + " and ";
                }
                if (txt_habitat_name.Text != "")
                {
                    hasParams = true;
                    habName   = txt_habitat_name.Text;
                    query    += "habitat_name = " + habName + " and ";
                }
                if (txt_habitat_humidity.Text != "")
                {
                    hasParams = true;
                    humiVal   = int.Parse(txt_habitat_humidity.Text);
                    query    += "humidity = " + humiVal + " and ";
                }
                if (txt_habitat_temperature.Text != "")
                {
                    tempVal = int.Parse(txt_habitat_temperature.Text);
                    query  += "temperature = '" + tempVal + "' and ";
                }

                //Add a semicolon to end the statement
                query += ";";

                //Next, remove the additional add statement along with the
                //extra space before the semicolon.
                query = query.Remove(query.Length - 5, 4);

                //If the user did not enter any search parameters, use the
                //default query. Otherwise, use the query with the where statement.
                if (hasParams)
                {
                    datagrid_habitat.ItemsSource = Database.Query(query)?.DefaultView;
                }
                else
                {
                    datagrid_habitat.ItemsSource = Database.Query(defaultQuery)?.DefaultView;
                }
            }
            catch
            {
                MessageBox.Show("One or more boxes are invalid because they are not numbers");
            }
        }
Beispiel #2
0
        private void Btn_animals_get_Click(object sender, RoutedEventArgs e)
        {
            //If a text box is blank, do not include it in the search.
            try
            {
                bool   hasParams = false;
                int    idVal, habIdVal, speIdVal, wei, dietID;
                string animalName, animalDate;

                //Create the default query which will select every animal
                string defaultQuery = "SELECT * FROM ANIMAL;";

                //Create the query that will select animals with some
                //specified attribute
                string query = "SELECT * FROM ANIMAL WHERE ";

                //Check each field to determine if it should be added
                //to the search attributes. If it is blank, it is left out
                if (txt_animal_id.Text != "")
                {
                    hasParams = true;
                    idVal     = int.Parse(txt_animal_id.Text);
                    query    += "animal_id = " + idVal + " and ";
                }
                if (txt_animal_habitat_id.Text != "")
                {
                    hasParams = true;
                    habIdVal  = int.Parse(txt_animal_habitat_id.Text);
                    query    += "habitat_id = " + habIdVal + " and ";
                }
                if (txt_animal_species_id.Text != "")
                {
                    hasParams = true;
                    speIdVal  = int.Parse(txt_animal_species_id.Text);
                    query    += "species_id = " + speIdVal + " and ";
                }
                if (txt_animal_name.Text != "")
                {
                    hasParams  = true;
                    animalName = txt_animal_name.Text;
                    query     += "animal_name = '" + animalName + "' and ";
                }

                if (date_animal_birthday.Text != "")
                {
                    hasParams  = true;
                    animalDate = date_animal_birthday.Text;
                    query     += "birthday = '" + animalDate + "' and ";
                }
                if (txt_animal_weight.Text != "")
                {
                    hasParams = true;
                    wei       = int.Parse(txt_animal_weight.Text);
                    query    += "weight = " + wei + " and ";
                }
                if (txt_animal_diet_id.Text != "")
                {
                    hasParams = true;
                    dietID    = int.Parse(txt_animal_diet_id.Text);
                    query    += "diet_id = " + dietID + " and ";
                }

                //add a semicolon to end the specific search query
                query += ";";

                //Take out the last and with the additional space from
                //that query.
                query = query.Remove(query.Length - 5, 4);


                //If the user did not enter any data, use the default query
                //else use the query that we have been modifying.
                if (hasParams)
                {
                    datagrid_animals.ItemsSource = Database.Query(query)?.DefaultView;
                }
                else
                {
                    datagrid_animals.ItemsSource = Database.Query(defaultQuery)?.DefaultView;
                }
            }
            catch
            {
                MessageBox.Show("One or more boxes are invalid because they are not numbers");
            }
        }
        private void Btn_dpt_remove_Click(object sender, RoutedEventArgs e)
        {
            string query;

            if (dpt_field_name.Text.Equals(""))
            {
                MessageBox.Show("Please specfiy the department by name to remove.");
            }
            else
            {
                string get_dep_id = Get_Department(dpt_field_name.Text);
                if (get_dep_id.Equals("-1"))
                {
                    MessageBox.Show("Department with name: " + dpt_field_name.Text);
                }
                else
                {
                    query = "select * from employee where department_id=@Dept;";
                    if (Database.Query(query, ("@Dept", get_dep_id)).Rows.Count > 0)
                    {
                        MessageBox.Show("Cannot delete department: " + dpt_field_name.Text + ", because " + Database.Query(query).Rows.Count + " user(s) are still in that department.");
                    }