1 00:00:00,610 --> 00:00:07,820 Gay guys falling or previous activity of having already to model to our project we will no proceed to 2 00:00:07,820 --> 00:00:15,730 starts running queries to collect data from our database and somehow present it in our application. 3 00:00:15,770 --> 00:00:23,030 So our first activity is to deal with well existing data so we know we have data in our table we put 4 00:00:23,030 --> 00:00:31,370 it there in the types of cars table where we have for cars to be selected from and the objective here 5 00:00:31,610 --> 00:00:40,430 is to modify our list for the dropdown box then bind it directly to what is coming from the database. 6 00:00:40,460 --> 00:00:48,230 So I went into the design I went to edit items and I know there is nothing inside of type of car that 7 00:00:48,230 --> 00:00:51,260 is empty and this does verify how empty it is. 8 00:00:51,290 --> 00:00:55,050 So I'm looking at the application itself and you see there is nothing in there. 9 00:00:55,070 --> 00:01:00,580 So the expectation is that after all our operation we will see the cars in there. 10 00:01:00,680 --> 00:01:07,540 All right so let me just close that go over to the code behind and then we can get started. 11 00:01:07,550 --> 00:01:13,930 So the first thing that I want to do is create an object that represents our data model. 12 00:01:13,940 --> 00:01:18,960 So remember I said that there is a few names here that we need to know strictly. 13 00:01:19,000 --> 00:01:27,800 Those are properties and then that we need to know the name of our entity container which is car into 14 00:01:27,890 --> 00:01:28,850 entities right. 15 00:01:29,240 --> 00:01:35,740 So in the code behind on the public partial on the right side of the control the constructor sorry. 16 00:01:36,110 --> 00:01:43,010 I'm going to enter private and say Read only some declaring an object of type. 17 00:01:43,010 --> 00:01:46,000 Car rental entities. 18 00:01:46,130 --> 00:01:53,480 So this object embodies this entire space is entire space that you're looking at the entire diagram 19 00:01:53,510 --> 00:02:01,860 every entity represented in the diagram is encapsulated by the type car rental entities. 20 00:02:01,880 --> 00:02:08,450 So by making reference the car into entities giving it an object name some want to call the car into 21 00:02:08,480 --> 00:02:17,750 entities that come on C and I to just do semicolon and then so this object is going to give me access 22 00:02:17,750 --> 00:02:26,600 to every single entity that is inside of my model or every table that's I would inserted into the model. 23 00:02:26,610 --> 00:02:27,740 All right. 24 00:02:27,740 --> 00:02:32,840 So Mark in the code behind the next thing I want to do is actually initialize it. 25 00:02:32,840 --> 00:02:35,470 So this is a declaration but it's not initialized right. 26 00:02:35,490 --> 00:02:37,670 No this is no right. 27 00:02:37,670 --> 00:02:48,190 So I'm just going to see car are into entities is equal to a new instance off and car or into entities. 28 00:02:48,380 --> 00:02:50,680 Please note var wouldn't work here. 29 00:02:50,690 --> 00:02:55,310 You can declare parts of a class as var All right. 30 00:02:55,310 --> 00:03:00,890 So you can do variables as var we can do properties of class as var. 31 00:03:00,910 --> 00:03:04,820 So this is a private property of our farm. 32 00:03:04,820 --> 00:03:05,630 All right. 33 00:03:05,720 --> 00:03:13,880 So once again we've declared a private property of type Car Into entities car into entities being the 34 00:03:13,880 --> 00:03:17,510 name of our entire database model. 35 00:03:17,730 --> 00:03:19,060 All right. 36 00:03:19,070 --> 00:03:22,400 And then I give you the object name with the same name. 37 00:03:22,400 --> 00:03:28,720 This would come on see and here in the constructor I'm just initializing it to a new instance. 38 00:03:28,730 --> 00:03:33,130 So it's no longer No it's agreed to because no it's not being used. 39 00:03:33,140 --> 00:03:35,480 But don't worry about that just yet. 40 00:03:35,480 --> 00:03:36,260 All right. 41 00:03:36,380 --> 00:03:43,070 The next thing I need to do is to figure out how is it I'm going to retrieve the data from the database 42 00:03:43,310 --> 00:03:47,420 so that it is present by the time the form comes up. 43 00:03:47,420 --> 00:03:54,230 So the expectation is that when I open this application the form loads and it will load with the drop 44 00:03:54,230 --> 00:03:59,400 down list fully equipped with the data it needs to care of operation. 45 00:03:59,400 --> 00:04:06,230 So that means I need to query the database get the information from the database have it presented in 46 00:04:06,230 --> 00:04:12,080 the form all of that by the time you will the user would be ready to click the dropdown box. 47 00:04:12,080 --> 00:04:17,630 So I'm going to add what we call a farm load events I'm just clicking the form. 48 00:04:17,670 --> 00:04:25,910 Notice I think farm and the box sets off or the canvas itself is selected and then I go to properties. 49 00:04:26,130 --> 00:04:30,460 The lightning bolts and I'm going to look for the Load event. 50 00:04:30,490 --> 00:04:31,210 See. 51 00:04:31,380 --> 00:04:34,730 So I want something to happen when the farm is loaded. 52 00:04:34,760 --> 00:04:39,590 So when the farm loads I want to run that query very quickly. 53 00:04:39,590 --> 00:04:40,210 All right. 54 00:04:40,370 --> 00:04:43,170 So I want to generate a formal event. 55 00:04:43,190 --> 00:04:47,610 I'm just into the thinking that I need seeing farm one on the score load. 56 00:04:47,840 --> 00:04:52,040 So then seeing what do you want to happen when the farm loads. 57 00:04:52,070 --> 00:04:57,680 So when the farm loads once again I want to query the database someone to say something. 58 00:04:57,720 --> 00:05:09,410 Var cars is equal to so I'm declaring some variable called var I'm calling it cars are sorry. 59 00:05:09,420 --> 00:05:11,220 Some variable of type. 60 00:05:11,250 --> 00:05:13,940 Var calling it cars. 61 00:05:14,120 --> 00:05:16,890 And then I need to assign something to it. 62 00:05:17,070 --> 00:05:24,140 And what I intend to assign to it is records from the database from the table types of cars. 63 00:05:24,180 --> 00:05:32,210 So when I declared this entity up top here are these objects rather of type Car Into entities. 64 00:05:32,280 --> 00:05:42,720 This represents my database so I can then see car rental entities and then use my dots because it's 65 00:05:42,720 --> 00:05:47,220 an object so I can use my dot upwards as to see the properties therein. 66 00:05:47,310 --> 00:05:55,250 And then when I say dot you'll notice that I have my table being made references of car rental records 67 00:05:55,260 --> 00:06:02,760 if I go back to my database and look car into the red card was the name of the table but then it has 68 00:06:02,760 --> 00:06:04,030 many records. 69 00:06:04,440 --> 00:06:10,260 So that's one of the things the month the model does by default do try some plural lies the table names 70 00:06:10,290 --> 00:06:16,560 because I mean I call it it's a table name car into a record but it's really a car into our records 71 00:06:16,740 --> 00:06:17,350 system. 72 00:06:17,370 --> 00:06:21,660 So I really should have called a car into our records but that's beside the point. 73 00:06:21,660 --> 00:06:26,700 The fact is that using my dot operates I can access this table. 74 00:06:26,700 --> 00:06:28,680 So that's on the table I want. 75 00:06:28,690 --> 00:06:32,710 Do I want type types of cars. 76 00:06:32,710 --> 00:06:33,450 All right. 77 00:06:33,480 --> 00:06:43,490 And then I want this in the form of at least someone to see not to list open and close parentheses. 78 00:06:43,590 --> 00:06:50,140 And so what this is doing is using a language or mechanism we call a link. 79 00:06:50,220 --> 00:06:57,300 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 80 00:06:57,300 --> 00:07:00,600 database using need to see sharp code. 81 00:07:00,600 --> 00:07:09,230 So once again I would of basically established an instance of a connection to my database through the 82 00:07:09,230 --> 00:07:11,690 declaration of this property. 83 00:07:11,690 --> 00:07:17,390 I initialized it in the constructor so that's all constructors are for the initialized. 84 00:07:17,390 --> 00:07:23,840 Things are initialize see initialize component there will initializing my database to a new instance 85 00:07:24,260 --> 00:07:33,020 and then when I want to call in the database I use that object that was previously declared and initialized. 86 00:07:33,110 --> 00:07:39,340 I use my dot operator and then I can access my table and then basically that's it. 87 00:07:39,350 --> 00:07:42,800 So if you're used to that literally that's it. 88 00:07:42,860 --> 00:07:44,670 And I'm just converting into all of these. 89 00:07:44,680 --> 00:07:50,970 So on a specific date that I have called a C sharp at least and that is why I have the list. 90 00:07:51,020 --> 00:08:00,020 So if you use the database development this is the same thing as seeing select star from types of cars. 91 00:08:00,020 --> 00:08:01,480 But then this is askew. 92 00:08:01,550 --> 00:08:08,270 This is not native C sharp so to be able to write this kind of obscure would've taken far more lines 93 00:08:08,660 --> 00:08:15,410 and you know I would love to initialize a data reader than a data table to call it the objects and a 94 00:08:15,410 --> 00:08:22,150 bunch of things but link makes it so easy because you just call the database model. 95 00:08:22,280 --> 00:08:27,750 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 96 00:08:27,770 --> 00:08:33,110 to list because that's a more universal data type and it's easier to maneuver with. 97 00:08:33,440 --> 00:08:39,920 So now that we have the list of cars stored instead of for variable cars we need to put it to use so 98 00:08:40,250 --> 00:08:46,850 what I want to do once again is when the farm loads which is going to fire this event we want to get 99 00:08:46,910 --> 00:08:52,170 the list of cars from the database and then have the combo box display them. 100 00:08:52,220 --> 00:08:52,870 All right. 101 00:08:53,000 --> 00:09:00,830 So the next step is to say call our combo box at a CB type of car right. 102 00:09:00,950 --> 00:09:03,740 And then we're going to see display members. 103 00:09:03,740 --> 00:09:05,780 So there are two properties that we're going to set. 104 00:09:05,780 --> 00:09:11,990 First we want to set the display member and that's who's going to be equal to name. 105 00:09:12,020 --> 00:09:15,560 So display members it takes that you will see it. 106 00:09:15,560 --> 00:09:22,240 So remember that when we had it set up the we setting the items and you would see that takes Tondo you 107 00:09:22,240 --> 00:09:27,890 would see that takes Toyota etc display a member is the takes that you see. 108 00:09:27,890 --> 00:09:34,010 And so if you look back at your database design you would notice that the idea is one two three four 109 00:09:34,020 --> 00:09:37,900 you don't want the person to be seeing one two three four that means nothing to them. 110 00:09:38,030 --> 00:09:44,050 But the takes that you want them to see is the name Honda Lada buggy Toyota. 111 00:09:44,060 --> 00:09:44,430 Right. 112 00:09:44,720 --> 00:09:53,300 So name is the display a member and the value that we're interested in would be the idea. 113 00:09:53,300 --> 00:09:55,650 So we have volume member. 114 00:09:56,300 --> 00:10:02,810 Display a member and you have value member because it's actually the value that we want to store not 115 00:10:02,810 --> 00:10:04,070 the display. 116 00:10:04,070 --> 00:10:12,650 Once again we have a car into our card that has a type of car idea of type int which means we only want 117 00:10:12,650 --> 00:10:18,410 to store the idea associated to the type of car we don't need to know the name again because that's 118 00:10:18,410 --> 00:10:22,520 why we created the table to just store the names and the I.D.. 119 00:10:22,580 --> 00:10:28,430 So once we make reference to that I.D. from the car into our record we can look back and see that oh 120 00:10:28,520 --> 00:10:35,300 if it was eighty three then we know it's buggy or the person selects buggy we want to store I.D. three 121 00:10:35,390 --> 00:10:36,600 on the red card. 122 00:10:36,740 --> 00:10:37,180 All right. 123 00:10:37,490 --> 00:10:43,790 So the volume member would be what you intend to really store and that's in the background. 124 00:10:43,790 --> 00:10:44,240 Right. 125 00:10:44,630 --> 00:10:53,660 And then after sitting those two I am going to see that the data source for the items to be put into 126 00:10:53,660 --> 00:11:02,510 the combo box so that CB type of card data source is going to be equal to cars. 127 00:11:02,510 --> 00:11:04,540 So let's go through this again. 128 00:11:04,700 --> 00:11:11,360 I'm querying the database for the list of cars and then I'm saying that I want my combo box whatever 129 00:11:11,360 --> 00:11:18,770 data source it gets lots of little bits of data gets does look for a display a member for a name set 130 00:11:18,770 --> 00:11:24,350 the volume member to be I.D. and then said the data source to be the cars. 131 00:11:24,350 --> 00:11:31,070 So and I get the list of cars they're all coming over with I.D. and name someone to display the name 132 00:11:31,290 --> 00:11:39,740 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 133 00:11:39,740 --> 00:11:44,490 the source for the list of items coming into this combo box. 134 00:11:44,810 --> 00:11:48,470 So let's start and see how that works. 135 00:11:48,770 --> 00:11:49,720 And voilĂ . 136 00:11:49,730 --> 00:11:59,060 So now you're seeing a list of items for our farm or for our drop though and sermon by cutting them 137 00:11:59,070 --> 00:11:59,760 out earlier. 138 00:11:59,780 --> 00:12:00,680 So loaded. 139 00:12:00,680 --> 00:12:06,950 There was nothing in there so you're seeing that data is actually in there and this data is going directly 140 00:12:06,950 --> 00:12:08,250 from the database. 141 00:12:08,330 --> 00:12:13,450 And to prove that I want to close that window go to the database. 142 00:12:13,470 --> 00:12:14,590 Add one more car. 143 00:12:14,620 --> 00:12:16,820 So we added one more car to our fleet. 144 00:12:17,090 --> 00:12:19,340 And this one is a Subaru. 145 00:12:19,340 --> 00:12:19,820 All right. 146 00:12:19,820 --> 00:12:22,720 So now we have five cars in our fleet. 147 00:12:22,940 --> 00:12:29,330 And I want to go back execute again and when we look in the list the expectation is that we're going 148 00:12:29,330 --> 00:12:32,150 to see the Subaru that was just added. 149 00:12:32,300 --> 00:12:35,600 So that's a zero first step towards connecting to your database. 150 00:12:35,630 --> 00:12:38,170 So let's just review what we did. 151 00:12:38,300 --> 00:12:44,960 We set up our farm load events because we realized that there's certain data coming from the database 152 00:12:44,990 --> 00:12:46,960 that needs to be presented or our farm. 153 00:12:47,060 --> 00:12:49,020 By the time the farm loads. 154 00:12:49,040 --> 00:12:57,620 So after initializing a connection to the database through the use of my object our reference to the 155 00:12:58,190 --> 00:12:59,780 entities models. 156 00:12:59,800 --> 00:13:02,000 So we have our entity model. 157 00:13:02,090 --> 00:13:06,680 I initialize an object of the type of that entity model. 158 00:13:06,740 --> 00:13:07,200 All right. 159 00:13:07,220 --> 00:13:13,640 So I declared it's up here initialize it here and then I realized that I needed data from that database 160 00:13:13,640 --> 00:13:15,050 before the form loaded. 161 00:13:15,050 --> 00:13:25,310 So I set up a farm load event which queried the table and basically just did the initial steps toward 162 00:13:25,310 --> 00:13:28,610 setting up the combo box so these steps are pretty much universal. 163 00:13:28,610 --> 00:13:29,270 This is not you. 164 00:13:29,480 --> 00:13:34,020 This is not unique to the fact that I'm using the entity model and this and that. 165 00:13:34,100 --> 00:13:41,030 This is how you set up a combo box to inherit from my list of items whether it's a static list or a 166 00:13:41,030 --> 00:13:47,270 list going from the database where they're using link or you're using the other methods that are there. 167 00:13:47,270 --> 00:13:54,920 The fact is that this is all you data by and are you buying the objects to your controller or to your 168 00:13:54,920 --> 00:13:55,920 combo box rather. 169 00:13:56,040 --> 00:13:56,510 All right. 170 00:13:56,900 --> 00:14:03,050 So once again we run our query got to stuff cars from our database. 171 00:14:03,050 --> 00:14:09,410 Literally if you read from right to left that's left that's all it is saying give me a list of the types 172 00:14:09,410 --> 00:14:17,930 of cars from the database and store it in this variable and then later on we say that we want to display 173 00:14:18,020 --> 00:14:27,530 the name and store the idea and then the data source should be from the list of the cars that we just 174 00:14:27,530 --> 00:14:29,330 retrieved from the database. 175 00:14:29,330 --> 00:14:36,440 And so in our form loads we see that it is running that query and getting all that data if you want 176 00:14:36,440 --> 00:14:42,640 to put our breakpoint here and restart application and just go step by step to see exactly what happens 177 00:14:42,650 --> 00:14:43,660 just to be sure. 178 00:14:43,910 --> 00:14:50,150 Then you can feel free to do so but that is our first step to connecting to the database. 179 00:14:50,300 --> 00:14:59,230 In the next video we will be looking at how when we click submit no all we do when we click submit is 180 00:14:59,970 --> 00:15:01,940 you know display the information. 181 00:15:01,950 --> 00:15:08,410 Next time we want to actually save it in our car or into the record table. 182 00:15:08,440 --> 00:15:10,870 So that's all we'd be doing in the next video.