Exemplo n.º 1
0
    protected void gvData_RowDelete(object sender, GridViewDeleteEventArgs e) // Same as the above comment
    {
        ltErr.Text = string.Empty;
        GridViewRow gvRow = gvData.Rows[e.RowIndex];
        int         petId = Convert.ToInt32(((HiddenField)gvRow.FindControl("petId")).Value);

        try
        {
            using (VetSystemEntities db = new VetSystemEntities())
            {
                Owner petOwner = db.Pets // List of pet owners
                                 .AsEnumerable()
                                 .Where(x => x.Id == petId)
                                 .Select(x => x.Owner)
                                 .First();
                db.Owners.Remove(petOwner);

                Pet pet = db.Pets // List of pets
                          .AsEnumerable()
                          .Where(x => x.Id == petId)
                          .First();
                db.Pets.Remove(pet);

                db.SaveChanges();
                gvData.EditIndex = -1;
                ShowDataInGrid();
            }
        }
        catch (Exception ex) { ltErr.Text = "Error: " + ex; }
    }
Exemplo n.º 2
0
 protected void AddRow(object sender, EventArgs e)
 {
     using (VetSystemEntities db = new VetSystemEntities()) // Some of the fields in the database can not be NULL, so put random stuff in them (best comment ever)
     {
         int ownerId = db.Pets
                       .OrderByDescending(x => x.Owner.Id)
                       .First()
                       .Id + 1;
         db.Owners.Add(new Owner {
             Id = ownerId, Name = "Owner name", Phone = "+359899121343", Email = "*****@*****.**", Password = "******"
         });
         db.Pets.Add(new Pet {
             Name = "Pet name", OwnerID = ownerId, AnimalTypeID = 1, AnimalSubTypeID = 1
         });
         db.SaveChanges();
         ShowDataInGrid();
     }
 }
Exemplo n.º 3
0
 // Well I guess there isn't a function starting with O
 public void ShowDataInGrid() // Visualizes parts of the database in grid view in the webpage
 {
     try
     {
         using (VetSystemEntities db = new VetSystemEntities())
         {
             DbSet <Pet>       pets        = db.Pets;
             List <AnimalType> animalTypes = db.AnimalTypes
                                             .GroupBy(x => x.Name)
                                             .Select(grp => grp.FirstOrDefault())
                                             .ToList();
             foreach (var animalType in animalTypes)
             {
                 filterList.Items.Add(animalType.Name);
                 filterList.Items.FindByValue(animalType.Name).Value = animalType.Name;
             }
             gvData.DataSource = pets.ToList();
             gvData.DataBind();
         }
     }
     catch (Exception ex) { ltErr.Text = "Error: " + ex; }
 }
Exemplo n.º 4
0
    protected void gvData_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.SortExpression == (string)ViewState["SortColumn"])
        {
            // We are resorting the same column, so flip the sort direction
            e.SortDirection = ((SortDirection)ViewState["SortColumnDirection"] == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;
        }
        // Apply the sort
        using (VetSystemEntities db = new VetSystemEntities())
        {
            var pets = db.Pets.ToList();
            switch (e.SortExpression)
            {
            case "Name":
                gvData.DataSource = e.SortDirection == SortDirection.Ascending ? pets.OrderBy(x => x.Name) : pets.OrderByDescending(x => x.Name);
                break;

            case "AnimalType.Name":
                gvData.DataSource = e.SortDirection == SortDirection.Ascending ? pets.OrderBy(x => x.AnimalType.Name) : pets.OrderByDescending(x => x.AnimalType.Name);
                break;

            case "AnimalSubType.Name":
                gvData.DataSource = e.SortDirection == SortDirection.Ascending ? pets.OrderBy(x => x.AnimalSubType.Name) : pets.OrderByDescending(x => x.AnimalSubType.Name);
                break;

            case "Owner.Name":
                gvData.DataSource = e.SortDirection == SortDirection.Ascending ? pets.OrderBy(x => x.Owner.Name) : pets.OrderByDescending(x => x.Owner.Name);
                break;

            case "Owner.Email":
                gvData.DataSource = e.SortDirection == SortDirection.Ascending ? pets.OrderBy(x => x.Owner.Email) : pets.OrderByDescending(x => x.Owner.Email);
                break;
            }
            gvData.DataBind();
        }
        ViewState["SortColumn"]          = e.SortExpression;
        ViewState["SortColumnDirection"] = e.SortDirection;
    }
Exemplo n.º 5
0
 protected void FilterGridView(object sender, EventArgs e)
 {
     try
     {
         using (VetSystemEntities db = new VetSystemEntities())
         {
             List <Pet> filteredPets;
             if (filterList.SelectedValue != "Not applied")
             {
                 filteredPets = db.Pets
                                .Where(x => x.AnimalType.Name == filterList.SelectedValue)
                                .Select(x => x)
                                .ToList();
             }
             else
             {
                 filteredPets = db.Pets.ToList();
             }
             gvData.DataSource = filteredPets;
             gvData.DataBind(); // Binds the return of the LINQ to the webpage grid view
         }
     }
     catch (Exception ex) { ltErr.Text = "Error: " + ex; }
 }
