We'll come back.
Now that we have everything set up, we can go ahead and create our first code here.
Well, we have some code, but it's not the magic that we were expecting.
Right?
So let's go ahead and create a method which will insert universities into our university table because
we have this table university and if we check out the data.
So open table definition, it's not the one, but open or show table data.
If we do that, we see that it's empty.
So we have no idea, no name yet, but we are going to change that with code.
So let's create a new method and it's going to be a public void, insert universities and this method
should simply insert universities.
Once we call that, it will add universities to our university table.
Now in here, what I'm going to do is I first of all, need a university.
And in order to use this university class, because we didn't create a university class.
Right.
And I want to use university objects.
I need to go ahead to my link to SQL data, classes, DB, HTML and drag university in here.
So from my tables I drag university in and now I can go ahead and use university in here.
So it will take a little while, but now you can see now it's green and now we can use it.
Now let's go ahead and create an object.
I'm going to call that Yale, which will be a new university.
And this Yale object should have a name.
And as you can see, Yale has a name.
And that's pretty cool because when we can access its ID, we can access its name so we can access all
of its properties that we have set up.
And when we set up those properties, well, we did that in our table.
So you can create objects of your table and that's pretty cool.
So you can simply use this link to SQL data classes, HTML, and drag your tables in here and it will
create classes for you.
And that's pretty cool.
So we can now use the name and I'm going to set that name to Yale like that.
Now, in order to insert Yale into my data table with this name, I need to go ahead and use the data
context that we have set up in the last video.
So this one link to the data context and here in universities, so there is a property called universities,
it contains this method here insert on submit.
And what do we want to insert?
Well, an entity of university and the entity that I want to enter is Yale.
So what I'm actually doing here is I'm entering data into my table of university.
So this code does that.
And that's pretty neat, isn't it?
So in order to submit those changes, I simply do something at that data context that submit changes
and that's it.
So now I submit those changes, meaning I submit this insert on submit.
So as you can see, it's only inserting once it's submitted.
And in this line here I'm submitting now, in order to display the university on my table, I need to
call my main data grid and set its item source to the data context thought universities.
All right, so if we check what that is, well, main data grid is in our XAML file.
Is this data grid currently?
It doesn't have any data.
It's pretty empty.
Right.
But it's this data grid where we set this name to main data grid and now we are setting its item source
to the data context university.
So it will show us all the universities that are inside of our database or inside of this table.
So I'm going to call this Insert University's method.
And run the code.
And if I do that, you see that we have this ID one and the name Yael in here.
So that's what our code does already.
So no SQL entries yet.
We didn't need to use, wear or put something into and so forth.
We did all of that with code and that's pretty easy.
So you you create an object of your.
Well, in this case, it's a table name and you add some values to it.
In our case is just the name and you insert that on submit and you submit those changes.
And of course, if you want to see something, you need to set the item source as this data context
universities.
So now as a little challenge, add another university into the table and run the app.
I hope you tried it.
So I'm going to create a new university.
University?
It's going to be Beijing Tech, which is a new university.
And now Beijing Tech needs a name.
So I'm going to set its name to something like beI Jing Tech.
And now of course I need to call Data Context University's Dot insert on submit.
And in this case it's Beijing Tech that I want to submit.
And as you can see, the submit changes is also or is only needed once and here.
This is not all on submit but on submit.
So this data context submit changes will be executed once we don't need to execute it after every single
one, but we only need to execute it when we are ready to submit all those changes that we have prepared.
All right.
So now let's run it again.
And we should see two entries in there.
And I hope you tried to do that.
And actually, we see three entries.
Wow.
Why is that?
And you might wonder, but well, we ran the code already and now we run it again.
So we add all the universities again.
So what is going on?
Well, that's pretty much what we intended.
Well, at least in our code.
So this is exactly what we do.
We add new stuff.
We don't say, okay, this is all the stuff that we want to have in our universities table.
But we say, okay, we add those two new lines.
So if we execute and run again.
Or close and execute.
Again, we see we have Yale, Yale, Beijing Tech, Yale, Beijing Tech.
And now in order to clean that up, in order to only have the entries that we have in here, only those
two we can do something like that.
Data context dot.
Execute command and here we we simply use SQL code delete from universe C T like that.
So here it's needed to be correctly written.
So delete from university.
So this way I simply delete all the entries and then I add them.
So now we should only have two entries in there.
So only Yale and Beijing, as you can see, Yale in Beijing.
But what didn't change, as you can see as well, is the ID.
So it's still going up, up, up.
All the entries that we had before were all deleted, but the ID didn't go back to its starting value.
So that's something that you need to keep in mind.
All right.
So now you know how to insert data in there.
So now a little challenge, insert students and yeah, of course you can try to do that, but it's going
to be a bit difficult, I'd say a bit too difficult for now and I'm going to show you.
So let's create a new method and I'm going to call that insert students.
So public void insert students.
Because what I want to use here is a lambda expression.
And we haven't used lambda that much yet or not at all, based on how far you've gone in the course.
So what I'm going to use is I'm going to use a university here as well.
So I'm going to create a Yale University, but I'm going to use data context, dot universities so this
property.
And then if I open that up, as you can see, there are many different methods available here and the
one that I want to use is called first.
And what first does is it returns the first element of a sequence.
So first is going to return a the first element of a sequence.
So what I can do here is I can run a lambda expression in here.
So what I'm going to do is I'm going to say N which stands for university, should give me a UN name
that equals Yale.
So this is a very, very short line and it does a lot of stuff.
So it's it's pretty extended.
So it's very similar to, let's say, this statement here I'm going to show you.
So let's say this statement here.
So if I had to write it in SQL, it would look like that from university in data context.
University where university is equal to lane a Yale select that specific university.
All right.
So from university in data context university, we know what that is.
That's for each loop.
Then we say where the university is equal to a specific name and then select the university.
But instead of doing that, we simply have this very short piece of code which does exactly the same
thing.
So it's going to return the university and it's going to check for the first entry in that sequence.
So it's check.
It's checking for the first entry that is called Yale, and it's going to store that in Yale because
the difference of that is what that will return us is a i enumerable right.
It's a collection that will be returned if we run that code.
But the cool thing is here with this line, it's not returning a collection, but it's returning an
actual object of university and we can use that further on.
So it's, it's mighty, it's pretty -- strong and very short code.
So if you want to know more about what's going on here, then you should check out Lambda Expressions.
But I'm just going to use it like that for now because it's well, lambda expressions are a lot more.
They can do so much more than just this specific line that we have here.
But I'm just going to use it like that for now.
So I'm going to do the same thing for the other university, which will be beI Jing Tech, and we search
for Beijing Tech like that.
So now we have two different universities, the Beijing one and the Yale one.
Now instead of adding students to the universities individually, I'm going to add a whole list of students.
So I'm going to create a whole list of students and put them into the correct university.
And in order to do so, I'm going to, first of all, shut down the server explorer.
So I have more space here and.
Now I'm going to create a list of student and I'm going to call that students is a new list of student.
So now it's complaining.
It doesn't know what student is.
So what we need to do is we need to drag the student in here from our server explorer.
So open up server explorer again and drag your student in here.
So there we are.
And as you can see, they are connected.
And they are connected by the University ID.
Now we can go back into our code behind and after a little while students should be recognized that
we are.
And now we can use student as a class or we can make objects out of it.
That's pretty cool.
So let's create a new entry.
So we're going to use the ADD method, which will add an element to the list called students.
And what do we want to add here?
Well, I'm going to add a new student object.
So I'm going to say, okay, give me this whole student object with certain information.
So, for example, with the name of Karla, the gender of female and the age.
Well, actually, we don't need the age, but we need the university ID and I'm going to say something
like Yale Dot ID.
All right.
So like that.
Just like that.
With this entry, I have added a student to my student's list and I could use the student's list to
add that to my data context in the same way that we have done here.
So insert on submit.
I can drag those students in.
It will add this whole list of all the students that I have in my list.
All right.
So that's pretty powerful and cool.
But now another cool thing is, instead of using the university ID, I can just hand over to university
itself so I can create a new student entry.
I'm going to say new student with the name of Tony
and the gender of male
then and then university.
And as you can see, there is a property called university and I can just say it should be the Beijing
or in this case the Yale.
So let's say Tony is in Yale and let's write it like that.
So Tony is in Yale and now I'm going to add some more students.
So as you can see in this case, we use this university ID, now we're using the university and both
are fine because they are connected to each other.
A university has a specific ID and we use them pretty much same thing here.
Now I'm going to add some more.
Layla is going to be female and her university will be Beijing Tech and the last one will be James.
And James is a transgender.
So we have three genders here as well.
And this one will be Beijing Tech.
All right.
So that we are we can insert students very easily.
And now for use a little challenge.
Add that to the data context.
All right.
I hope you tried it.
Data context dot students dot insert all on submit.
So here we used insert on submit and it was just one entry but now insert all and submit takes an i
enumerable so a list I enumerable accepts list.
So this list is of type list, but it's a generic connection, so we can use it in the same way here.
We can just hand over a connection collection here and it will be my students.
So submit students like that.
And of course we need to submit the changes.
So submit changes like that.
And now my main data grid should be item source items source.
And in this case it's my data context dot students.
So now I want to see the students in my grade and it should be those for students.
Now let me call insert students instead of insert universities.
So insert you students here like that.
Now let's run the code and we should see four entries in our table.
And now, as you can see, we have ID, name gender university ID and the university, which in this
case is a link to SQL University.
So it's an object of type link to SQL University, so it doesn't tell us the actual name here within
the university.
All right.
But that's something that we have set up here and in here.
So we said it's the University of Beijing Tech.
It's the University of Beijing Tech.
But these are university objects and they don't equal the name.
So they are more than just the name.
They are the actual university object in there.
But other than that, you see that we have Carla, Toni, Layla, Layla, Jane and well, I didn't write
the names correctly, but we have also the gender and the university ID and as you might remember,
University ID six was Yale and University ID seven was Beijing Tech.
And that was because I deleted entries.
And then I call this insert university method multiple times, and that's why we had those IDs instead
of one and two.
All righty.
So if we check out our data, we should see the same thing.
So let's do that.
Let's check out our student data.
Show table data or data table.
We see Karla female.
And what you also see is university ID.
So there is no additional column called universities.
That's something that we have just set up with code here as well.
So we didn't create them permanently, but we set them up here and they are displayed because the data
context contains them as we have added them in here, you see.
Okay.
So that's quite a bit about well link and not just that, we also used a lambda function here in this
piece of code and we will do that a little more in the next video.
So no worries, you'll get used to it.
And yeah, in the next video we're going to do something with an associate of table between students
and lectures.
So we're going to add students and we're going to add lectures to the students and so forth.
So see you in the next video.