1 00:00:00,480 --> 00:00:01,260 ‫Welcome back. 2 00:00:01,290 --> 00:00:06,630 ‫In this video, we are going to add the update zoo and update animal button. 3 00:00:06,660 --> 00:00:13,470 ‫So first of all, I want this text to be updated with whatever zoo, for example, I have chosen or 4 00:00:13,470 --> 00:00:16,410 ‫with whatever animal I have chosen. 5 00:00:16,470 --> 00:00:21,090 ‫So let's create that functionality and go into our code behind. 6 00:00:21,540 --> 00:00:26,490 ‫So I will create a new method and well, I'm just going to do it in here. 7 00:00:26,880 --> 00:00:29,820 ‫Private void. 8 00:00:31,940 --> 00:00:37,220 ‫Show selected zoo in text box. 9 00:00:37,790 --> 00:00:39,860 ‫Pretty long name, but that's exactly what it does. 10 00:00:39,860 --> 00:00:43,190 ‫So it's going to show us the select zoo in the text box. 11 00:00:43,190 --> 00:00:53,120 ‫And in order to well run the correct code, I'm actually going to use something very similar to show 12 00:00:53,120 --> 00:00:56,210 ‫associated animals or show zoos. 13 00:00:56,240 --> 00:01:05,180 ‫Actually, let's copy the show Associated Animals entries that we have here and I'm going to plug it 14 00:01:05,180 --> 00:01:05,660 ‫in. 15 00:01:05,660 --> 00:01:08,300 ‫So the query is, of course, a lot shorter. 16 00:01:08,720 --> 00:01:11,990 ‫Instead of showing everything, I want to see the location. 17 00:01:11,990 --> 00:01:22,580 ‫So select location from zoo where the ID is equal to at zoo ID. 18 00:01:23,480 --> 00:01:34,970 ‫And now of course this one is here at the value is zoo ID and now this one is the list zoo selected 19 00:01:34,970 --> 00:01:35,860 ‫value. 20 00:01:35,870 --> 00:01:38,540 ‫So all of that code is pretty much the same. 21 00:01:38,570 --> 00:01:40,910 ‫Now, there is one difference, though. 22 00:01:41,660 --> 00:01:49,160 ‫We don't want to update our list associated values and stuff like that, but what we want to do is to 23 00:01:49,160 --> 00:01:50,720 ‫update our text box. 24 00:01:50,810 --> 00:02:01,880 ‫So I need to access my text box, dot text and update it with zoo data table. 25 00:02:01,880 --> 00:02:03,290 ‫And now that's interesting. 26 00:02:03,710 --> 00:02:06,500 ‫Actually, this is not an animal data table. 27 00:02:06,500 --> 00:02:10,550 ‫I'm going to call that zoo data table. 28 00:02:12,740 --> 00:02:15,140 ‫And now I can access that zoo data table. 29 00:02:16,520 --> 00:02:20,900 ‫And this data table is pretty much a c sharp object. 30 00:02:20,900 --> 00:02:21,410 ‫Right. 31 00:02:21,410 --> 00:02:25,880 ‫And it contains something well, it contains multiple rows. 32 00:02:25,880 --> 00:02:27,860 ‫So there's a property called rows. 33 00:02:28,070 --> 00:02:40,670 ‫And I want to have the first row that there is, which is at the data column called Location and its 34 00:02:40,670 --> 00:02:42,470 ‫Capital L location. 35 00:02:43,190 --> 00:02:47,000 ‫And now that should be converted to a string. 36 00:02:47,270 --> 00:02:53,480 ‫So zoo data table, as I said, is pretty much just the table that we are looking at. 37 00:02:53,480 --> 00:03:02,300 ‫And I want to have the very first entry at the location because we have this selected value, which 38 00:03:02,300 --> 00:03:09,770 ‫is one particular value, and it creates a new data table based on our command. 39 00:03:09,770 --> 00:03:10,280 ‫Right? 40 00:03:10,280 --> 00:03:17,300 ‫So what it does is it searches for this particular entry, which we say here. 41 00:03:17,300 --> 00:03:20,960 ‫Third, where ID is zoo ID and this is unique, right? 42 00:03:20,960 --> 00:03:22,640 ‫The ID is unique as we know. 43 00:03:22,640 --> 00:03:27,080 ‫So this is going to get one entry only. 44 00:03:27,080 --> 00:03:28,880 ‫That's why I can use zero here. 45 00:03:28,880 --> 00:03:37,250 ‫So I can say, give me the very first entry, the very first row in your table and there, give me the 46 00:03:37,250 --> 00:03:38,360 ‫location, please. 47 00:03:38,360 --> 00:03:43,370 ‫And I want that to be translated into a string or convert it into a string. 48 00:03:43,370 --> 00:03:48,470 ‫So that's why I can simply use this approach here and I can run that. 49 00:03:48,680 --> 00:03:54,860 ‫And what that will allow me to do is, well, update my text box based on clicks. 50 00:03:55,970 --> 00:03:58,510 ‫And actually show selected Zune text box. 51 00:03:58,520 --> 00:04:03,140 ‫Well, of course, we're not calling the method yet, so it's not going to be called. 52 00:04:03,140 --> 00:04:06,110 ‫So when I click here, as you can see, nothing happens. 53 00:04:06,260 --> 00:04:16,250 ‫And that has to do with us not having implemented our zoo or adjusted our zoo change method. 54 00:04:16,250 --> 00:04:16,700 ‫What is it? 55 00:04:16,700 --> 00:04:19,370 ‫List zoo selection changed exactly that one. 56 00:04:19,370 --> 00:04:22,880 ‫So in here I now want to call this method that I've just created. 57 00:04:22,880 --> 00:04:23,390 ‫Right. 58 00:04:23,480 --> 00:04:28,820 ‫And it's called show or or what's the name exactly. 59 00:04:29,690 --> 00:04:31,730 ‫So select zoo and TextBox. 60 00:04:31,730 --> 00:04:33,530 ‫So that's the method that I want to call in. 61 00:04:33,530 --> 00:04:37,190 ‫My selection changed method from list zoos. 62 00:04:37,190 --> 00:04:41,930 ‫So in here I'm going to call this method show selected zoo in text box. 63 00:04:41,930 --> 00:04:43,220 ‫Now let's run that. 64 00:04:44,150 --> 00:04:44,870 ‫There we are. 65 00:04:44,870 --> 00:04:51,110 ‫So I'm going to click on Cairo and Berlin and you can see our text is updated all the time. 66 00:04:51,110 --> 00:04:51,830 ‫That's perfect. 67 00:04:51,830 --> 00:04:56,360 ‫That's exactly what I wanted, because now that I have that, I can go ahead and implement the update 68 00:04:56,360 --> 00:04:57,650 ‫zoo functionality. 69 00:04:57,740 --> 00:05:00,470 ‫But before we do that, a little challenge for you. 70 00:05:00,470 --> 00:05:04,580 ‫Please implement the same functionality for our list here at the right. 71 00:05:04,580 --> 00:05:11,240 ‫So for the animals list, because when somebody presses on Monkey, I want this text to be updated as 72 00:05:11,240 --> 00:05:11,660 ‫well. 73 00:05:11,660 --> 00:05:13,310 ‫So please go ahead and try that. 74 00:05:15,890 --> 00:05:16,220 ‫All right. 75 00:05:16,220 --> 00:05:17,380 ‫I hope you did. 76 00:05:17,390 --> 00:05:19,820 ‫So I'm going to make it simple. 77 00:05:19,820 --> 00:05:23,030 ‫I'm going to just copy this method that I've just created. 78 00:05:23,030 --> 00:05:34,220 ‫So show selected zoo in text box, and I'm going to stop the application and I'm going to call that 79 00:05:34,220 --> 00:05:39,320 ‫show selected animal in text box and now select. 80 00:05:39,320 --> 00:05:45,920 ‫And now it's not the location, it's the name from zoo and it's not Zoo, it's from animal where the 81 00:05:45,920 --> 00:05:48,800 ‫ID is animal ID. 82 00:05:49,610 --> 00:05:52,850 ‫And of course, I need to adjust that entry here. 83 00:05:52,850 --> 00:05:59,270 ‫And it's not the list zoos, but it's the list animals all list all animals. 84 00:05:59,270 --> 00:06:04,400 ‫And now instead of the location, I want to have the name of the animal. 85 00:06:05,410 --> 00:06:07,870 ‫And everything else should be the same. 86 00:06:07,900 --> 00:06:08,410 ‫All right. 87 00:06:08,410 --> 00:06:10,010 ‫So that's pretty much it. 88 00:06:10,030 --> 00:06:13,300 ‫So now we can execute that code. 89 00:06:14,250 --> 00:06:19,290 ‫Actually we cannot because we have not implemented the functionality to be run. 90 00:06:19,290 --> 00:06:22,220 ‫But as you can see, this one is not implemented. 91 00:06:22,230 --> 00:06:27,630 ‫So now let's implement this functionality to our list here. 92 00:06:27,630 --> 00:06:29,340 ‫So I'm going to double click on the list. 93 00:06:29,340 --> 00:06:32,340 ‫And as you can see, it adds this list. 94 00:06:32,340 --> 00:06:34,920 ‫All animals selection changed. 95 00:06:35,580 --> 00:06:42,270 ‫And in here, I'm just going to call show selected animal in text box. 96 00:06:42,570 --> 00:06:44,340 ‫All right, now let's try it again. 97 00:06:46,200 --> 00:06:47,290 ‫And there we are. 98 00:06:47,310 --> 00:06:53,820 ‫As you can see, our tiger comes up, our clownfish comes up, all of that and works great. 99 00:06:54,210 --> 00:07:00,330 ‫Now, there's one thing missing and that's to update our owl, our zoo, our parrot. 100 00:07:00,350 --> 00:07:07,350 ‫So let's say we have made a typo and we would like to change the name or well, the city name has changed 101 00:07:07,350 --> 00:07:14,160 ‫for some reason because the mayor decided now it's not going to be New York, it's going to be New York 102 00:07:14,430 --> 00:07:15,720 ‫or something like that. 103 00:07:16,170 --> 00:07:17,610 ‫So yeah, let's do that. 104 00:07:17,610 --> 00:07:19,800 ‫Let's implement that functionality. 105 00:07:19,800 --> 00:07:25,320 ‫And in order to do so, we need to run a query again. 106 00:07:25,320 --> 00:07:27,660 ‫So let's create a new method for that. 107 00:07:28,800 --> 00:07:32,130 ‫So let's start with the update zoo click method. 108 00:07:32,130 --> 00:07:35,760 ‫So actually I'm going to. 109 00:07:37,020 --> 00:07:40,530 ‫Copy this ad animal to zoo method. 110 00:07:41,390 --> 00:07:51,860 ‫Because it's very similar copy and it's going to be not add animal but update zoo click. 111 00:07:54,140 --> 00:08:02,450 ‫So once this update zoo click method is called I want to run a different query than we have here I want 112 00:08:02,450 --> 00:08:17,300 ‫to update zoo and set its location to is equal to add location where the ID is. 113 00:08:17,660 --> 00:08:23,420 ‫So where ID is at zoo ID. 114 00:08:24,770 --> 00:08:25,370 ‫All right. 115 00:08:25,370 --> 00:08:26,690 ‫So that's pretty much what I want to do. 116 00:08:26,690 --> 00:08:28,340 ‫I just want to change the location. 117 00:08:28,340 --> 00:08:31,340 ‫And the location is the name of the zoo, right? 118 00:08:31,340 --> 00:08:32,630 ‫So that's that one here. 119 00:08:32,630 --> 00:08:33,140 ‫Zoo. 120 00:08:33,500 --> 00:08:41,510 ‫So update the zoo table and set the location to the location that we have where the ID is the zoo ID 121 00:08:41,540 --> 00:08:47,060 ‫and now we have zoo ID already, which is the last zoos and now the location. 122 00:08:47,300 --> 00:08:58,430 ‫Location should be our and now it's interesting here our text box which is my text box actually my text 123 00:08:58,430 --> 00:09:01,010 ‫box dot text. 124 00:09:03,990 --> 00:09:04,620 ‫All right. 125 00:09:04,740 --> 00:09:07,640 ‫And, well, that should be it. 126 00:09:07,650 --> 00:09:14,070 ‫Instead of showing associated animals, I want to update the zoos. 127 00:09:14,070 --> 00:09:20,100 ‫So show zoos should be called and everything else should be the same. 128 00:09:20,100 --> 00:09:24,690 ‫Let's test that before we update the animal kind of button as well. 129 00:09:24,690 --> 00:09:25,680 ‫Or the animal button. 130 00:09:25,680 --> 00:09:26,970 ‫Update animal button. 131 00:09:27,180 --> 00:09:29,310 ‫So let's press on Cairo. 132 00:09:29,640 --> 00:09:31,610 ‫Let's change that to Cairo. 133 00:09:32,040 --> 00:09:34,650 ‫Just add an O and it's update. 134 00:09:34,680 --> 00:09:37,530 ‫Nothing happens because the update button is not implemented. 135 00:09:38,070 --> 00:09:42,150 ‫So let's go to Main Window example update button. 136 00:09:42,150 --> 00:09:50,400 ‫And here let's add this click event which is going to be update zoo click and now we can run it again. 137 00:09:50,400 --> 00:09:54,530 ‫And our update button should do what it was supposed to do. 138 00:09:56,630 --> 00:10:00,050 ‫All right, so now let's call this one. 139 00:10:00,050 --> 00:10:05,000 ‫Well, actually, let's select Cairo and let's change it to Cairo and update. 140 00:10:05,000 --> 00:10:11,510 ‫And as you can see, our list here has updated Cairo or Berlin is now called Berlin. 141 00:10:12,740 --> 00:10:15,530 ‫We changed the name to a female name, Berlin. 142 00:10:16,580 --> 00:10:21,050 ‫And we are or New York, as I said, let's call it new org. 143 00:10:22,280 --> 00:10:23,300 ‫It's a new org. 144 00:10:23,330 --> 00:10:26,150 ‫A City of Works update. 145 00:10:26,150 --> 00:10:26,750 ‫There we are. 146 00:10:26,750 --> 00:10:29,630 ‫New York has sharks and clownfish and tigers. 147 00:10:30,350 --> 00:10:31,370 ‫That's pretty awesome. 148 00:10:32,120 --> 00:10:33,950 ‫A little bit like World of Warcraft here. 149 00:10:33,950 --> 00:10:35,900 ‫So now for you, a little challenge. 150 00:10:35,900 --> 00:10:41,900 ‫Please implement this update animal button, which updates the animal on the list on the right hand 151 00:10:41,900 --> 00:10:45,890 ‫side based on what is entered into the text box here. 152 00:10:48,420 --> 00:10:48,990 ‫All right. 153 00:10:48,990 --> 00:10:50,380 ‫I hope you have tried it. 154 00:10:50,400 --> 00:10:53,240 ‫I hope you passed the video and tried it by yourself. 155 00:10:53,250 --> 00:11:00,480 ‫So I'm just going to copy this update zu click and I'm going to call it Update Animal Click. 156 00:11:01,950 --> 00:11:06,300 ‫Now, instead of updating the zoo table, I want to update the animal table. 157 00:11:06,300 --> 00:11:08,550 ‫And there I don't want to change the location. 158 00:11:08,550 --> 00:11:10,080 ‫I want to change the name. 159 00:11:10,080 --> 00:11:15,630 ‫To name where animal ID? 160 00:11:16,020 --> 00:11:19,290 ‫So where the ID is the animal ID that we have? 161 00:11:19,830 --> 00:11:20,430 ‫Well. 162 00:11:21,680 --> 00:11:22,430 ‫At the bottom here. 163 00:11:22,430 --> 00:11:31,040 ‫So the animal ID is going to be the animal list or what is it called list? 164 00:11:31,040 --> 00:11:31,960 ‫All animals. 165 00:11:31,970 --> 00:11:32,930 ‫That's the one. 166 00:11:33,140 --> 00:11:36,320 ‫And text box should have the name. 167 00:11:36,320 --> 00:11:37,880 ‫So this one will be the name. 168 00:11:38,710 --> 00:11:40,240 ‫And everything else is the same. 169 00:11:40,570 --> 00:11:43,030 ‫So now let's run it again. 170 00:11:43,420 --> 00:11:49,510 ‫Well, actually, before we run it, let's go to the XAML file and implement that method because or 171 00:11:49,510 --> 00:11:51,370 ‫add that method to the button click. 172 00:11:51,370 --> 00:11:54,390 ‫So Update Animal is going to have a new click here. 173 00:11:54,400 --> 00:11:59,290 ‫Click is equal to update animal click. 174 00:12:00,700 --> 00:12:01,360 ‫Okay. 175 00:12:01,660 --> 00:12:04,720 ‫Now we can test it and we should be done. 176 00:12:04,720 --> 00:12:07,450 ‫By the way, I hope you manage to do that yourself. 177 00:12:07,870 --> 00:12:08,810 ‫So there we are. 178 00:12:08,830 --> 00:12:18,700 ‫Let's select a monkey and let's call it monkey or monkey update animal. 179 00:12:18,880 --> 00:12:22,570 ‫And most likely it is updated, but we don't see it. 180 00:12:23,200 --> 00:12:25,400 ‫And you might know why that is. 181 00:12:25,420 --> 00:12:30,340 ‫That's because we show the zoo's here and we don't show all animals. 182 00:12:31,150 --> 00:12:37,510 ‫Now, if we do that and we start again, we should see monkeys there already because internally it has 183 00:12:37,510 --> 00:12:37,960 ‫changed. 184 00:12:37,960 --> 00:12:42,700 ‫So the database has been updated, but we haven't seen it so that we are monkey. 185 00:12:43,250 --> 00:12:43,910 ‫Monkey. 186 00:12:44,590 --> 00:12:49,960 ‫And now let's change monkey back to monkey with well, just one. 187 00:12:49,960 --> 00:12:53,050 ‫Why update animal bam monkey is updated. 188 00:12:53,050 --> 00:12:59,320 ‫So let's change the clownfish to a simple clown bird. 189 00:13:00,280 --> 00:13:01,480 ‫Let's update the animal. 190 00:13:01,900 --> 00:13:02,890 ‫We have a cloud bird. 191 00:13:02,890 --> 00:13:06,410 ‫And now, by the way, you see new org has a clown bird. 192 00:13:06,430 --> 00:13:08,200 ‫Now, isn't that awesome? 193 00:13:08,530 --> 00:13:11,230 ‫Because New York has clown birds. 194 00:13:11,710 --> 00:13:13,330 ‫That's a place where I want to be. 195 00:13:13,780 --> 00:13:14,260 ‫All right. 196 00:13:14,260 --> 00:13:17,380 ‫So, yeah, I'd say we're pretty much done. 197 00:13:17,410 --> 00:13:23,650 ‫Of course, there are some cases which could still be added and the functionality could be extended 198 00:13:23,650 --> 00:13:24,610 ‫and so forth. 199 00:13:24,610 --> 00:13:31,150 ‫But it's about understanding what those queries do, about understanding how to run a command, how 200 00:13:31,150 --> 00:13:35,110 ‫to adjust that, how to implement that into our WPF. 201 00:13:35,110 --> 00:13:41,770 ‫And as you can see, we have taken stuff that we know from our WPF tutorial. 202 00:13:41,770 --> 00:13:48,430 ‫We have implemented additional functionality that we have not learned so far, which is to use databases. 203 00:13:48,430 --> 00:13:56,950 ‫So you have seen how to use tables, how to well adjust your entries via code, with buttons and with 204 00:13:56,950 --> 00:13:58,660 ‫text fields and so forth. 205 00:13:58,660 --> 00:14:02,440 ‫And well, in general you have built a whole application here. 206 00:14:02,440 --> 00:14:10,000 ‫So now you could apply that knowledge to build a whole database application for your employees or for 207 00:14:10,000 --> 00:14:14,380 ‫whatever you might come up with, or if you have a job to do. 208 00:14:14,380 --> 00:14:20,140 ‫And somebody says, Yeah, I need this database where I can enter A, B, C, D, and then, you know, 209 00:14:20,140 --> 00:14:21,400 ‫All right, yeah, I can do that. 210 00:14:21,400 --> 00:14:24,640 ‫And you simply go back to this tutorial and you can check out. 211 00:14:24,940 --> 00:14:27,370 ‫This was the method for updating. 212 00:14:27,370 --> 00:14:30,760 ‫This was the method for well deleting and so forth. 213 00:14:31,360 --> 00:14:31,990 ‫All right. 214 00:14:32,170 --> 00:14:38,290 ‫So if you manage to do the exercises in this chapter, great job. 215 00:14:38,290 --> 00:14:39,880 ‫If you didn't, no worries. 216 00:14:39,880 --> 00:14:46,480 ‫You can simply go back and try to do it yourself once you have finished this chapter, which is already 217 00:14:46,480 --> 00:14:46,960 ‫now. 218 00:14:46,960 --> 00:14:50,200 ‫So you can go ahead and try to build that on your own. 219 00:14:50,200 --> 00:14:55,390 ‫Now, of course you can always go a little bit, but it's best if you try to build it yourself. 220 00:14:55,390 --> 00:15:01,450 ‫And by the way, if you don't come up with this SQL Command stuff and so forth, you can always Google 221 00:15:01,450 --> 00:15:01,600 ‫it. 222 00:15:01,600 --> 00:15:03,530 ‫It's always good to yeah. 223 00:15:03,550 --> 00:15:07,870 ‫Research a little bit to read about it and to understand a little better what's going on. 224 00:15:09,270 --> 00:15:09,950 ‫Okey dokey. 225 00:15:09,960 --> 00:15:11,880 ‫So I hope you enjoyed this chapter. 226 00:15:11,880 --> 00:15:17,940 ‫We have created a pretty cool little application and in the next chapter we are going to check out link 227 00:15:17,940 --> 00:15:26,310 ‫which well will improve our life a lot because all of this stuff is well, it's old school, it's a 228 00:15:26,310 --> 00:15:34,650 ‫little old school link increases the efficiency by a bit so we can actually work with real sharp queries 229 00:15:34,650 --> 00:15:38,280 ‫instead of using these MySQL queries which can always go wrong. 230 00:15:38,280 --> 00:15:46,830 ‫So if I have data here instead of updates or updates, then I get errors and well, it's not so easy 231 00:15:46,830 --> 00:15:49,500 ‫to figure it out sometimes. 232 00:15:49,710 --> 00:15:50,250 ‫Great. 233 00:15:50,250 --> 00:15:53,940 ‫So I see you in the next chapter, so see you there.