Welcome back.
Now that you know how to create your first little queries, how to set up everything with a database
and so forth, it's time for us to go ahead and get started with the UI and do some crazy stuff with
the UI.
So let's go to the main window XAML file.
And we can we can leave it like that.
Let's close this so we have a little more space here.
And actually like that, it's fine.
All right.
So, first of all, what do we need?
We need a list in here.
And I used a list box.
And actually, let's check it out here.
We can see we have the label, then we have the list box buttons and so forth.
And we're going to start with the label and the list box first before we do any more than that.
And while we could go ahead and do all of that stuff very clean and a clean manner, creating grids
and all the different columns and rows and all those kind of things.
What I will do is I'm simply going to drag and drop everything in there and let them all do the job
for us.
Because this is well, this tutorial in this chapter is not that much about WPF.
It's a lot more about the database component.
So I'm not going to spend too much time on XAML here.
So in order to get something onto our main window, I'm going to zoom in a little bit.
Actually, we don't need that big here, the symbol part, because we're just going to see how the code
will be added up and in here.
I'm just going to drag stuff in.
So what do I need?
Well, I need a label, so I'm going to start with the label.
I'm going to drag it in and I'm going to put it just here at the left top corner.
And instead of label, it's going to say Zoo List.
Then I'm going to add a list.
Box.
Which will contain all the different list elements.
And I'm gonna drag that just below list or below the zoo list, and I'm gonna make it a little higher.
I'm going to change the size by a bit.
So this one will be 500 times.
Let's say 725.
That should be fine.
Now, I can make this a little wider.
And as you can see, everything is being set up here at the bottom.
So there is a margin.
There's vertical alignment towards the top, horizontal alignment towards the left for our label.
Then our list box is horizontally aligned towards the left, has a specific height.
There is some margin to the top and so forth.
So you can see it's all set up here directly for us.
And what I want to do is I want the items to be seen here or the list elements to appear on my zoo list
and.
Therefore I will need to create a name for it.
So I'm going to add a name to my list box and I'm going to call that one name is equal to list Zeus
and well, it's the list of Zeus.
That's why I'm just going to call it like that.
Now let's go back to our code behind, because now I want to show our Zeus on that list and well, we
could run a query and all of that, but that's not going to be enough.
We need to use a specific approach here in C sharp and we can make it very cleanly and quickly with
Link, but we're not going to cover Link until the next chapter.
So for now, we're going to do it the old fashioned way, let's say.
And later on we're going to add the namespace link which will well increase the speed and reduce the
amount of code we need to write.
But for now, what we need to do is we need to have an SQL connection.
So I'm going to create an SQL connection.
And in order to use the SQL connection, I need a namespace here using system DOT data, that SQL client.
And now as you can see, a SQL connection seems to be fine and we can create an SQL connection object
and I'm going to call that a SQL connection.
Now, we need at one point to initialize that, and the best one is to do that in our constructor of
the main window.
So I'm just going to say SQL Connection is going to be a new SQL connection and now it needs well,
there are multiple different overloads.
And the one that I'm going to use is this one here.
String connection string.
Well, guess what?
We have a connection string.
We set that up in the last video.
It's this string called Connection String, which is from configuration manager of type connection string.
So it's a connection string and that's beautiful because that's exactly what we need.
So we're going to hand it over and I'm going to just say like that.
So now we have an SQL connection.
So our main window, so this program here are Double Zoom manager is connected to the database here.
And it's this DB connection string that allows us.
All right.
Now, what I want to do is I want to show all my zoos and therefore I'm going to create a new method.
And it should be private because nobody needs to see.
We couldn't make it public so we can access it from other classes, but I'm just going to make it private
so only this class can access it.
And I'm going to call that one show zoos and in here.
I need a query first.
So I'm going to create a new query.
String query is going to be a select star from zoo.
What that will do is it will select all from our zoo table.
And as you can see here, that's a table and it has all the locations in it.
So now we have this query and we can use that query with an SQL adapter, SQL data adapter, to be precise,
SQL data adapter.
And that SQL data adapter can now go ahead and create a connection to that database that we have and
run the query.
So that's pretty much what it does.
So here we hand over the query and then we hand over the SQL connection, as you can see here.
So we say a SQL connection like that, and now that's pretty much what this is going to be.
And it's a small letter SQL connection.
Like we want to have the instance of our SQL connection, not the class, of course.
So what it's going to do is this SQL adapter will run this query onto that connection.
So onto our SQL connection, which is connecting to our database.
All right.
So that's pretty much what it does.
Okay.
So you can pretty much see this SQL adaptor as a, well, an interface to make tables usable and see
sharp objects.
Then the beauty about SQL adapters is as well that we don't need to open a connection to SQL and close
it because the adapter is going to take care of that.
So it's taking a lot of weight from our shoulders, pretty much.
So what we need to do here is use the using keyword and use our SQL data adapter that we have just set
up.
And in here we can use it.
So I'm going to create a data table which is going to be my zoom table.
And it's a new data table.
Well, not complaining.
It doesn't no data table yet.
We can fix that by using the class system dot data.
So using system data.
And now here I actually made something weird here and just want to create a new data table here.
So what this data table allows us is to store data from tables within a object.
So pretty much a zoo table is an object of our table or an object of C-sharp?
Pretty much.
So this allows us well, it's pretty much an interface between the table and what we have here.
So data table and data set, they are two different classes which we can use to get some data from our
database.
And now I can run my SQL adapter with the fill method, and it's going to fill the zoo table because
now we just created a data table.
Now we fill it with the SQL adapter.
So pretty much with all the content that is given to us from the query.
And what we can do now is get our list views and display a member path, and that's going to be the
location.
And then we need to have the idea as well.
List zoo.
It's going to be selected value path.
We're just going to be our ID.
And now finally we have list zoo's item source.
And that will be our zoo table.
Default view.
So default you get a customized view of the table that may include a filtered view or cursor position.
So what we're doing here with those three lines is we say that the column pretty much of our list zoos
with the content that we want to see is going to be the location from our table, then the selected
value path.
So the value behind the specific item within our list is going to be the ID and as the item source,
we set the zoo table here default view.
So we simply say that the content from our zoo table is going to be the item source for our list.
So it's just going to fill our list now or a list box with the content from the zoo table.
So which information in of the table and the table data table should be shown in our list box.
That's the display member path.
Then which value should be delivered when an item from a list box is selected?
That's the selected value path.
And then finally reference to the data the list books should populate.
And that's the item source.
Well, our zoo table default view, which is the data table which contains all the data.
Now that's great and we can run that.
But there is one thing that we need to consider and well, let's just run it before we consider that
and before we take care of that in a second.
And as you can see, there's our zoo list and it's empty.
And that is because I'm not calling it.
So let's call it in our main window here.
Let's call this show Zoo's method.
And of course, before we run that, it's important that location starts with a capital L, because
if I look at my zoo, then location the chapter data.
The location column name started with a capital L.
Now, if we start that, we should see our zoos on the screen.
And there we are, New York, Tokyo, Berlin, Cairo and Milan.
And if you compare that with your list, so I'm going to close that and we compare it with the data
in the zoo table.
So show table data we can see in New York, Tokyo, Berlin, Cairo and Milan.
All right.
So that was the first step towards our little application.
And as you can see, it's not that difficult when you use a adapter because it's opening the SQL connection,
it's closing the connection as required and it's pretty well short.
The code is not that long, but we can even shorten that later on.
There is only one thing that I would like to still add, and that is I try and catch because when you
use something with databases or connections and stuff like that, it's always good to use, try and
catch because there could be an error, something could happen, an exception could come up.
And I'm going to just catch that with a generic exception.
And this is going to be message box, dot show and it's just going to show the E to string.
So it's just going to show the error because we could, of course, catch all the different exceptions
that could come up whenever we use that.
But I'm just going to keep it simple and just add this one generic exception and it's going to show
us what the error was if showing the zoo should not work for some reason.
All right.
So that's it for this video.
And the next video, we are going to add the functionality so that we can show the associated animals
of that specific zoo that we are clicking on.
So see you there.