-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppointmentUC.xaml.cs
138 lines (119 loc) · 4.63 KB
/
AppointmentUC.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Model;
using System.Data.Entity.Validation;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace VetClinic
{
/// <summary>
/// Interaction logic for Home.xaml
/// </summary>
public partial class AppointmentUC : UserControl
{
public AppointmentUC()
{
InitializeComponent();
try
{
LvAppointment.ItemsSource = Globals.dbContext.Appointments.ToList();
}
catch (SystemException ex)
{
MessageBox.Show("Error reading from database\n" + ex.Message, "Fatal error",
MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(1);
}
}
private void BtnAppointment_Click(object sender, RoutedEventArgs e)
{
try
{
DateTime when = DpAppointment.SelectedDate.Value.Date.Add(TpAppointments.SelectedTime.Value.TimeOfDay);
string vetIdStr = ComboVet.SelectedValue.ToString();
int vet_id = Convert.ToInt32(vetIdStr);
string ownerIdStr = ComboOwner.SelectedValue.ToString();
int owner_id = Convert.ToInt32(ownerIdStr);
string petIdStr = ComboPet.SelectedValue.ToString();
int pet_id = Convert.ToInt32(petIdStr);
string note = TbxNotes.Text;
Appointment newAppointment = new Appointment(vet_id, owner_id, pet_id, when, note);
Globals.dbContext.Appointments.Add(newAppointment);
try
{
Globals.dbContext.SaveChanges();
}
catch(SystemException ex)
{
MessageBox.Show("Error adding to database\n" + ex.Message, "Database error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
LvAppointment.ItemsSource = Globals.dbContext.Appointments.ToList();
ResetFields();
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, "Input error", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (SystemException ex)
{
MessageBox.Show("Error reading from database\n" + ex.Message, "Database error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
try
{
ComboOwner.ItemsSource = Globals.dbContext.Owners.ToList();
ComboVet.ItemsSource = Globals.dbContext.Vets.ToList();
}
catch (SystemException ex)
{
MessageBox.Show( "Error reading from database for ComboOwner/ComboPet\n" + ex.Message, "Database error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ComboOwner_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try { //ComboVet.SelectedValue = ComboPet.ItemsSource.ToString();
/* var cbo = sender as ComboBox;
var selItem = cbo.SelectedItem as Appointment;
foreach (var item in selItem.owner_id.ToString())
{
ComboPet.Items.Add(item);
}*/
ComboPet.ItemsSource = Globals.dbContext.Pets.ToList();
}
catch (SystemException ex)
{
MessageBox.Show("Error reading from database ComboPets\n" + ex.Message, "Database error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ResetFields()
{
ComboVet.SelectedIndex = -1;
ComboOwner.SelectedIndex = -1;
ComboPet.SelectedIndex = -1;
TbxNotes.Text = "";
DpAppointment.SelectedDate = null;
TpAppointments.SelectedTime = null;
}
}
}