Gay guys falling or previous activity of having already to model to our project we will no proceed to
starts running queries to collect data from our database and somehow present it in our application.
So our first activity is to deal with well existing data so we know we have data in our table we put
it there in the types of cars table where we have for cars to be selected from and the objective here
is to modify our list for the dropdown box then bind it directly to what is coming from the database.
So I went into the design I went to edit items and I know there is nothing inside of type of car that
is empty and this does verify how empty it is.
So I'm looking at the application itself and you see there is nothing in there.
So the expectation is that after all our operation we will see the cars in there.
All right so let me just close that go over to the code behind and then we can get started.
So the first thing that I want to do is create an object that represents our data model.
So remember I said that there is a few names here that we need to know strictly.
Those are properties and then that we need to know the name of our entity container which is car into
entities right.
So in the code behind on the public partial on the right side of the control the constructor sorry.
I'm going to enter private and say Read only some declaring an object of type.
Car rental entities.
So this object embodies this entire space is entire space that you're looking at the entire diagram
every entity represented in the diagram is encapsulated by the type car rental entities.
So by making reference the car into entities giving it an object name some want to call the car into
entities that come on C and I to just do semicolon and then so this object is going to give me access
to every single entity that is inside of my model or every table that's I would inserted into the model.
All right.
So Mark in the code behind the next thing I want to do is actually initialize it.
So this is a declaration but it's not initialized right.
No this is no right.
So I'm just going to see car are into entities is equal to a new instance off and car or into entities.
Please note var wouldn't work here.
You can declare parts of a class as var All right.
So you can do variables as var we can do properties of class as var.
So this is a private property of our farm.
All right.
So once again we've declared a private property of type Car Into entities car into entities being the
name of our entire database model.
All right.
And then I give you the object name with the same name.
This would come on see and here in the constructor I'm just initializing it to a new instance.
So it's no longer No it's agreed to because no it's not being used.
But don't worry about that just yet.
All right.
The next thing I need to do is to figure out how is it I'm going to retrieve the data from the database
so that it is present by the time the form comes up.
So the expectation is that when I open this application the form loads and it will load with the drop
down list fully equipped with the data it needs to care of operation.
So that means I need to query the database get the information from the database have it presented in
the form all of that by the time you will the user would be ready to click the dropdown box.
So I'm going to add what we call a farm load events I'm just clicking the form.
Notice I think farm and the box sets off or the canvas itself is selected and then I go to properties.
The lightning bolts and I'm going to look for the Load event.
See.
So I want something to happen when the farm is loaded.
So when the farm loads I want to run that query very quickly.
All right.
So I want to generate a formal event.
I'm just into the thinking that I need seeing farm one on the score load.
So then seeing what do you want to happen when the farm loads.
So when the farm loads once again I want to query the database someone to say something.
Var cars is equal to so I'm declaring some variable called var I'm calling it cars are sorry.
Some variable of type.
Var calling it cars.
And then I need to assign something to it.
And what I intend to assign to it is records from the database from the table types of cars.
So when I declared this entity up top here are these objects rather of type Car Into entities.
This represents my database so I can then see car rental entities and then use my dots because it's
an object so I can use my dot upwards as to see the properties therein.
And then when I say dot you'll notice that I have my table being made references of car rental records
if I go back to my database and look car into the red card was the name of the table but then it has
many records.
So that's one of the things the month the model does by default do try some plural lies the table names
because I mean I call it it's a table name car into a record but it's really a car into our records
system.
So I really should have called a car into our records but that's beside the point.
The fact is that using my dot operates I can access this table.
So that's on the table I want.
Do I want type types of cars.
All right.
And then I want this in the form of at least someone to see not to list open and close parentheses.
And so what this is doing is using a language or mechanism we call a link.
So it's an easy to see sharp but the library of is called the link and it is a nice way to query your
database using need to see sharp code.
So once again I would of basically established an instance of a connection to my database through the
declaration of this property.
I initialized it in the constructor so that's all constructors are for the initialized.
Things are initialize see initialize component there will initializing my database to a new instance
and then when I want to call in the database I use that object that was previously declared and initialized.
I use my dot operator and then I can access my table and then basically that's it.
So if you're used to that literally that's it.
And I'm just converting into all of these.
So on a specific date that I have called a C sharp at least and that is why I have the list.
So if you use the database development this is the same thing as seeing select star from types of cars.
But then this is askew.
This is not native C sharp so to be able to write this kind of obscure would've taken far more lines
and you know I would love to initialize a data reader than a data table to call it the objects and a
bunch of things but link makes it so easy because you just call the database model.
Call the table and then if you want it in a list you just say so it is then it's easier to just say
to list because that's a more universal data type and it's easier to maneuver with.
So now that we have the list of cars stored instead of for variable cars we need to put it to use so
what I want to do once again is when the farm loads which is going to fire this event we want to get
the list of cars from the database and then have the combo box display them.
All right.
So the next step is to say call our combo box at a CB type of car right.
And then we're going to see display members.
So there are two properties that we're going to set.
First we want to set the display member and that's who's going to be equal to name.
So display members it takes that you will see it.
So remember that when we had it set up the we setting the items and you would see that takes Tondo you
would see that takes Toyota etc display a member is the takes that you see.
And so if you look back at your database design you would notice that the idea is one two three four
you don't want the person to be seeing one two three four that means nothing to them.
But the takes that you want them to see is the name Honda Lada buggy Toyota.
Right.
So name is the display a member and the value that we're interested in would be the idea.
So we have volume member.
Display a member and you have value member because it's actually the value that we want to store not
the display.
Once again we have a car into our card that has a type of car idea of type int which means we only want
to store the idea associated to the type of car we don't need to know the name again because that's
why we created the table to just store the names and the I.D..
So once we make reference to that I.D. from the car into our record we can look back and see that oh
if it was eighty three then we know it's buggy or the person selects buggy we want to store I.D. three
on the red card.
All right.
So the volume member would be what you intend to really store and that's in the background.
Right.
And then after sitting those two I am going to see that the data source for the items to be put into
the combo box so that CB type of card data source is going to be equal to cars.
So let's go through this again.
I'm querying the database for the list of cars and then I'm saying that I want my combo box whatever
data source it gets lots of little bits of data gets does look for a display a member for a name set
the volume member to be I.D. and then said the data source to be the cars.
So and I get the list of cars they're all coming over with I.D. and name someone to display the name
and when to store the I.D. And I'm going to set that this list of cars with I.D. and name should be
the source for the list of items coming into this combo box.
So let's start and see how that works.
And voilà.
So now you're seeing a list of items for our farm or for our drop though and sermon by cutting them
out earlier.
So loaded.
There was nothing in there so you're seeing that data is actually in there and this data is going directly
from the database.
And to prove that I want to close that window go to the database.
Add one more car.
So we added one more car to our fleet.
And this one is a Subaru.
All right.
So now we have five cars in our fleet.
And I want to go back execute again and when we look in the list the expectation is that we're going
to see the Subaru that was just added.
So that's a zero first step towards connecting to your database.
So let's just review what we did.
We set up our farm load events because we realized that there's certain data coming from the database
that needs to be presented or our farm.
By the time the farm loads.
So after initializing a connection to the database through the use of my object our reference to the
entities models.
So we have our entity model.
I initialize an object of the type of that entity model.
All right.
So I declared it's up here initialize it here and then I realized that I needed data from that database
before the form loaded.
So I set up a farm load event which queried the table and basically just did the initial steps toward
setting up the combo box so these steps are pretty much universal.
This is not you.
This is not unique to the fact that I'm using the entity model and this and that.
This is how you set up a combo box to inherit from my list of items whether it's a static list or a
list going from the database where they're using link or you're using the other methods that are there.
The fact is that this is all you data by and are you buying the objects to your controller or to your
combo box rather.
All right.
So once again we run our query got to stuff cars from our database.
Literally if you read from right to left that's left that's all it is saying give me a list of the types
of cars from the database and store it in this variable and then later on we say that we want to display
the name and store the idea and then the data source should be from the list of the cars that we just
retrieved from the database.
And so in our form loads we see that it is running that query and getting all that data if you want
to put our breakpoint here and restart application and just go step by step to see exactly what happens
just to be sure.
Then you can feel free to do so but that is our first step to connecting to the database.
In the next video we will be looking at how when we click submit no all we do when we click submit is
you know display the information.
Next time we want to actually save it in our car or into the record table.
So that's all we'd be doing in the next video.