· 6 years ago · Nov 29, 2019, 04:34 PM
1private void ButtonEdit_Click(object sender, RoutedEventArgs e)
2 {
3 if (ButtonEdit.Content.ToString() == "Edit")
4 {
5 TextBoxName.IsReadOnly = false;
6 TextBoxPosition.IsReadOnly = false;
7 TextBoxPlayStyle.IsReadOnly = false;
8 TextBoxGamesPlayed.IsReadOnly = false;
9 TextBoxGoals.IsReadOnly = false;
10 TextBoxAssists.IsReadOnly = false;
11
12 ButtonEdit.Content = "Save";
13
14 TextBoxName.Background = Brushes.White;
15 TextBoxPosition.Background = Brushes.White;
16 TextBoxPlayStyle.Background = Brushes.White;
17 TextBoxGamesPlayed.Background = Brushes.White;
18 TextBoxGoals.Background = Brushes.White;
19 TextBoxAssists.Background = Brushes.White;
20 }
21 else
22 {
23 using (var db = new MarketDBEntities())
24 {
25 var taskToEdit = db.Players.Find(player.PlayerId);
26 // update description & categoryId
27 taskToEdit.PlayerName = TextBoxName.Text;
28
29 //converting categoryid to integer from text box (string)
30 // tryparse is a safe way to do conversion: null if fails
31 int.TryParse(TextBoxID.Text, out int playerid);
32 taskToEdit.PlayerId = playerid;
33
34 if (player.PlayerId != null)
35 {
36 ComboBoxPlayStyle.SelectedIndex = (int)player.PlayerId - 1;
37 }
38 else
39 {
40 ComboBoxPlayStyle.SelectedItem = null;
41 }
42 // save to database
43 db.SaveChanges();
44
45 ListBoxPlayers.ItemsSource = null; // reset list box
46 players = db.Players.ToList(); // get fresh link
47
48 ListBoxPlayers.ItemsSource = tasks; // re-link
49 }
50 ButtonEdit.Content = "Edit";
51 ButtonEdit.IsEnabled = false;
52
53 TextBoxName.IsReadOnly = true;
54 TextBoxPosition.IsReadOnly = true;
55 TextBoxPlayStyle.IsReadOnly = true;
56 TextBoxGamesPlayed.IsReadOnly = true;
57 TextBoxGoals.IsReadOnly = true;
58 TextBoxAssists.IsReadOnly = true;
59
60 var brush = new BrushConverter();
61
62 TextBoxName.Background = (Brush)brush.ConvertFrom("#E8FBFF");
63 TextBoxPosition.Background = (Brush)brush.ConvertFrom("#E8FBFF");
64 TextBoxPlayStyle.Background = (Brush)brush.ConvertFrom("#E8FBFF");
65 TextBoxGamesPlayed.Background = (Brush)brush.ConvertFrom("#E8FBFF");
66 TextBoxGoals.Background = (Brush)brush.ConvertFrom("#E8FBFF");
67 TextBoxAssists.Background = (Brush)brush.ConvertFrom("#E8FBFF");
68 }
69 }
70
71 private void ButtonAdd_Click(object sender, RoutedEventArgs e)
72 {
73 if (ButtonAdd.Content.ToString() == "Add")
74 {
75 //set boxes to editable
76 ButtonAdd.Content = "Confirm";
77 TextBoxName.Background = Brushes.White;
78 TextBoxPosition.Background = Brushes.White;
79 TextBoxPlayStyle.Background = Brushes.White;
80 TextBoxGamesPlayed.Background = Brushes.White;
81 TextBoxGoals.Background = Brushes.White;
82 TextBoxAssists.Background = Brushes.White;
83
84
85 TextBoxName.IsReadOnly = false;
86 TextBoxPosition.IsReadOnly = false;
87 TextBoxPlayStyle.IsReadOnly = false;
88 TextBoxGamesPlayed.IsReadOnly = false;
89 TextBoxGoals.IsReadOnly = false;
90 TextBoxAssists.IsReadOnly = false;
91
92
93 //clear out boxes
94 TextBoxName.Text = " ";
95 TextBoxPosition.Text = " ";
96 TextBoxPlayStyle.Text = " ";
97 TextBoxGamesPlayed.Text = " ";
98 TextBoxGoals.Text = " ";
99 TextBoxAssists.Text = " ";
100
101 }
102 else
103 {
104 ButtonAdd.Content = "Add";
105 TextBoxName.IsReadOnly = true;
106 TextBoxPosition.IsReadOnly = true;
107 TextBoxPlayStyle.IsReadOnly = true;
108 TextBoxGamesPlayed.IsReadOnly = true;
109 TextBoxGoals.IsReadOnly = true;
110 TextBoxAssists.IsReadOnly = true;
111
112 var brush = new BrushConverter();
113 TextBoxName.Background = (Brush)brush.ConvertFrom("#E8FBFF");
114 TextBoxPosition.Background = (Brush)brush.ConvertFrom("#E8FBFF");
115 TextBoxPlayStyle.Background = (Brush)brush.ConvertFrom("#E8FBFF");
116 TextBoxGamesPlayed.Background = (Brush)brush.ConvertFrom("#E8FBFF");
117 TextBoxGoals.Background = (Brush)brush.ConvertFrom("#E8FBFF");
118 TextBoxAssists.Background = (Brush)brush.ConvertFrom("#E8FBFF");
119
120
121 using (var db = new MarketDBEntities())
122 {
123 Player newPlayer = new Player
124 {
125 PlayerId = Convert.ToInt32(TextBoxID.Text),
126 PlayerName = TextBoxName.Text,
127 Games = Convert.ToInt32(TextBoxGamesPlayed.Text),
128 Goals = Convert.ToInt32(TextBoxGoals.Text.ToString()),
129 Assists = Convert.ToInt32(TextBoxAssists.Text.ToString()),
130
131
132
133
134
135
136 };
137
138 db.Players.Add(newPlayer);
139
140 // save to database
141 db.SaveChanges();
142
143 ListBoxPlayers.ItemsSource = null; // reset list box
144 players = db.Players.ToList(); // get fresh link
145
146 ListBoxPlayers.ItemsSource = players; // re-link
147 }
148 ButtonAdd.Content = "Add";
149 TextBoxName.IsReadOnly = true;
150 TextBoxPosition.IsReadOnly = true;
151 TextBoxPlayStyle.IsReadOnly = true;
152 TextBoxGamesPlayed.IsReadOnly = true;
153 TextBoxGoals.IsReadOnly = true;
154 TextBoxAssists.IsReadOnly = true;
155
156 brush = new BrushConverter();
157 TextBoxName.Background = (Brush)brush.ConvertFrom("#E8FBFF");
158 TextBoxPosition.Background = (Brush)brush.ConvertFrom("#E8FBFF");
159 TextBoxPlayStyle.Background = (Brush)brush.ConvertFrom("#E8FBFF");
160 TextBoxGamesPlayed.Background = (Brush)brush.ConvertFrom("#E8FBFF");
161 TextBoxGoals.Background = (Brush)brush.ConvertFrom("#E8FBFF");
162 TextBoxAssists.Background = (Brush)brush.ConvertFrom("#E8FBFF");
163 }
164
165 }
166
167
168
169//////////////////////// sql
170
171USE Master
172GO
173
174DROP DATABASE IF EXISTS PlayerMarket
175GO
176
177CREATE DATABASE PlayerMarket
178GO
179
180USE PlayerMarket
181GO
182
183
184
185
186DROP TABLE IF EXISTS PlayStyle
187GO
188
189DROP TABLE IF EXISTS Agent
190GO
191
192DROP TABLE IF EXISTS Player
193GO
194
195
196CREATE TABLE Agent(
197AgentId INT IDENTITY NOT NULL PRIMARY KEY,
198AgentName VARCHAR(50),
199Rating INT
200)
201
202CREATE TABLE PlayStyle(
203PlayStyleId INT IDENTITY NOT NULL PRIMARY KEY,
204StyleName VARCHAR(50)NULL,
205Position VARCHAR(50)
206)
207
208
209CREATE TABLE Player(
210
211PlayerId INT IDENTITY NOT NULL PRIMARY KEY,
212PlayerName VARCHAR(50),
213Games INT NULL,
214Goals INT NULL,
215Assists INT NULL,
216CleanSheets INT NULL,
217PlayStyleId INT NULL,
218AgentId INT NULL,
219Age INT NULL,
220Salary INT NULL,
221PlayerPrice INT NULL,
222IsInjured BIT NULL,
223FOREIGN KEY (AgentId) REFERENCES Agent(AgentId),
224FOREIGN KEY (PlayStyleId) REFERENCES PlayStyle(PlayStyleId)
225)
226GO
227
228SELECT * FROM Player
229SELECT * FROM PlayStyle
230SELECT * FROM Agent
231
232--INSERT INTO PlayStyle VALUES('Sweeper','GK'),('Traditional','GK'),
233--('Acrobatic','GK'),('Ball Playing','CB'),('Traditional','CB'),('Wing Back','FB'),('Traditional','FB'),
234--('Def Mid','Mid'),('Box 2 Box','Mid'),('No. 10','Mid'),('Winger','Mid'),('False 9','Forward'),('Striker','Forward')
235
236INSERT INTO Agent VALUES('Gabriel Santini',9),('Marcos Rojas',6),('George Graham',7),('Mirko Cadillac',4),
237('Michael Williams',8),('Jason Micic',10),('Sam Wise',2)
238
239
240INSERT INTO Player VALUES('Henrik Mkhitaryan',35,5,7,0,11,4,30,100000,20,1),('Alex Lacazette',30,10,7,0,13,2,28,90000,60,0),
241('Virgil Van Dijk',38,4,1,20,4,1,28,100000,100,0),('Berndt Leno',38,0,0,6,2,6,28,50000,30,0),
242('Lionel Messi',40,35,20,0,12,3,31,500000,500,0),('Kieran Tierney',8,0,3,2,6,3,22,30000,25,1)