Exemplo n.º 6
0
    protected void gvData_RowUpdate(object sender, GridViewUpdateEventArgs e)
    {
        ltErr.Text = string.Empty;
        GridViewRow gvRow = gvData.Rows[e.RowIndex];
        // These are horrible - HELP
        int    petId            = Convert.ToInt32(((HiddenField)gvRow.FindControl("petId")).Value);
        string txtPetName       = ((TextBox)gvRow.Cells[1].Controls[0]).Text;
        string txtAnimalType    = ((TextBox)gvRow.Cells[2].Controls[0]).Text;
        string txtAnimalSubType = ((TextBox)gvRow.Cells[3].Controls[0]).Text;
        string txtOwner         = ((TextBox)gvRow.Cells[4].Controls[0]).Text;
        string txtOwnerEmail    = ((TextBox)gvRow.Cells[5].Controls[0]).Text;

        try
        {
            using (VetSystemEntities db = new VetSystemEntities())
            {
                List <Pet> pets = db.Pets // List of pets read from the db
                                  .AsEnumerable()
                                  .Where(x => x.Id == petId)
                                  .ToList();
                pets.ForEach(x => x.Name = txtPetName != "" && x.Name != txtPetName ? txtPetName : x.Name); // Update pet names

                int animalTypeIndex           = 0;
                List <AnimalType> animalTypes = db.Pets // List of animal types read from the db
                                                .Select(x => x.AnimalType)
                                                .ToList();

                foreach (var animalType in animalTypes)
                {
                    if (animalType.Name.ToLower() == txtAnimalType.ToLower()) // Check if the value exists in the db (case insensitive)
                    {
                        animalTypeIndex = animalType.Id;
                        break;
                    }
                }

                if (animalTypeIndex == 0 && txtAnimalType != "") // If it doesn't ***
                {
                    animalTypeIndex = db.AnimalTypes
                                      .OrderByDescending(x => x.Id)
                                      .First()
                                      .Id + 1;
                    db.AnimalTypes.Add(new AnimalType {
                        Id = animalTypeIndex, Name = txtAnimalType
                    });                                                                                              // *** add it
                }
                pets.ToList().ForEach(x => x.AnimalTypeID = txtAnimalType != "" ? animalTypeIndex : x.AnimalTypeID); // Update pet's animal type id (in Pets table)

                int animalSubTypeIndex = 0;
                List <AnimalSubType> animalSubTypes = db.Pets // List of animal subtypes read from the db
                                                      .Select(x => x.AnimalSubType)
                                                      .ToList();

                foreach (var animalSubType in animalSubTypes)
                {
                    if (animalSubType.Name.ToLower() == txtAnimalSubType.ToLower()) // Check if the value exists in the db (case insensitive)
                    {
                        animalSubTypeIndex = animalSubType.Id;
                        break;
                    }
                }
                if (animalSubTypeIndex == 0 && txtAnimalSubType != "") // If it doesn't ***
                {
                    int parentId = db.AnimalTypes
                                   .Where(x => x.Name.ToLower() == txtAnimalType.ToLower())
                                   .OrderByDescending(x => x.Id)
                                   .Select(x => x.Id)
                                   .First();
                    animalSubTypeIndex = db.AnimalSubTypes
                                         .OrderByDescending(x => x.Id)
                                         .First()
                                         .Id + 1;
                    db.AnimalSubTypes.Add(new AnimalSubType {
                        Id = animalSubTypeIndex, Name = txtAnimalSubType, ParentAnimal = parentId
                    });                                                                                                                     // *** add it
                }
                pets.ToList().ForEach(x => x.AnimalSubTypeID = txtAnimalSubType != "" ? animalSubTypeIndex : x.AnimalSubTypeID);

                List <Owner> petOwners = db.Pets // List of pet types read from the db
                                         .AsEnumerable()
                                         .Where(x => x.Id == petId)
                                         .Select(x => x.Owner)
                                         .ToList();
                petOwners.ToList().ForEach(x => x.Name  = txtOwner != "" && x.Name != txtOwner ? txtOwner : x.Name);
                petOwners.ToList().ForEach(x => x.Email = txtOwnerEmail != "" && x.Email != txtOwnerEmail ? txtOwnerEmail : x.Email);

                db.SaveChanges();
                gvData.EditIndex = -1;
                ShowDataInGrid(); // 'Redraw' the grid view
            }
        }
        catch (Exception ex) { ltErr.Text = "Error: " + ex; }
    }