private void delete_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Do you want to delete account?", "Warning!", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { if (_table.Equals("Clinic")) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Clinic clinic = dataBase.Clinics.FirstOrDefault(c => c.Name.Equals(_username) && c.City.Equals(_password)); dataBase.Clinics.Remove(clinic); dataBase.SaveChanges(); } } else if (_table.Equals("Owner")) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Owner owner = dataBase.Owners.FirstOrDefault(c => c.Name.Equals(_username) && c.SurName.Equals(_password)); dataBase.Owners.Remove(owner); dataBase.SaveChanges(); } } DialogResult = DialogResult.OK; Close(); } }
private void SeeVetAnimals() { int selected = animalComboBox.SelectedIndex; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = (from pet in dataBase.Pets join owner in dataBase.Owners on pet.Owner equals owner.PCode join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode where vet.PCode == _pCode select new { PName = pet.Name, PType = pet.Type, Clinic = vet.Clinic }).Distinct(); foreach (var pet in pets) { animalComboBox.Items.Add(pet.PName + ", " + pet.PType.ToLower()); _clinic = pet.Clinic; } } }
private void choosePet_SelectedIndexChanged(object sender, EventArgs e) { _petId = _petsId[choosePet.SelectedIndex]; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pet = (from p in dataBase.Pets join t in dataBase.Treatments on p.Id equals t.Animal where t.Vet == _pCode && _petId == t.Animal select new { Name = p.Name, Type = p.Type, Species = p.Species, Weight = p.Weight, Color = p.Color, Start = t.Start, Id = p.Id }).FirstOrDefault(); name.Text = pet.Name; type.Text = pet.Type; species.Text = pet.Species; weight.Text = pet.Weight.ToString(); color.Text = pet.Color; birth.Text = pet.Start.ToShortDateString(); endDate.MinDate = pet.Start; } }
private void ok_Click(object sender, EventArgs e) { fName.ForeColor = System.Drawing.Color.Black; sName.ForeColor = System.Drawing.Color.Black; pCode.ForeColor = System.Drawing.Color.Black; phone.ForeColor = System.Drawing.Color.Black; if (string.IsNullOrEmpty(fName.Text) || string.IsNullOrEmpty(sName.Text) || string.IsNullOrEmpty(pCode.Text)) { MessageBox.Show("Some required fields are empty.", "Error", MessageBoxButtons.OK); } else { if (CheckValidation()) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { dataBase.Vets.Add(new Vet { Clinic = _username, Name = fName.Text, SurName = sName.Text, PCode = pCode.Text, Phone = _phone, StartedAt = _date }); dataBase.SaveChanges(); } Close(); } } }
private void chooseVet_SelectedIndexChanged(object sender, EventArgs e) { int selected = chooseVet.SelectedIndex; if (selected >= 0) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vets = dataBase.Vets.Where(v => v.Clinic.Equals(_clinic)). Select(v => new { Name = v.Name, SurName = v.SurName, PCode = v.PCode, Phone = v.Phone, Started = v.StartedAt }). OrderBy(v => v.SurName).Skip(selected).Take(1); foreach (var vet in vets) { var pets = dataBase.Treatments.Where(t => t.Vet.Equals(vet.PCode) && (t.End >= DateTime.Today)).Count(); fNameText.Text = vet.Name; sNameText.Text = vet.SurName; phoneText.Text = vet.Phone; started_animalTypeText.Text = vet.Started.ToString(); pCodeText.Text = vet.PCode; _pCode = vet.PCode; count.Text = pets.ToString(); } } } }
public ProfileWindow(string name, string pass, string table) { InitializeComponent(); _username = name; _password = pass; _table = table; ClearLabels(); if (table.Equals("Clinic")) { SeeClinicLabels(); ShowClinicData(); } else if (table.Equals("Owner")) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Owner owner = dataBase.Owners.Where(c => c.Name.Equals(_username) && c.SurName.Equals(_password)).Select(p => p).FirstOrDefault(); _username = owner.PCode; } SeeOwner_VetLabels(); ShowOwnerData(); } else if (table.Equals("Vet")) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Vet vet = dataBase.Vets.Where(c => c.Name.Equals(_username) && c.SurName.Equals(_password)).Select(p => p).FirstOrDefault(); _username = vet.PCode; } SeeOwner_VetLabels(); startedDate.Visible = true; startedLabel.Visible = true; ShowVetData(); } }
private void ok_Click(object sender, EventArgs e) { city_fNameTextBox.ForeColor = System.Drawing.Color.Black; streetTextBox.ForeColor = System.Drawing.Color.Black; noTextBox.ForeColor = System.Drawing.Color.Black; phoneTextBox.ForeColor = System.Drawing.Color.Black; sNameTextBox.ForeColor = System.Drawing.Color.Black; if (_table.Equals("Clinic")) { if (CheckValidation()) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Clinic clinic = dataBase.Clinics.FirstOrDefault(c => c.Name.Equals(_username)); clinic.City = city_fNameTextBox.Text; clinic.Street = streetTextBox.Text; clinic.No = noTextBox.Text; clinic.Phone = phoneTextBox.Text; dataBase.SaveChanges(); } Close(); } } else if (_table.Equals("Owner")) { if (CheckValidation()) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Owner owner = dataBase.Owners.FirstOrDefault(c => c.PCode.Equals(_username)); owner.Name = city_fNameTextBox.Text; owner.SurName = streetTextBox.Text; owner.Phone = phoneTextBox.Text; dataBase.SaveChanges(); } Close(); } } else if (_table.Equals("Vet")) { if (CheckValidation()) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Vet vet = dataBase.Vets.FirstOrDefault(c => c.PCode.Equals(_username)); vet.Name = city_fNameTextBox.Text; vet.SurName = streetTextBox.Text; vet.Phone = phoneTextBox.Text; vet.StartedAt = startedDate.Value.Date; dataBase.SaveChanges(); } Close(); } } }
private void ShowOwnerData() { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Owner owner = dataBase.Owners.Where(c => c.PCode.Equals(_username)).Select(p => p).FirstOrDefault(); clinic_pCode.Text = owner.PCode.ToString(); city_fNameTextBox.Text = owner.Name.ToString(); sNameTextBox.Text = owner.SurName.ToString(); streetTextBox.Text = owner.SurName.ToString(); phoneTextBox.Text = owner.Phone.ToString(); } }
private void ShowVetData() { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Vet vet = dataBase.Vets.Where(c => c.PCode.Equals(_username)).Select(p => p).FirstOrDefault(); clinic_pCode.Text = vet.PCode.ToString(); city_fNameTextBox.Text = vet.Name.ToString(); sNameTextBox.Text = vet.SurName.ToString(); streetTextBox.Text = vet.SurName.ToString(); phoneTextBox.Text = vet.Phone.ToString(); startedDate.Text = vet.StartedAt.ToString(); } }
private void SeeVets() { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vets = dataBase.Vets.Where(v => v.Clinic.Equals(_clinic)).Select(v => new { Name = v.Name, SurName = v.SurName }). OrderBy(v => v.SurName); foreach (var vet in vets) { string user = vet.Name + " " + vet.SurName; chooseVet.Items.Add(user); } } }
private void ShowClinicsInformation() { tableDataGridView.Rows.Clear(); top5.Visible = true; if (!top5.Checked) { label.Text = "All clinics' information:"; } else { label.Text = "Top5 clinics' information:"; } string[] info = new string[] { "Clinic name", "City", "Adress", "Phone", "Vets" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var clinics = (from c in dataBase.Clinics join vet in dataBase.Vets on c.Name equals vet.Clinic join treat in dataBase.Treatments on vet.PCode equals treat.Vet select new { Name = c.Name, Phone = c.Phone, City = c.City, Adress = c.Street + " g. " + c.No, Vets = dataBase.Vets.Where(v => v.Clinic.Equals(c.Name)).Select(v => v).Count(), Pets = dataBase.Treatments.Where(t => t.Vet.Equals(vet.PCode)).Count() }).GroupBy(c => c.Name, (key, c) => c.FirstOrDefault()); if (top5.Checked) { clinics = clinics.OrderBy(c => c.Pets).Take(4); } int i = 0; foreach (var clinic in clinics) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = clinic.Name; tableDataGridView.Rows[i].Cells[1].Value = clinic.City; tableDataGridView.Rows[i].Cells[2].Value = clinic.Adress; tableDataGridView.Rows[i].Cells[3].Value = clinic.Phone; tableDataGridView.Rows[i].Cells[4].Value = clinic.Vets; i++; } } }
private void PaintTableVets() { tableDataGridView.Rows.Clear(); string[] info = new string[] { "Personal code", "First name", "Second name", "Phone", "Experience", "Treating pets" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vetList = dataBase.Vets.ToList(); var vets = vetList.Where(v => v.Clinic.Equals(_clinic)).Select(v => new { PCode = v.PCode, Name = v.Name, SurName = v.SurName, Phone = v.Phone, Experience = v.StartedAt, Pets = dataBase.Treatments.Where(s => s.Vet.Equals(v.PCode)).Count() }); if (top5.Checked) { vets = vets.OrderByDescending(p => p.Pets).Take(5); } int i = 0; foreach (var vet in vets) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = vet.PCode; tableDataGridView.Rows[i].Cells[1].Value = vet.Name; tableDataGridView.Rows[i].Cells[2].Value = vet.SurName; tableDataGridView.Rows[i].Cells[3].Value = vet.Phone; if (string.IsNullOrEmpty(vet.Experience.ToString())) { tableDataGridView.Rows[i].Cells[4].Value = null; } else { tableDataGridView.Rows[i].Cells[4].Value = (DateTime.Today.Year - vet.Experience.Value.Year) + " years"; } tableDataGridView.Rows[i].Cells[5].Value = vet.Pets; i++; } } }
public UpdateDeleteWindow(int id) { InitializeComponent(); _petId = id; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = dataBase.Pets.Where(p => p.Id == _petId).Select(p => new { Name = p.Name }); foreach (var pet in pets) { name.Text = pet.Name + "?"; _petName = pet.Name; } } }
private void Vet_Load(object sender, EventArgs e) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vets = from v in dataBase.Vets where v.Name == _username && v.SurName == _password select new { PCode = v.PCode }; foreach (var vet in vets) { _pCode = vet.PCode; } Trace.WriteLine(_username + " " + _password + " " + _pCode); } }
private void ok_Click(object sender, EventArgs e) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var treat = dataBase.Treatments.Add(new Treatment { Animal = _selectedPet, Vet = _selectedVet, Start = startDate.Value.Date, End = null }); dataBase.SaveChanges(); } Close(); }
public OwnerWindow(string user, string pass) { InitializeComponent(); _username = user; _password = pass; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var owners = dataBase.Owners.Where(o => o.Name.Equals(_username) && o.SurName.Equals(_password)). Select(o => new { pCode = o.PCode }); foreach (var owner in owners) { _pCode = owner.pCode; } } }
private void ShowClinicData() { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var smth = dataBase.Clinics.Where(c => c.Name.Equals(_username)).Select(p => p); foreach (var item in smth) { clinic_pCode.Text = item.Name.ToString(); city_fNameTextBox.Text = item.City.ToString(); streetTextBox.Text = item.Street.ToString(); noTextBox.Text = item.No.ToString(); phoneTextBox.Text = item.Phone.ToString(); } } }
private void editPatient_Click(object sender, EventArgs e) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { int pets = dataBase.Treatments.Where(t => t.Vet.Equals(_pCode)).Count(); if (pets == 0) { MessageBox.Show("Sad, but you do not have any treatments!", "Error", MessageBoxButtons.OK); } else { OwnerNewPetWindow pet = new OwnerNewPetWindow(_pCode, "VetEdit"); pet.Show(); } } }
private void PetUpdateWindow_Load(object sender, EventArgs e) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = dataBase.Pets.Where(p => p.Id == _petId).Select(p => p); foreach (var pet in pets) { name.Text = pet.Name; type.Text = pet.Type; species.Text = pet.Species; weight.Text = pet.Weight.ToString(); color.Text = pet.Color; birth.Text = pet.Born.ToString(); } } }
private void chooseClinic_SelectedIndexChanged(object sender, EventArgs e) { chooseVet.Items.Clear(); _selectedClinic = _clinics[chooseClinic.SelectedIndex]; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vets = dataBase.Vets.Where(v => v.Clinic.Equals(_selectedClinic)). Select(v => new { Name = v.Name, SurName = v.SurName, PCode = v.PCode }); _vets = new string[vets.Count()]; int i = 0; foreach (var vet in vets) { chooseVet.Items.Add(vet.Name + " " + vet.SurName); _vets[i++] = vet.PCode; } } }
private void ShowVetsInformation() { label.Text = $"All clinic:{_pCode} vets' information:"; string[] info = new string[] { "Name", "Surname", "Experience", "Phone", "Treated pets" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var vets = dataBase.Vets.Where(v => v.Clinic.Equals(_pCode)).Select(v => new { Name = v.Name, SurName = v.SurName, Phone = v.Phone, Experience = v.StartedAt, Pets = dataBase.Treatments.Where(s => s.Vet.Equals(v.PCode)).Count() }).OrderBy(v => v.Experience); int i = 0; foreach (var vet in vets) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = vet.Name; tableDataGridView.Rows[i].Cells[1].Value = vet.SurName; if (string.IsNullOrEmpty(vet.Experience.ToString())) { tableDataGridView.Rows[i].Cells[2].Value = null; } else { tableDataGridView.Rows[i].Cells[2].Value = (DateTime.Today.Year - vet.Experience.Value.Year) + " years"; }; tableDataGridView.Rows[i].Cells[3].Value = vet.Phone; tableDataGridView.Rows[i].Cells[4].Value = vet.Pets; i++; } } }
private void PaintTableOwners() { string[] info = new string[] { "Personal code", "First name", "Second name", "Phone", "Pets" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var owners = (from owner in dataBase.Owners.ToList() join pet in dataBase.Pets on owner.PCode equals pet.Owner join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode select new { PCode = owner.PCode, Name = owner.Name, SurName = owner.SurName, Phone = owner.Phone, Pets = dataBase.Pets.Where(p => p.Owner.Equals(owner.PCode)).Count(), Clinic = vet.Clinic }).Distinct(); int i = 0; foreach (var owner in owners) { if (owner.Clinic.Equals(_clinic)) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = owner.PCode; tableDataGridView.Rows[i].Cells[1].Value = owner.Name; tableDataGridView.Rows[i].Cells[2].Value = owner.SurName; tableDataGridView.Rows[i].Cells[3].Value = owner.Phone; tableDataGridView.Rows[i].Cells[4].Value = owner.Pets; i++; } } } }
private void animalComboBox_SelectedIndexChanged(object sender, EventArgs e) { int selected = animalComboBox.SelectedIndex; using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var owners = (from pet in dataBase.Pets join owner in dataBase.Owners on pet.Owner equals owner.PCode join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode select new { FName = owner.Name, SName = owner.SurName, Phone = owner.Phone, PCode = owner.PCode, Pets = dataBase.Pets.Where(p => p.Owner.Equals(owner.PCode)).Count(), PName = pet.Name, PType = pet.Type, Clinic = vet.Clinic }).Distinct(); int i = 0; foreach (var owner in owners) { if (owner.Clinic.Equals(_clinic)) { if (i == selected) { _pCode = owner.PCode; pCodeText.Text = owner.PCode; fNameText.Text = owner.FName; sNameText.Text = owner.SName; phoneText.Text = owner.Phone; count.Text = owner.Pets.ToString(); } i++; } } } }
private void Form1_Load(object sender, EventArgs e) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = dataBase.Pets.Where(p => p.Owner.Equals(_pCode)).Select(p => new { Name = p.Name, Type = p.Type, Id = p.Id }); _petsId = new int[pets.Count()]; int i = 0; foreach (var pet in pets) { choosePet.Items.Add(pet.Name + ", " + pet.Type); _petsId[i++] = pet.Id; } i = 0; var clinics = dataBase.Clinics.Select(c => new { Name = c.Name, City = c.City }); _clinics = new string[clinics.Count()]; foreach (var clinic in clinics) { chooseClinic.Items.Add(clinic.Name + ", " + clinic.City); _clinics[i++] = clinic.Name; } } }
private void ShowPetData() { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { if (_type.Equals("Vet")) { var pets = from p in dataBase.Pets join t in dataBase.Treatments on p.Id equals t.Animal where t.Vet == _pCode && t.End == null select new { Name = p.Name, Type = p.Type, Id = p.Id }; _petsId = new int[pets.Count()]; int i = 0; foreach (var pet in pets) { choosePet.Items.Add(pet.Name + ", " + pet.Type); _petsId[i++] = pet.Id; } } else { var pets = from p in dataBase.Pets join t in dataBase.Treatments on p.Id equals t.Animal where t.Vet == _pCode select new { Name = p.Name, Type = p.Type, Id = p.Id }; _petsId = new int[pets.Count()]; int i = 0; foreach (var pet in pets) { choosePet.Items.Add(pet.Name + ", " + pet.Type); _petsId[i++] = pet.Id; } } } }
private void PaintTablePets() { string[] info = new string[] { "Name", "Owner name", "Type", "Species", "Birth", "Weight", "Color" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = (from pet in dataBase.Pets join owner in dataBase.Owners on pet.Owner equals owner.PCode join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode select new { Clinic = vet.Clinic, Name = pet.Name, Owner = owner.Name + " " + owner.SurName, Type = pet.Type, Species = pet.Species, Birth = pet.Born, Weight = pet.Weight, Color = pet.Color }).Distinct(); int i = 0; foreach (var pet in pets) { if (pet.Clinic.Equals(_clinic)) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = pet.Name; tableDataGridView.Rows[i].Cells[1].Value = pet.Owner; tableDataGridView.Rows[i].Cells[2].Value = pet.Type; tableDataGridView.Rows[i].Cells[3].Value = pet.Species; if (string.IsNullOrEmpty(pet.Birth.ToString())) { tableDataGridView.Rows[i].Cells[4].Value = null; } else { tableDataGridView.Rows[i].Cells[4].Value = pet.Birth.Value.Date.ToShortDateString(); } if (string.IsNullOrEmpty(pet.Weight.ToString())) { tableDataGridView.Rows[i].Cells[5].Value = null; } else { tableDataGridView.Rows[i].Cells[5].Value = pet.Weight + " kg."; } tableDataGridView.Rows[i].Cells[6].Value = pet.Color; i++; } } } }
private void ShowOwner() { string[] info = new string[] { "Name", "Type", "Species", "Birth", "Weight", "Color" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = (from pet in dataBase.Pets join owner in dataBase.Owners on pet.Owner equals owner.PCode join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode where owner.PCode.Equals(_pCode) select new { Clinic = vet.Clinic, Name = pet.Name, Type = pet.Type, Species = pet.Species, Birth = pet.Born, Weight = pet.Weight, Color = pet.Color, }).Distinct(); Trace.WriteLine(pets.ToList().Count); if (pets.ToList().Count == 1) { Close(); MessageBox.Show("This owner do not have more pets.", "Sorry", MessageBoxButtons.OK); } else { var ownerInfo = dataBase.Owners.Where(o => _pCode.Equals(o.PCode)).Select(o => new { Name = o.Name, Surname = o.SurName }); foreach (var owner in ownerInfo) { label.Text = $"{owner.Name} {owner.Surname}'s all pets' info:"; } int i = 0; foreach (var pet in pets) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = pet.Name; tableDataGridView.Rows[i].Cells[1].Value = pet.Type; tableDataGridView.Rows[i].Cells[2].Value = pet.Species; if (string.IsNullOrEmpty(pet.Birth.ToString())) { tableDataGridView.Rows[i].Cells[3].Value = null; } else { tableDataGridView.Rows[i].Cells[3].Value = pet.Birth.Value.Date.ToShortDateString(); } if (string.IsNullOrEmpty(pet.Weight.ToString())) { tableDataGridView.Rows[i].Cells[4].Value = null; } else { tableDataGridView.Rows[i].Cells[4].Value = pet.Weight + " kg."; } tableDataGridView.Rows[i].Cells[5].Value = pet.Color; i++; } } } }
private void ShowVet() { string[] info = new string[] { "Name", "Owner name", "Type", "Species", "Birth", "Weight", "Color", "Start", "End" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = (from pet in dataBase.Pets join owner in dataBase.Owners on pet.Owner equals owner.PCode join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode where treat.Vet.Equals(_pCode) select new { Clinic = vet.Clinic, Name = pet.Name, Owner = owner.Name + " " + owner.SurName, Type = pet.Type, Species = pet.Species, Birth = pet.Born, Weight = pet.Weight, Color = pet.Color, Start = treat.Start, End = treat.End }).OrderBy(p => p.Start); var vetInfo = dataBase.Vets.Where(v => _pCode.Equals(v.PCode)).Select(v => new { Name = v.Name, SurName = v.SurName }); int i = 0; foreach (var vet in vetInfo) { label.Text = $"{vet.Name} {vet.SurName}'s treating pets' info:"; } foreach (var pet in pets) { if (pet.Clinic.Equals(_clinic)) { tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = pet.Name; tableDataGridView.Rows[i].Cells[1].Value = pet.Owner; tableDataGridView.Rows[i].Cells[2].Value = pet.Type; tableDataGridView.Rows[i].Cells[3].Value = pet.Species; if (string.IsNullOrEmpty(pet.Birth.ToString())) { tableDataGridView.Rows[i].Cells[4].Value = null; } else { tableDataGridView.Rows[i].Cells[4].Value = pet.Birth.Value.Date.ToShortDateString(); } if (string.IsNullOrEmpty(pet.Weight.ToString())) { tableDataGridView.Rows[i].Cells[5].Value = null; } else { tableDataGridView.Rows[i].Cells[5].Value = pet.Weight + " kg."; } tableDataGridView.Rows[i].Cells[6].Value = pet.Color; tableDataGridView.Rows[i].Cells[7].Value = pet.Start.Date.ToShortDateString(); if (string.IsNullOrEmpty(pet.End.Value.ToString())) { tableDataGridView.Rows[i].Cells[5].Value = null; } else { tableDataGridView.Rows[i].Cells[8].Value = pet.End.Value.ToShortDateString(); } i++; } } } }
private void ShowPetsInformation() { label.Text = "All pets' information:"; string[] info = new string[] { "Name", "Type", "Specie", "Treatment start", "Treatment end", "Vet", "Clinic" }; tableDataGridView.ColumnCount = info.Length; for (int i = 0; i < info.Length; i++) { tableDataGridView.Columns[i].Name = info[i]; } using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var pets = (from pet in dataBase.Pets join treat in dataBase.Treatments on pet.Id equals treat.Animal join vet in dataBase.Vets on treat.Vet equals vet.PCode where pet.Owner.Equals(_pCode) select new { Clinic = vet.Clinic, Name = pet.Name, Type = pet.Type, Species = pet.Species, Start = treat.Start, End = treat.End, Vet = vet.Name + " " + vet.SurName, Id = pet.Id }).OrderBy(p => p.End).GroupBy(c => c.Id, (key, c) => c.FirstOrDefault()); var pets1 = dataBase.Pets.Where(p => p.Owner.Equals(_pCode)).Select(p => p); _petID = new int[pets.ToList().Count + pets1.ToList().Count]; int i = 0; string[] petsNames = new string[pets.ToList().Count]; foreach (var pet in pets) { petsNames[i] = pet.Name; tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = pet.Name; tableDataGridView.Rows[i].Cells[1].Value = pet.Type; tableDataGridView.Rows[i].Cells[2].Value = pet.Species; tableDataGridView.Rows[i].Cells[3].Value = pet.Start.ToShortDateString(); if (string.IsNullOrEmpty(pet.End.Value.ToString())) { tableDataGridView.Rows[i].Cells[4].Value = null; } else { tableDataGridView.Rows[i].Cells[4].Value = pet.End.Value.ToShortDateString(); } tableDataGridView.Rows[i].Cells[5].Value = pet.Vet; tableDataGridView.Rows[i].Cells[6].Value = pet.Clinic; _petID[i] = pet.Id; i++; } int n = i; foreach (var pet in pets1) { bool found = false; for (int j = 0; j < petsNames.Length; j++) { if (pet.Name.Equals(petsNames[j])) { found = true; break; } } if (!found) { _petID[n] = pet.Id; tableDataGridView.Rows.Add(); tableDataGridView.Rows[i].Cells[0].Value = pet.Name; tableDataGridView.Rows[i].Cells[1].Value = pet.Type; tableDataGridView.Rows[i].Cells[2].Value = pet.Species; i++; n++; } } } }
private void ok_Click(object sender, EventArgs e) { name.ForeColor = System.Drawing.Color.Black; type.ForeColor = System.Drawing.Color.Black; species.ForeColor = System.Drawing.Color.Black; weight.ForeColor = System.Drawing.Color.Black; color.ForeColor = System.Drawing.Color.Black; if (_type.Equals("Vet") || _type.Equals("VetEdit")) { if (survive.Checked) { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { var treats = dataBase.Treatments.Where(t => t.Animal == _petId && _pCode.Equals(t.Vet)).Select(t => t); foreach (var treat in treats) { dataBase.Treatments.Remove(treat); } Pet pet = dataBase.Pets.Where(t => t.Id == _petId).Select(t => t).First(); dataBase.Pets.Remove(pet); dataBase.SaveChanges(); } } else { using (VeterinaryEntities dataBase = new VeterinaryEntities()) { Treatment treat = dataBase.Treatments.Where(t => t.Animal == _petId && t.Vet.Equals(_pCode)).Select(t => t).First(); treat.End = endDate.Value.Date; dataBase.SaveChanges(); } } Close(); } else { if (string.IsNullOrEmpty(name.Text) || string.IsNullOrEmpty(type.Text)) { MessageBox.Show("Some required fields are empty.", "Error", MessageBoxButtons.OK); } else { if (CheckValidation()) { string str_connection = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Dovile\Desktop\Veterinary\VeterinarySystem\VeterinarySystem\DatabaseVeterinary.mdf;Integrated Security=True"; SqlConnection connection = new SqlConnection(); connection.ConnectionString = str_connection; connection.Open(); SqlCommand insert = new SqlCommand("INSERT INTO Pet (Name, Owner, Type, Species, Born, Weight, Color) " + "VALUES (@N, @O, @T, @S, @B, @W, @C)", connection); insert.Parameters.Add(new SqlParameter("@N", name.Text)); insert.Parameters.Add(new SqlParameter("@O", _pCode)); insert.Parameters.Add(new SqlParameter("@T", type.Text)); if (string.IsNullOrEmpty(species.Text)) { insert.Parameters.Add(new SqlParameter("@S", DBNull.Value)); } else { insert.Parameters.Add(new SqlParameter("@S", species.Text)); } if (string.IsNullOrEmpty(birth.Text)) { insert.Parameters.Add(new SqlParameter("@B", DBNull.Value)); } else { insert.Parameters.Add(new SqlParameter("@B", birth.Value.Date)); } if (string.IsNullOrEmpty(weight.Text)) { insert.Parameters.Add(new SqlParameter("@W", DBNull.Value)); } else { insert.Parameters.Add(new SqlParameter("@W", weight.Text)); } if (string.IsNullOrEmpty(color.Text)) { insert.Parameters.Add(new SqlParameter("@C", DBNull.Value)); } else { insert.Parameters.Add(new SqlParameter("@C", color.Text)); } try { insert.ExecuteNonQuery(); } catch (SqlException) { } connection.Close(); Close(); } } } }