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; } }
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(); } }
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; } }