Hey guys in this video we'll be building oh the functionality for saving our form data to the database
so open to know what we've accomplished was that we built our database one we imported a data model
into our projects to represent our database and then we looked at how we actually invoke our connection
to the database.
So in our previous lesson we would have initialized the objects that connect stored database.
We we made a reference to it or we declared it and then we initialize the constructor and then don't
hear enough form one load function.
We would have run our first query using a link.
And so I'm just showing you that this is the same as seeing so star from types of cars.
That's the same thing as this line.
And then we just feel the combo box with a list of cars in the database.
No the expectation is that when we click submit and I'm just really a preview of the form when we click
submits.
The expectation is that we're going to be saving the data that was entered to our farm.
All right.
So I'm willing to modify the code a bit.
So we have a button click event that we've been using to get the data from the farm.
All right.
And then do some validations and then if it's valid then we display a message.
If not then we display.
Well if it's valid that we display a of message.
If it's not then we display error messages.
All right.
So what we're going to do is modify the is valid portion.
So if it is valid then I want to see the data and then I can display the message if I can see the data
then clearly I don't want to display the message.
So when we want to see data we need to declare an object of the type of relevancy of best I can explain
this.
So when I go back to my model.
So I feel you know it kills the model by accident.
That's fine you just go back to silos and explore and then you see the dot EDI x file second is a little
booklet that and relaunch your model all right.
No whatever the boxes name is.
It's usually much as the name of the table.
All right.
So you see the box there is the car into the record.
You see this one is types of car.
And if I look in the solution explorer if I dropped on the E.D. and makes the file then you see that
you have a dot see files per table.
So you have car into a card and they have types of cars no types of cars sorry.
So those files represent class files that represent your table.
These are classes that are directly mapping every field in your tables so that when we did the car into
record we know we set up an I.D. customer named data entered all of the fees that we put in the database.
I right here in our class file.
So the cool thing about this process I'm using link and Ada dot net is that I can create a new object
of this class type and then I can see this object into the database.
So let's go through this step by step.
The first thing I want to do is in the block that says is valid I want to declare a new object that
I want to say var or in tile or a card is equal to a new instance off car or intolerant card.
Right.
So once again this class directly matches the car into a card table that we created.
Here are all the fees that are available to us and they directly much the columns that are in our database
which means that I need to fill these fields with the data that I intend to store inside of the database
fields.
All right.
So while all of them with the exception of I.D. because we created I.D. to be auto increments so I don't
need to provide a value for I.T. but customer name is going to be entered in on the form.
They will go to the design course.
My name is going to be entered into the form in the code behind we're already collecting the customer
name I experience in a variable customer name.
And so what I'm going to do is try and mop whatever value is being stored in this variable Muppet into
my new rental record of type Car Rental record.
All right.
So not that type of object that represents my table.
I want to say that the rental record Dot's customer name field is so the name field in my table should
know have the value of the customer name value that I just collected from my farm.
All right so once again this is the table.
This is the textbook sorry store in customer name in the wind farms.
I'm collecting the text and I'm storing it in my local variable called customer name.
No no that's I've collected it locally I want to push it to the database and want to declare an object
off the table that I intend to fill with the data and then I'm going to say that this objects field
called customer name which directly maps to the database field.
I wanted to have the value coming from my wind farms application right.
And then I want to repeat that process for every other one.
So a rental record dot data printed is going to be equal to and which field what do I house during data
oriented data rented.
Value is being stored in my variable data out.
So I kind of deliberately named them kind of sparsely so you could see the difference and not get confused.
So data oriented is going to have the value that is stored inside of data out all right in the same
way.
And it is that control D to duplicate that line data returned is going to have the value that is being
stored in it in all right.
So you just below your your model so for every field that you intend to store data for in your database
for a field that is not present in your object you just fill them all with a corresponding data for
right.
So for Intel or card docs and then the next one will be cost that is equal to my variable cost.
And then the final one is the car data.
So no I have added the error here with my cost some storing cost does double.
But then the class type is decimal because I declared it does this amount instead of my database.
All right.
So cost is decimal right then we just see.
Or I could have used float so I could change the data type here just to make it okay but it's probably
easier to just cost it's here.
So I can probably see something like decimal dot.
And then I would just pass a decimal dot pass and the passing cost.
All right.
And that will take care of.
Okay.
Slight miscalculation there that did not take care of that because the pyres foams and is expecting
a string and then passing in a bubble.
So let's re gather our thoughts.
I think maybe I can use an implicit cost here and just say this in.
Let's see if that works.
So sometimes you can actually do a costing like this.
So we looked at two other ways to do cost.
You can do data type dot pass or you can see converts dot two and we use one of those functions.
Here's another way to do our costings.
So you just use parenthesis the data type you want to cost.
And then the value comes afterwards.
Right.
This works sometimes.
So situationally you just find the one that works and you move on.
Right.
So now we have the cost and the final value that we need to go into our table which is coming from our
wind farms would be the type of car I.D..
Right.
So the type of car I.D. is an integer because it is the foreign key value that needs them up.
Back to the primary key value of the car which is 1 2 1 2 3 4 5 based on the data that we have here.
So we already know that we're loading up the combo box with the name and the I.D..
But what we're collecting from the combo box is the text.
So if I look at it I'm really collecting the text I need to collect the actual idea behind the combo
box so to get the selected value literally the combo box is going to have a property called selected
value.
Right.
So I'm going to go back down here to my type of car I.D. and then I'm going to say CB type of car.
So this is my combo box property.
SARA This is my combo box control.
Right.
And then I say dot then I want to see that there is a property that is selected value so I can scroll
through.
If I'm not sure how to type it but selected value.
All right.
And then you would think that that sizable if you hover over a general is that this is just a generic
Object type.
We're trying to insert into an integer so we can use another implicit data type conversion there where
we just say open bracket.
Give me an entire presentation of whatever the selected value is and the selected value once again would
be whatever value is mapped to the item that is selected based on the fact that we have value member
being Heidi.
All right.
So what I'm going to do is create some breakpoint on that is valid and then I'm going to run.
And then we're going to step through and just see how with our table object gets filled those.
All right.
So I feel that it's already customer name is this user costs this twenty five hundred.
Let me change the dates so that we don't violate any of the validation.
And I want to go with bogey someone to click submit.
We hit our break point is valid is true.
So we can just use if you live and to step through and then we see here one where declaring an object
that represents our table.
All right.
So no you've declared object no we start filling it and I'm just going to step through don't the type
of car I.D. And if I hover over my object and just drill down into it you'll see all of the values cost
got set test use of that said the two dates got said the type of car I.D. is not set yet because I stopped
on that line so if I press if you live in and it goes beyond that line then you can see that the type
of car I.D. is three.
All right.
So that's is how you get the actual I.D..
That matches the value that was selected from the dropdown once again.
That's important because we're actually going to be storing the I.D. instead of the actual name of the
car.
All right.
And so I can just press F5 and we go on to see our success message.
No.
When I look in the database I am not one to see the data.
So we just do it and show it.
So when I select top 1 those under rules from this table I'm not willing to see the data in this table
because well we didn't really see it.
So we filled up the table but we didn't do anything with the data that we filled it up with.
So I'm going to come out of debug mode and then we're going to finish up what we need to do.
So now that I have filled the table object with all of the data needs I no need to pass it all to the
database and then save.
And that's really two steps.
All right.
So I'm just going to say something like And then just remind myself of the name of the database object.
So Kyra into entities that represents my database someone's estate car rental entities dot and then
the name of the table or collection of records from the table is Kyra until the records then I see data
again known when to hide.
And then what I'm adding is and it's expecting an object of car into the record.
Guess what I have an object of car into the record.
Type in the form offering dollar a card.
Right.
So I created the object her entire record.
It is off type car into a card which matches the table.
And so I am going to add this object that I just built just finished constructing and putting all the
data in.
I'm just adding it to the collection of records for that table.
All right.
And then I'm going to see a database.
I want to save changes no one said do those two lines.
And then I click Start again.
I'm going to fill out the form make sure everything is valid and then click submit and then let's step
through one more time so I'm just going to do.
If you live through all of these lines and then that's filling or objects if I hover over the objects.
Once again look at the data that I want to see that the really fun fields are filled.
You see one with no.
And later on we'll get to why that is no and why does it matter at the time we're creating the object
or creating the record.
It doesn't matter that this is mode and it does so much of that this is zero.
Since I'm creating because it's going to auto increments anyway right.
And then I went to F eleven so it now gets the passwords adding it to the collection.
We F eleven and then we save changes and then it moves onto the next line so I'm just going to f five
and then we see or or you know a success message and we click Okay.
But then we see it we saw all of that before what we're looking for that's different though is the actual
record being in our database.
So if I go back over to school management studio and I rerun this query where I select the items from
the table then I'm going to see my record in the database.
And if you look you'll see that we have one as the I.D. because once again auto increment the customer
name which was a poor item that's built in the word test and we have the dates rented the data returned
the cost and the type of car I.D..
And once again this idea matches back to our record.
So what kinds of I.D. seeks if there is no Sikhs and that's why binding the combo box like would be
done at the former lord.
That's why I binding it is very important part so as many times as you fill out this form and putting
information and submits the expectation is that it will create a new record fill the record with the
data and then save it.
All right.
In our next video we'll look at building.
Although windows so that we can see you know a lot of types of cars that are in the database as well
as the rental records in the database.
We'll be designing our rights in the logic get we'll just be looking at extending the design of our
application from just one form to maybe many forms that can show us the data that we want.