1 00:00:00,560 --> 00:00:08,740 Hey guys in this video we'll be building oh the functionality for saving our form data to the database 2 00:00:09,130 --> 00:00:17,320 so open to know what we've accomplished was that we built our database one we imported a data model 3 00:00:17,350 --> 00:00:24,880 into our projects to represent our database and then we looked at how we actually invoke our connection 4 00:00:24,880 --> 00:00:26,110 to the database. 5 00:00:26,110 --> 00:00:33,040 So in our previous lesson we would have initialized the objects that connect stored database. 6 00:00:33,090 --> 00:00:39,400 We we made a reference to it or we declared it and then we initialize the constructor and then don't 7 00:00:39,400 --> 00:00:42,040 hear enough form one load function. 8 00:00:42,130 --> 00:00:46,570 We would have run our first query using a link. 9 00:00:46,570 --> 00:00:52,540 And so I'm just showing you that this is the same as seeing so star from types of cars. 10 00:00:52,580 --> 00:00:54,810 That's the same thing as this line. 11 00:00:54,910 --> 00:01:00,210 And then we just feel the combo box with a list of cars in the database. 12 00:01:00,230 --> 00:01:06,250 No the expectation is that when we click submit and I'm just really a preview of the form when we click 13 00:01:06,250 --> 00:01:06,850 submits. 14 00:01:06,850 --> 00:01:13,570 The expectation is that we're going to be saving the data that was entered to our farm. 15 00:01:13,570 --> 00:01:13,970 All right. 16 00:01:14,290 --> 00:01:16,870 So I'm willing to modify the code a bit. 17 00:01:16,900 --> 00:01:23,350 So we have a button click event that we've been using to get the data from the farm. 18 00:01:23,350 --> 00:01:23,920 All right. 19 00:01:23,920 --> 00:01:30,360 And then do some validations and then if it's valid then we display a message. 20 00:01:30,370 --> 00:01:32,240 If not then we display. 21 00:01:32,650 --> 00:01:36,280 Well if it's valid that we display a of message. 22 00:01:36,280 --> 00:01:39,370 If it's not then we display error messages. 23 00:01:39,370 --> 00:01:40,230 All right. 24 00:01:40,300 --> 00:01:45,330 So what we're going to do is modify the is valid portion. 25 00:01:45,340 --> 00:01:52,360 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 26 00:01:52,390 --> 00:01:55,720 then clearly I don't want to display the message. 27 00:01:55,750 --> 00:02:04,980 So when we want to see data we need to declare an object of the type of relevancy of best I can explain 28 00:02:04,980 --> 00:02:05,330 this. 29 00:02:05,710 --> 00:02:07,760 So when I go back to my model. 30 00:02:07,800 --> 00:02:10,960 So I feel you know it kills the model by accident. 31 00:02:10,960 --> 00:02:17,530 That's fine you just go back to silos and explore and then you see the dot EDI x file second is a little 32 00:02:17,530 --> 00:02:20,520 booklet that and relaunch your model all right. 33 00:02:20,590 --> 00:02:24,310 No whatever the boxes name is. 34 00:02:24,310 --> 00:02:26,510 It's usually much as the name of the table. 35 00:02:26,510 --> 00:02:26,850 All right. 36 00:02:26,850 --> 00:02:29,940 So you see the box there is the car into the record. 37 00:02:30,010 --> 00:02:31,950 You see this one is types of car. 38 00:02:32,230 --> 00:02:39,280 And if I look in the solution explorer if I dropped on the E.D. and makes the file then you see that 39 00:02:39,280 --> 00:02:43,020 you have a dot see files per table. 40 00:02:43,030 --> 00:02:49,570 So you have car into a card and they have types of cars no types of cars sorry. 41 00:02:49,900 --> 00:02:55,510 So those files represent class files that represent your table. 42 00:02:55,510 --> 00:03:04,050 These are classes that are directly mapping every field in your tables so that when we did the car into 43 00:03:04,060 --> 00:03:10,360 record we know we set up an I.D. customer named data entered all of the fees that we put in the database. 44 00:03:10,360 --> 00:03:13,360 I right here in our class file. 45 00:03:13,360 --> 00:03:20,920 So the cool thing about this process I'm using link and Ada dot net is that I can create a new object 46 00:03:20,950 --> 00:03:26,490 of this class type and then I can see this object into the database. 47 00:03:26,680 --> 00:03:28,710 So let's go through this step by step. 48 00:03:28,750 --> 00:03:35,500 The first thing I want to do is in the block that says is valid I want to declare a new object that 49 00:03:35,500 --> 00:03:46,320 I want to say var or in tile or a card is equal to a new instance off car or intolerant card. 50 00:03:46,330 --> 00:03:46,810 Right. 51 00:03:47,260 --> 00:03:55,870 So once again this class directly matches the car into a card table that we created. 52 00:03:55,870 --> 00:04:03,550 Here are all the fees that are available to us and they directly much the columns that are in our database 53 00:04:03,850 --> 00:04:12,560 which means that I need to fill these fields with the data that I intend to store inside of the database 54 00:04:12,730 --> 00:04:13,620 fields. 55 00:04:13,660 --> 00:04:14,020 All right. 56 00:04:14,440 --> 00:04:19,720 So while all of them with the exception of I.D. because we created I.D. to be auto increments so I don't 57 00:04:19,720 --> 00:04:25,420 need to provide a value for I.T. but customer name is going to be entered in on the form. 58 00:04:25,450 --> 00:04:27,160 They will go to the design course. 59 00:04:27,190 --> 00:04:32,950 My name is going to be entered into the form in the code behind we're already collecting the customer 60 00:04:32,950 --> 00:04:35,970 name I experience in a variable customer name. 61 00:04:36,010 --> 00:04:43,540 And so what I'm going to do is try and mop whatever value is being stored in this variable Muppet into 62 00:04:43,750 --> 00:04:48,370 my new rental record of type Car Rental record. 63 00:04:48,370 --> 00:04:49,090 All right. 64 00:04:49,180 --> 00:04:53,170 So not that type of object that represents my table. 65 00:04:53,170 --> 00:05:02,560 I want to say that the rental record Dot's customer name field is so the name field in my table should 66 00:05:02,560 --> 00:05:12,710 know have the value of the customer name value that I just collected from my farm. 67 00:05:12,850 --> 00:05:15,190 All right so once again this is the table. 68 00:05:15,190 --> 00:05:19,740 This is the textbook sorry store in customer name in the wind farms. 69 00:05:19,750 --> 00:05:25,570 I'm collecting the text and I'm storing it in my local variable called customer name. 70 00:05:25,600 --> 00:05:31,690 No no that's I've collected it locally I want to push it to the database and want to declare an object 71 00:05:31,720 --> 00:05:39,270 off the table that I intend to fill with the data and then I'm going to say that this objects field 72 00:05:39,310 --> 00:05:44,060 called customer name which directly maps to the database field. 73 00:05:44,170 --> 00:05:49,890 I wanted to have the value coming from my wind farms application right. 74 00:05:50,050 --> 00:05:53,180 And then I want to repeat that process for every other one. 75 00:05:53,190 --> 00:06:01,380 So a rental record dot data printed is going to be equal to and which field what do I house during data 76 00:06:01,390 --> 00:06:03,520 oriented data rented. 77 00:06:03,610 --> 00:06:07,040 Value is being stored in my variable data out. 78 00:06:07,060 --> 00:06:13,660 So I kind of deliberately named them kind of sparsely so you could see the difference and not get confused. 79 00:06:14,050 --> 00:06:20,920 So data oriented is going to have the value that is stored inside of data out all right in the same 80 00:06:20,920 --> 00:06:21,360 way. 81 00:06:21,400 --> 00:06:27,970 And it is that control D to duplicate that line data returned is going to have the value that is being 82 00:06:27,970 --> 00:06:32,570 stored in it in all right. 83 00:06:32,780 --> 00:06:41,840 So you just below your your model so for every field that you intend to store data for in your database 84 00:06:42,080 --> 00:06:48,520 for a field that is not present in your object you just fill them all with a corresponding data for 85 00:06:48,620 --> 00:06:48,880 right. 86 00:06:48,890 --> 00:06:58,340 So for Intel or card docs and then the next one will be cost that is equal to my variable cost. 87 00:06:58,340 --> 00:07:02,870 And then the final one is the car data. 88 00:07:02,900 --> 00:07:09,590 So no I have added the error here with my cost some storing cost does double. 89 00:07:09,590 --> 00:07:17,730 But then the class type is decimal because I declared it does this amount instead of my database. 90 00:07:17,730 --> 00:07:17,990 All right. 91 00:07:17,990 --> 00:07:22,220 So cost is decimal right then we just see. 92 00:07:22,220 --> 00:07:29,540 Or I could have used float so I could change the data type here just to make it okay but it's probably 93 00:07:29,540 --> 00:07:31,960 easier to just cost it's here. 94 00:07:31,970 --> 00:07:38,730 So I can probably see something like decimal dot. 95 00:07:39,420 --> 00:07:45,240 And then I would just pass a decimal dot pass and the passing cost. 96 00:07:45,240 --> 00:07:45,660 All right. 97 00:07:45,660 --> 00:07:47,810 And that will take care of. 98 00:07:48,250 --> 00:07:49,020 Okay. 99 00:07:49,140 --> 00:07:55,440 Slight miscalculation there that did not take care of that because the pyres foams and is expecting 100 00:07:55,440 --> 00:07:57,980 a string and then passing in a bubble. 101 00:07:57,990 --> 00:08:01,140 So let's re gather our thoughts. 102 00:08:01,140 --> 00:08:06,210 I think maybe I can use an implicit cost here and just say this in. 103 00:08:06,240 --> 00:08:08,580 Let's see if that works. 104 00:08:08,580 --> 00:08:11,960 So sometimes you can actually do a costing like this. 105 00:08:12,020 --> 00:08:14,460 So we looked at two other ways to do cost. 106 00:08:14,460 --> 00:08:22,530 You can do data type dot pass or you can see converts dot two and we use one of those functions. 107 00:08:22,530 --> 00:08:24,740 Here's another way to do our costings. 108 00:08:24,750 --> 00:08:28,740 So you just use parenthesis the data type you want to cost. 109 00:08:29,010 --> 00:08:32,100 And then the value comes afterwards. 110 00:08:32,100 --> 00:08:32,580 Right. 111 00:08:32,580 --> 00:08:34,020 This works sometimes. 112 00:08:34,020 --> 00:08:38,910 So situationally you just find the one that works and you move on. 113 00:08:38,940 --> 00:08:40,090 Right. 114 00:08:40,110 --> 00:08:47,070 So now we have the cost and the final value that we need to go into our table which is coming from our 115 00:08:47,070 --> 00:08:51,580 wind farms would be the type of car I.D.. 116 00:08:51,660 --> 00:08:52,090 Right. 117 00:08:52,320 --> 00:08:58,670 So the type of car I.D. is an integer because it is the foreign key value that needs them up. 118 00:08:58,710 --> 00:09:06,450 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. 119 00:09:06,900 --> 00:09:14,130 So we already know that we're loading up the combo box with the name and the I.D.. 120 00:09:14,130 --> 00:09:18,470 But what we're collecting from the combo box is the text. 121 00:09:18,480 --> 00:09:27,030 So if I look at it I'm really collecting the text I need to collect the actual idea behind the combo 122 00:09:27,030 --> 00:09:35,730 box so to get the selected value literally the combo box is going to have a property called selected 123 00:09:35,730 --> 00:09:36,280 value. 124 00:09:36,300 --> 00:09:36,670 Right. 125 00:09:36,990 --> 00:09:45,090 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. 126 00:09:45,090 --> 00:09:48,270 So this is my combo box property. 127 00:09:48,350 --> 00:09:50,730 SARA This is my combo box control. 128 00:09:50,820 --> 00:09:51,300 Right. 129 00:09:51,690 --> 00:10:00,480 And then I say dot then I want to see that there is a property that is selected value so I can scroll 130 00:10:00,480 --> 00:10:01,380 through. 131 00:10:01,380 --> 00:10:05,730 If I'm not sure how to type it but selected value. 132 00:10:05,730 --> 00:10:06,710 All right. 133 00:10:06,750 --> 00:10:12,450 And then you would think that that sizable if you hover over a general is that this is just a generic 134 00:10:12,540 --> 00:10:13,790 Object type. 135 00:10:13,820 --> 00:10:22,140 We're trying to insert into an integer so we can use another implicit data type conversion there where 136 00:10:22,140 --> 00:10:23,850 we just say open bracket. 137 00:10:23,850 --> 00:10:30,510 Give me an entire presentation of whatever the selected value is and the selected value once again would 138 00:10:30,510 --> 00:10:37,620 be whatever value is mapped to the item that is selected based on the fact that we have value member 139 00:10:37,620 --> 00:10:39,630 being Heidi. 140 00:10:39,690 --> 00:10:40,630 All right. 141 00:10:40,710 --> 00:10:46,980 So what I'm going to do is create some breakpoint on that is valid and then I'm going to run. 142 00:10:46,980 --> 00:10:54,670 And then we're going to step through and just see how with our table object gets filled those. 143 00:10:54,700 --> 00:10:55,110 All right. 144 00:10:55,130 --> 00:11:00,920 So I feel that it's already customer name is this user costs this twenty five hundred. 145 00:11:00,920 --> 00:11:05,730 Let me change the dates so that we don't violate any of the validation. 146 00:11:05,790 --> 00:11:09,210 And I want to go with bogey someone to click submit. 147 00:11:09,270 --> 00:11:12,360 We hit our break point is valid is true. 148 00:11:12,360 --> 00:11:19,050 So we can just use if you live and to step through and then we see here one where declaring an object 149 00:11:19,080 --> 00:11:20,880 that represents our table. 150 00:11:20,910 --> 00:11:21,660 All right. 151 00:11:21,690 --> 00:11:28,350 So no you've declared object no we start filling it and I'm just going to step through don't the type 152 00:11:28,350 --> 00:11:35,750 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 153 00:11:35,760 --> 00:11:43,050 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 154 00:11:43,050 --> 00:11:48,750 on that line so if I press if you live in and it goes beyond that line then you can see that the type 155 00:11:48,750 --> 00:11:51,400 of car I.D. is three. 156 00:11:51,450 --> 00:11:51,890 All right. 157 00:11:51,900 --> 00:11:55,380 So that's is how you get the actual I.D.. 158 00:11:55,380 --> 00:12:00,290 That matches the value that was selected from the dropdown once again. 159 00:12:00,300 --> 00:12:06,180 That's important because we're actually going to be storing the I.D. instead of the actual name of the 160 00:12:06,180 --> 00:12:07,070 car. 161 00:12:07,080 --> 00:12:07,530 All right. 162 00:12:08,010 --> 00:12:14,100 And so I can just press F5 and we go on to see our success message. 163 00:12:14,100 --> 00:12:14,580 No. 164 00:12:14,580 --> 00:12:19,820 When I look in the database I am not one to see the data. 165 00:12:20,190 --> 00:12:21,810 So we just do it and show it. 166 00:12:21,840 --> 00:12:28,050 So when I select top 1 those under rules from this table I'm not willing to see the data in this table 167 00:12:28,320 --> 00:12:30,330 because well we didn't really see it. 168 00:12:30,360 --> 00:12:35,670 So we filled up the table but we didn't do anything with the data that we filled it up with. 169 00:12:35,670 --> 00:12:41,820 So I'm going to come out of debug mode and then we're going to finish up what we need to do. 170 00:12:42,360 --> 00:12:50,070 So now that I have filled the table object with all of the data needs I no need to pass it all to the 171 00:12:50,070 --> 00:12:52,800 database and then save. 172 00:12:52,800 --> 00:12:55,230 And that's really two steps. 173 00:12:55,230 --> 00:12:55,650 All right. 174 00:12:56,040 --> 00:13:04,500 So I'm just going to say something like And then just remind myself of the name of the database object. 175 00:13:04,500 --> 00:13:11,580 So Kyra into entities that represents my database someone's estate car rental entities dot and then 176 00:13:11,580 --> 00:13:19,670 the name of the table or collection of records from the table is Kyra until the records then I see data 177 00:13:19,680 --> 00:13:21,770 again known when to hide. 178 00:13:22,590 --> 00:13:29,030 And then what I'm adding is and it's expecting an object of car into the record. 179 00:13:29,160 --> 00:13:33,100 Guess what I have an object of car into the record. 180 00:13:33,210 --> 00:13:35,870 Type in the form offering dollar a card. 181 00:13:35,880 --> 00:13:36,210 Right. 182 00:13:36,240 --> 00:13:38,880 So I created the object her entire record. 183 00:13:38,880 --> 00:13:43,470 It is off type car into a card which matches the table. 184 00:13:43,470 --> 00:13:50,670 And so I am going to add this object that I just built just finished constructing and putting all the 185 00:13:50,670 --> 00:13:51,570 data in. 186 00:13:51,720 --> 00:13:56,820 I'm just adding it to the collection of records for that table. 187 00:13:56,820 --> 00:13:57,850 All right. 188 00:13:57,930 --> 00:14:01,860 And then I'm going to see a database. 189 00:14:01,860 --> 00:14:08,980 I want to save changes no one said do those two lines. 190 00:14:09,200 --> 00:14:12,620 And then I click Start again. 191 00:14:12,620 --> 00:14:19,940 I'm going to fill out the form make sure everything is valid and then click submit and then let's step 192 00:14:19,940 --> 00:14:22,300 through one more time so I'm just going to do. 193 00:14:22,310 --> 00:14:28,550 If you live through all of these lines and then that's filling or objects if I hover over the objects. 194 00:14:28,550 --> 00:14:33,250 Once again look at the data that I want to see that the really fun fields are filled. 195 00:14:33,250 --> 00:14:34,920 You see one with no. 196 00:14:35,030 --> 00:14:41,060 And later on we'll get to why that is no and why does it matter at the time we're creating the object 197 00:14:41,060 --> 00:14:42,560 or creating the record. 198 00:14:42,560 --> 00:14:45,980 It doesn't matter that this is mode and it does so much of that this is zero. 199 00:14:45,980 --> 00:14:50,540 Since I'm creating because it's going to auto increments anyway right. 200 00:14:50,600 --> 00:14:55,800 And then I went to F eleven so it now gets the passwords adding it to the collection. 201 00:14:55,890 --> 00:15:02,690 We F eleven and then we save changes and then it moves onto the next line so I'm just going to f five 202 00:15:03,260 --> 00:15:08,790 and then we see or or you know a success message and we click Okay. 203 00:15:08,930 --> 00:15:14,110 But then we see it we saw all of that before what we're looking for that's different though is the actual 204 00:15:14,170 --> 00:15:16,280 record being in our database. 205 00:15:16,310 --> 00:15:23,990 So if I go back over to school management studio and I rerun this query where I select the items from 206 00:15:23,990 --> 00:15:28,270 the table then I'm going to see my record in the database. 207 00:15:28,280 --> 00:15:34,730 And if you look you'll see that we have one as the I.D. because once again auto increment the customer 208 00:15:34,730 --> 00:15:41,570 name which was a poor item that's built in the word test and we have the dates rented the data returned 209 00:15:41,810 --> 00:15:45,770 the cost and the type of car I.D.. 210 00:15:45,980 --> 00:15:49,700 And once again this idea matches back to our record. 211 00:15:49,730 --> 00:15:57,470 So what kinds of I.D. seeks if there is no Sikhs and that's why binding the combo box like would be 212 00:15:57,470 --> 00:15:59,810 done at the former lord. 213 00:15:59,810 --> 00:16:05,840 That's why I binding it is very important part so as many times as you fill out this form and putting 214 00:16:05,840 --> 00:16:13,040 information and submits the expectation is that it will create a new record fill the record with the 215 00:16:13,040 --> 00:16:16,200 data and then save it. 216 00:16:16,370 --> 00:16:16,820 All right. 217 00:16:20,190 --> 00:16:22,720 In our next video we'll look at building. 218 00:16:23,030 --> 00:16:30,170 Although windows so that we can see you know a lot of types of cars that are in the database as well 219 00:16:30,170 --> 00:16:33,190 as the rental records in the database. 220 00:16:33,210 --> 00:16:38,750 We'll be designing our rights in the logic get we'll just be looking at extending the design of our 221 00:16:38,780 --> 00:16:45,290 application from just one form to maybe many forms that can show us the data that we want.