1 00:00:00,360 --> 00:00:01,120 ‫Welcome back. 2 00:00:01,140 --> 00:00:06,400 ‫In this video, we are going to use the link in order to update and delete data. 3 00:00:06,420 --> 00:00:08,970 ‫So let's first of all, update data. 4 00:00:09,210 --> 00:00:16,770 ‫So let's create a new method here, public void update and I'm just going to call it update 20. 5 00:00:16,770 --> 00:00:25,980 ‫So I want to update Tony because let's say I want to call him Antonio or Tony's just a well short version 6 00:00:25,980 --> 00:00:26,580 ‫of his name. 7 00:00:26,580 --> 00:00:29,220 ‫So I'm just going to use the whole name. 8 00:00:29,220 --> 00:00:37,140 ‫And first of all, I need to get the Tony object and you can go ahead and just use the student class 9 00:00:37,140 --> 00:00:39,090 ‫here and call them Tony. 10 00:00:39,140 --> 00:00:47,400 ‫That will be data context dot students dot first or default. 11 00:00:47,460 --> 00:00:53,010 ‫What that does, it returns the first element of a sequence or a default value of the sequence contains 12 00:00:53,040 --> 00:00:54,000 ‫no elements. 13 00:00:54,000 --> 00:01:00,900 ‫And that's good because if we try to access something that doesn't exist, then nothing will work or 14 00:01:00,900 --> 00:01:03,210 ‫it won't work and we will get errors and stuff. 15 00:01:03,210 --> 00:01:12,840 ‫So in here I'm just going to say ste equal bigger sign first name is equal to Tony. 16 00:01:13,920 --> 00:01:18,090 ‫So we could of course use equals here as well, but that's another way of doing it, right? 17 00:01:18,090 --> 00:01:26,520 ‫So now we get Tony and what we can do now is simply say, okay, I want to change the name of Tony to 18 00:01:26,550 --> 00:01:29,820 ‫Antonio and Tony like that. 19 00:01:31,340 --> 00:01:35,510 ‫And now I'll need to submit the changes because otherwise nothing will happen. 20 00:01:35,510 --> 00:01:38,930 ‫So data context not submit changes. 21 00:01:39,980 --> 00:01:42,830 ‫And now I can display it on my data grid. 22 00:01:42,830 --> 00:01:48,950 ‫So item source will be data context dot students. 23 00:01:48,950 --> 00:01:53,390 ‫So I want to see all the students and now I will see that Tony is not Tony anymore. 24 00:01:53,390 --> 00:01:54,470 ‫He's Antonio. 25 00:01:54,740 --> 00:01:58,040 ‫And before we run the code, we of course need to call this method. 26 00:01:58,040 --> 00:02:04,100 ‫So let's comment that out here and let's update our good old friend, Tony. 27 00:02:04,910 --> 00:02:07,700 ‫Update Tony. 28 00:02:11,490 --> 00:02:12,870 ‫Now let's run the code. 29 00:02:14,350 --> 00:02:18,540 ‫And we are we have all our students and Carla and then here Antonio. 30 00:02:18,550 --> 00:02:20,890 ‫So we changed the name of Tony. 31 00:02:20,890 --> 00:02:26,830 ‫And that's pretty cool because we can simply get the whole object and adjust the components that we 32 00:02:26,830 --> 00:02:27,100 ‫want. 33 00:02:27,100 --> 00:02:31,780 ‫So adjust all the properties that we want and then we can just submit the changes and that's it. 34 00:02:31,780 --> 00:02:38,410 ‫So it's a lot easier than to say, okay, get the object where the ID is so and so where the user is 35 00:02:38,410 --> 00:02:42,370 ‫so and so, then change or update X and Y, Z. 36 00:02:42,370 --> 00:02:45,970 ‫So SQL is a lot more complex than just doing it like this. 37 00:02:45,970 --> 00:02:48,940 ‫So that's a pretty, very easy and cool way. 38 00:02:48,940 --> 00:02:51,610 ‫So you could try to update other students. 39 00:02:51,610 --> 00:02:57,700 ‫Now you can update lectures, you could update universities and stuff like that in a very easy way. 40 00:02:57,700 --> 00:03:07,030 ‫So just like you've seen here, now let's delete some a user because I'm not happy with this James user, 41 00:03:07,060 --> 00:03:13,510 ‫not because he's not because he's transgender, but because he doesn't have any object or any lectures. 42 00:03:13,510 --> 00:03:15,880 ‫And his name is James instead of James. 43 00:03:15,880 --> 00:03:19,750 ‫So I did a mistake there, so I want to delete that object entirely. 44 00:03:19,750 --> 00:03:23,530 ‫Or maybe he just left the university so it's not part of units anymore. 45 00:03:23,530 --> 00:03:27,850 ‫So I want to use the delete functionality to do that. 46 00:03:27,850 --> 00:03:30,290 ‫So delete Jane. 47 00:03:31,180 --> 00:03:34,420 ‫So I'm going to call it and I need to get Jane first. 48 00:03:34,420 --> 00:03:47,740 ‫So student James is going to be data context thought students that first or default and here it will 49 00:03:47,740 --> 00:03:54,070 ‫be as t start name is equal to Jane. 50 00:03:56,200 --> 00:04:03,880 ‫Now there is a data context dot students method and as you can see, there are many. 51 00:04:03,880 --> 00:04:04,180 ‫Right. 52 00:04:04,180 --> 00:04:08,740 ‫And the one that I want to use is delete on submit. 53 00:04:11,020 --> 00:04:19,300 ‫So what that will do is it will simply delete this Jane object and I need to go ahead and context that 54 00:04:19,300 --> 00:04:20,830 ‫submit the changes. 55 00:04:21,460 --> 00:04:23,500 ‫Submit changes like that. 56 00:04:25,610 --> 00:04:27,560 ‫And now if I run that. 57 00:04:29,550 --> 00:04:31,050 ‫I might run into errors. 58 00:04:31,050 --> 00:04:33,070 ‫So I'm going to show you. 59 00:04:33,090 --> 00:04:35,490 ‫Let's show the students again. 60 00:04:36,540 --> 00:04:40,290 ‫Let's go back to the top where we updated Tony. 61 00:04:40,290 --> 00:04:46,350 ‫Now is the gym, the elite gym. 62 00:04:47,820 --> 00:04:48,780 ‫Let's run it. 63 00:04:50,050 --> 00:04:54,550 ‫And it worked so that we are Carla, Antonio and Lily. 64 00:04:54,670 --> 00:04:58,640 ‫They are still at the university and James is not anymore. 65 00:04:58,660 --> 00:05:01,090 ‫So in this case, in my case, it worked. 66 00:05:01,090 --> 00:05:09,010 ‫But if you run into errors, deleting it here, what you'll need to do is to rebuild the connection. 67 00:05:09,190 --> 00:05:12,100 ‫So I'm going to show you real quick how you can do it. 68 00:05:12,550 --> 00:05:17,830 ‫So if it doesn't work for you, so if you run into an error, then instead of running this line of code 69 00:05:17,830 --> 00:05:19,780 ‫here, you need to add those three lines. 70 00:05:19,780 --> 00:05:26,830 ‫So first of all, you need to set the connection string again with the correct one for you. 71 00:05:26,860 --> 00:05:33,640 ‫So in my case, it's not student manager, but it's in this case it's the link to SQL. 72 00:05:33,640 --> 00:05:38,740 ‫So it's exactly the same link here and I'm going to use it here. 73 00:05:38,740 --> 00:05:43,150 ‫So this is just bug fixing or, well, a workaround. 74 00:05:43,240 --> 00:05:43,630 ‫All right. 75 00:05:43,630 --> 00:05:46,320 ‫So if you run into this error, that's how you can solve it. 76 00:05:46,330 --> 00:05:54,760 ‫Then you create a link as to SQL data, classes data, connect the context called DB, and then you 77 00:05:54,760 --> 00:05:58,660 ‫can set DB students as your item source. 78 00:05:58,900 --> 00:05:59,530 ‫All right. 79 00:05:59,530 --> 00:06:02,380 ‫So that's if you run into this error. 80 00:06:02,380 --> 00:06:06,790 ‫But in my case, luckily it worked well, so I'm just going to keep it as is. 81 00:06:06,790 --> 00:06:08,440 ‫So we have this delete game. 82 00:06:08,920 --> 00:06:09,310 ‫All right. 83 00:06:09,310 --> 00:06:14,680 ‫Now, a little summary of what we've done because we've done quite something and there is even something 84 00:06:14,680 --> 00:06:18,370 ‫that I haven't touched on at all, which is quite important as well. 85 00:06:18,520 --> 00:06:25,270 ‫And you might have noticed that whenever you do something like that, there can be plenty of errors. 86 00:06:25,270 --> 00:06:30,340 ‫So whenever you work with databases and so forth, there can be a lot of errors. 87 00:06:30,340 --> 00:06:36,240 ‫So whenever you submit or stuff like that, things just could go wrong. 88 00:06:36,250 --> 00:06:38,170 ‫So then try and catch. 89 00:06:38,170 --> 00:06:44,390 ‫You try and catch in that case because otherwise, well, you could crash a whole program, right? 90 00:06:44,410 --> 00:06:47,920 ‫And that's not the whole idea about this or of this. 91 00:06:48,010 --> 00:06:53,620 ‫So you try and catch wherever you have a potential error and there are many here. 92 00:06:53,620 --> 00:06:59,410 ‫So please go ahead and try to fix all of the potential errors so that your program will never crash. 93 00:06:59,410 --> 00:07:03,880 ‫So let's say we have an incorrect name or we have. 94 00:07:04,120 --> 00:07:06,640 ‫Yeah, we're trying to access something that doesn't exist. 95 00:07:06,640 --> 00:07:11,770 ‫Or we're trying to put something somewhere where the somewhere where we put it, it doesn't exist. 96 00:07:11,770 --> 00:07:15,670 ‫So those are things that you always have to be very careful about. 97 00:07:16,330 --> 00:07:18,040 ‫And now let's just sum it up. 98 00:07:18,100 --> 00:07:20,200 ‫Let's go through the different methods that we have. 99 00:07:20,200 --> 00:07:25,360 ‫So we have this insert universities, and that's generally where we, first of all, gotten rid of all 100 00:07:25,360 --> 00:07:31,270 ‫the universities because each time we executed that method, we created those two new universities. 101 00:07:31,270 --> 00:07:33,700 ‫So we added them on top each time. 102 00:07:33,700 --> 00:07:37,630 ‫So we delete it all first and then we added them step by step. 103 00:07:37,750 --> 00:07:38,460 ‫All right. 104 00:07:38,470 --> 00:07:43,510 ‫Now this execute command is just to execute some SQL Command. 105 00:07:43,510 --> 00:07:45,580 ‫So this is delete from university. 106 00:07:45,580 --> 00:07:48,520 ‫So it deletes all the content from university. 107 00:07:48,520 --> 00:07:57,940 ‫So from the university table that we have here on the side now we create objects of a specific class 108 00:07:57,940 --> 00:08:02,920 ‫and this class, where does it come from because we haven't created a class entry ourselves? 109 00:08:02,920 --> 00:08:10,870 ‫Well, it comes from this link to SQL data classes, which is amazing because it allows us to just drag 110 00:08:10,870 --> 00:08:14,860 ‫tables in here and then we can use those tables as classes. 111 00:08:14,860 --> 00:08:20,950 ‫And those properties that there are are simply the properties of the class that we can access and adjust. 112 00:08:20,950 --> 00:08:28,030 ‫So we can create objects of that and adjust them accordingly and even store them so we can store them 113 00:08:28,030 --> 00:08:32,380 ‫inside of that specific data context or in that table. 114 00:08:32,380 --> 00:08:40,180 ‫So we have this data context, which is the link to SQL data classes, data context, which again is 115 00:08:40,180 --> 00:08:45,940 ‫connected via our connection string, which is our connection to our database. 116 00:08:45,940 --> 00:08:49,240 ‫So our connection here, data connection that we have. 117 00:08:50,650 --> 00:08:53,620 ‫Next we saw how to submit those changes. 118 00:08:53,620 --> 00:09:00,220 ‫So we add something, then we add that to the or insert on submit and then we submit. 119 00:09:00,220 --> 00:09:06,100 ‫So that's always the approach if you want to add something and if you want to show it on our data grid, 120 00:09:06,100 --> 00:09:08,800 ‫which is a very basic. 121 00:09:10,260 --> 00:09:15,930 ‫Object that we have here in our XAML file, which is just a data grid in Zemo. 122 00:09:16,870 --> 00:09:23,860 ‫Then we can go ahead and set this item source to that data context that we want to see. 123 00:09:23,860 --> 00:09:27,850 ‫So in our case, we want to see all the universities and that's what we get here. 124 00:09:27,850 --> 00:09:29,830 ‫So the same thing goes for students. 125 00:09:29,830 --> 00:09:36,130 ‫And as you can see here, because we added multiple objects at once, we just said, okay, let's create 126 00:09:36,130 --> 00:09:40,990 ‫a list and use insert all and submit instead of insert on submit. 127 00:09:41,380 --> 00:09:49,690 ‫And we created all of those students, Carla, Tony, Lily, Jane, all of them with additional info 128 00:09:49,750 --> 00:09:53,170 ‫here, like gender, like university ID and so forth. 129 00:09:53,170 --> 00:09:55,540 ‫And those are available in students. 130 00:09:55,540 --> 00:09:57,970 ‫So you have name, gender and university ID. 131 00:09:58,270 --> 00:10:03,400 ‫So we just used those and we added values to them for every single student. 132 00:10:03,400 --> 00:10:04,870 ‫So we created new objects. 133 00:10:04,870 --> 00:10:08,410 ‫So this is a new object, that's a new object and so forth. 134 00:10:08,410 --> 00:10:14,560 ‫So we have the student list which contains all the student objects, and we can submit those changes 135 00:10:14,560 --> 00:10:16,840 ‫again before we will see any effects. 136 00:10:16,840 --> 00:10:19,900 ‫So if you don't run this line, nothing will happen. 137 00:10:19,900 --> 00:10:24,760 ‫And then again, we wanted to show the students so the same thing with with the lectures. 138 00:10:24,850 --> 00:10:30,940 ‫And now here we created associations between students and lectures. 139 00:10:31,210 --> 00:10:33,520 ‫So we have lectures now which are math and history. 140 00:10:33,520 --> 00:10:40,030 ‫And what we just did is we said, okay, let's create a new student lecture where we just give the whole 141 00:10:40,030 --> 00:10:42,760 ‫object of Carla and the whole object of math. 142 00:10:42,760 --> 00:10:49,870 ‫And the cool thing is this student lecture knows by its or lost on itself what kind of information is 143 00:10:49,870 --> 00:10:50,320 ‫required. 144 00:10:50,320 --> 00:10:55,570 ‫So it takes the ID of Carla and it takes the idea of math, and then it creates them in student lecture. 145 00:10:55,570 --> 00:10:58,030 ‫So it's taught there correctly already. 146 00:10:58,030 --> 00:11:02,500 ‫So we don't need to do that manually, but of course we can do it as we have seen here. 147 00:11:02,500 --> 00:11:04,090 ‫So that's how we've done it. 148 00:11:04,090 --> 00:11:11,260 ‫Alternatively, which is a, let's say, not as good or not as beautiful in short way, but that's totally 149 00:11:11,260 --> 00:11:11,740 ‫fine. 150 00:11:11,740 --> 00:11:17,620 ‫And here I just use the student instead of the student ID Of course I could have done like that student 151 00:11:17,620 --> 00:11:24,610 ‫ID and Toni Dot ID, so that would be, let's say, the very precise approach. 152 00:11:24,610 --> 00:11:30,400 ‫But as you see otherwise it works as well because it just takes the ID of Toni, even though if we just 153 00:11:30,400 --> 00:11:37,810 ‫give the whole Toni object, then we well inserted that on submit. 154 00:11:37,810 --> 00:11:41,170 ‫So we inserted those students and we submit those changes. 155 00:11:41,170 --> 00:11:44,050 ‫And so the students then get University of Toni. 156 00:11:44,050 --> 00:11:45,100 ‫Well, we just checked out. 157 00:11:45,100 --> 00:11:47,170 ‫Where is Toni at which university. 158 00:11:47,170 --> 00:11:47,800 ‫So we checked. 159 00:11:47,870 --> 00:11:54,280 ‫We created this Toni student, we created this Toni's University University, which is Toni University. 160 00:11:54,460 --> 00:11:56,680 ‫So that's pretty neat as well. 161 00:11:57,590 --> 00:12:03,890 ‫And then we saw how to get his lectures, how to get all students from Yale. 162 00:12:03,890 --> 00:12:07,550 ‫And now came the more interesting stuff where we had to join table. 163 00:12:07,580 --> 00:12:11,570 ‫So that's where we combined universities with transgenders. 164 00:12:11,570 --> 00:12:15,380 ‫So we had to combine students with universities. 165 00:12:15,770 --> 00:12:21,620 ‫And what was even more advanced was where we combined lectures with universities. 166 00:12:21,620 --> 00:12:28,400 ‫So that's a joint statement here, which is quite complex, I'd say, because we are going this long 167 00:12:28,400 --> 00:12:34,010 ‫way from lecture to students over student lecture over student and so forth. 168 00:12:34,010 --> 00:12:36,950 ‫So we're connecting those different tables with each other. 169 00:12:37,550 --> 00:12:41,810 ‫And then we saw how to update data and how to delete data. 170 00:12:42,880 --> 00:12:43,530 ‫Great. 171 00:12:43,540 --> 00:12:47,630 ‫So I'd say that's enough for this video that was quite extended. 172 00:12:47,650 --> 00:12:54,310 ‫Not this video in itself, but the whole example here does link to a SQL example, but I hope you could 173 00:12:54,310 --> 00:12:54,970 ‫follow along. 174 00:12:54,970 --> 00:13:01,090 ‫And now you know how to use link to SQL and you see how powerful it is and in general how powerful link 175 00:13:01,090 --> 00:13:04,960 ‫is because it's always the same kind of kind of stuff, right? 176 00:13:04,960 --> 00:13:10,960 ‫It's the same kind of code and the same approach and data source in this case is a SQL. 177 00:13:10,960 --> 00:13:15,250 ‫In the other case it was an XML and in our case it was just an array or a list. 178 00:13:15,250 --> 00:13:21,550 ‫So you see link is pretty powerful when it comes to handling data and displaying it correctly. 179 00:13:22,360 --> 00:13:22,840 ‫Great. 180 00:13:22,840 --> 00:13:28,210 ‫So I hope you enjoyed this lecture and see you in the next one.