Welcome back.
In this video, we are going to use the link in order to update and delete data.
So let's first of all, update data.
So let's create a new method here, public void update and I'm just going to call it update 20.
So I want to update Tony because let's say I want to call him Antonio or Tony's just a well short version
of his name.
So I'm just going to use the whole name.
And first of all, I need to get the Tony object and you can go ahead and just use the student class
here and call them Tony.
That will be data context dot students dot first or default.
What that does, it returns the first element of a sequence or a default value of the sequence contains
no elements.
And that's good because if we try to access something that doesn't exist, then nothing will work or
it won't work and we will get errors and stuff.
So in here I'm just going to say ste equal bigger sign first name is equal to Tony.
So we could of course use equals here as well, but that's another way of doing it, right?
So now we get Tony and what we can do now is simply say, okay, I want to change the name of Tony to
Antonio and Tony like that.
And now I'll need to submit the changes because otherwise nothing will happen.
So data context not submit changes.
And now I can display it on my data grid.
So item source will be data context dot students.
So I want to see all the students and now I will see that Tony is not Tony anymore.
He's Antonio.
And before we run the code, we of course need to call this method.
So let's comment that out here and let's update our good old friend, Tony.
Update Tony.
Now let's run the code.
And we are we have all our students and Carla and then here Antonio.
So we changed the name of Tony.
And that's pretty cool because we can simply get the whole object and adjust the components that we
want.
So adjust all the properties that we want and then we can just submit the changes and that's it.
So it's a lot easier than to say, okay, get the object where the ID is so and so where the user is
so and so, then change or update X and Y, Z.
So SQL is a lot more complex than just doing it like this.
So that's a pretty, very easy and cool way.
So you could try to update other students.
Now you can update lectures, you could update universities and stuff like that in a very easy way.
So just like you've seen here, now let's delete some a user because I'm not happy with this James user,
not because he's not because he's transgender, but because he doesn't have any object or any lectures.
And his name is James instead of James.
So I did a mistake there, so I want to delete that object entirely.
Or maybe he just left the university so it's not part of units anymore.
So I want to use the delete functionality to do that.
So delete Jane.
So I'm going to call it and I need to get Jane first.
So student James is going to be data context thought students that first or default and here it will
be as t start name is equal to Jane.
Now there is a data context dot students method and as you can see, there are many.
Right.
And the one that I want to use is delete on submit.
So what that will do is it will simply delete this Jane object and I need to go ahead and context that
submit the changes.
Submit changes like that.
And now if I run that.
I might run into errors.
So I'm going to show you.
Let's show the students again.
Let's go back to the top where we updated Tony.
Now is the gym, the elite gym.
Let's run it.
And it worked so that we are Carla, Antonio and Lily.
They are still at the university and James is not anymore.
So in this case, in my case, it worked.
But if you run into errors, deleting it here, what you'll need to do is to rebuild the connection.
So I'm going to show you real quick how you can do it.
So if it doesn't work for you, so if you run into an error, then instead of running this line of code
here, you need to add those three lines.
So first of all, you need to set the connection string again with the correct one for you.
So in my case, it's not student manager, but it's in this case it's the link to SQL.
So it's exactly the same link here and I'm going to use it here.
So this is just bug fixing or, well, a workaround.
All right.
So if you run into this error, that's how you can solve it.
Then you create a link as to SQL data, classes data, connect the context called DB, and then you
can set DB students as your item source.
All right.
So that's if you run into this error.
But in my case, luckily it worked well, so I'm just going to keep it as is.
So we have this delete game.
All right.
Now, a little summary of what we've done because we've done quite something and there is even something
that I haven't touched on at all, which is quite important as well.
And you might have noticed that whenever you do something like that, there can be plenty of errors.
So whenever you work with databases and so forth, there can be a lot of errors.
So whenever you submit or stuff like that, things just could go wrong.
So then try and catch.
You try and catch in that case because otherwise, well, you could crash a whole program, right?
And that's not the whole idea about this or of this.
So you try and catch wherever you have a potential error and there are many here.
So please go ahead and try to fix all of the potential errors so that your program will never crash.
So let's say we have an incorrect name or we have.
Yeah, we're trying to access something that doesn't exist.
Or we're trying to put something somewhere where the somewhere where we put it, it doesn't exist.
So those are things that you always have to be very careful about.
And now let's just sum it up.
Let's go through the different methods that we have.
So we have this insert universities, and that's generally where we, first of all, gotten rid of all
the universities because each time we executed that method, we created those two new universities.
So we added them on top each time.
So we delete it all first and then we added them step by step.
All right.
Now this execute command is just to execute some SQL Command.
So this is delete from university.
So it deletes all the content from university.
So from the university table that we have here on the side now we create objects of a specific class
and this class, where does it come from because we haven't created a class entry ourselves?
Well, it comes from this link to SQL data classes, which is amazing because it allows us to just drag
tables in here and then we can use those tables as classes.
And those properties that there are are simply the properties of the class that we can access and adjust.
So we can create objects of that and adjust them accordingly and even store them so we can store them
inside of that specific data context or in that table.
So we have this data context, which is the link to SQL data classes, data context, which again is
connected via our connection string, which is our connection to our database.
So our connection here, data connection that we have.
Next we saw how to submit those changes.
So we add something, then we add that to the or insert on submit and then we submit.
So that's always the approach if you want to add something and if you want to show it on our data grid,
which is a very basic.
Object that we have here in our XAML file, which is just a data grid in Zemo.
Then we can go ahead and set this item source to that data context that we want to see.
So in our case, we want to see all the universities and that's what we get here.
So the same thing goes for students.
And as you can see here, because we added multiple objects at once, we just said, okay, let's create
a list and use insert all and submit instead of insert on submit.
And we created all of those students, Carla, Tony, Lily, Jane, all of them with additional info
here, like gender, like university ID and so forth.
And those are available in students.
So you have name, gender and university ID.
So we just used those and we added values to them for every single student.
So we created new objects.
So this is a new object, that's a new object and so forth.
So we have the student list which contains all the student objects, and we can submit those changes
again before we will see any effects.
So if you don't run this line, nothing will happen.
And then again, we wanted to show the students so the same thing with with the lectures.
And now here we created associations between students and lectures.
So we have lectures now which are math and history.
And what we just did is we said, okay, let's create a new student lecture where we just give the whole
object of Carla and the whole object of math.
And the cool thing is this student lecture knows by its or lost on itself what kind of information is
required.
So it takes the ID of Carla and it takes the idea of math, and then it creates them in student lecture.
So it's taught there correctly already.
So we don't need to do that manually, but of course we can do it as we have seen here.
So that's how we've done it.
Alternatively, which is a, let's say, not as good or not as beautiful in short way, but that's totally
fine.
And here I just use the student instead of the student ID Of course I could have done like that student
ID and Toni Dot ID, so that would be, let's say, the very precise approach.
But as you see otherwise it works as well because it just takes the ID of Toni, even though if we just
give the whole Toni object, then we well inserted that on submit.
So we inserted those students and we submit those changes.
And so the students then get University of Toni.
Well, we just checked out.
Where is Toni at which university.
So we checked.
We created this Toni student, we created this Toni's University University, which is Toni University.
So that's pretty neat as well.
And then we saw how to get his lectures, how to get all students from Yale.
And now came the more interesting stuff where we had to join table.
So that's where we combined universities with transgenders.
So we had to combine students with universities.
And what was even more advanced was where we combined lectures with universities.
So that's a joint statement here, which is quite complex, I'd say, because we are going this long
way from lecture to students over student lecture over student and so forth.
So we're connecting those different tables with each other.
And then we saw how to update data and how to delete data.
Great.
So I'd say that's enough for this video that was quite extended.
Not this video in itself, but the whole example here does link to a SQL example, but I hope you could
follow along.
And now you know how to use link to SQL and you see how powerful it is and in general how powerful link
is because it's always the same kind of kind of stuff, right?
It's the same kind of code and the same approach and data source in this case is a SQL.
In the other case it was an XML and in our case it was just an array or a list.
So you see link is pretty powerful when it comes to handling data and displaying it correctly.
Great.
So I hope you enjoyed this lecture and see you in the next one.