1 00:00:00,390 --> 00:00:01,300 ‫Welcome back. 2 00:00:01,320 --> 00:00:08,430 ‫Now that you know how to create a zoo table and associated animals table, it's time for you to create 3 00:00:08,430 --> 00:00:12,600 ‫this table here, which is our animals table. 4 00:00:12,600 --> 00:00:17,610 ‫So it's going to be all the animals that there are in our database that should be displayed here. 5 00:00:17,610 --> 00:00:24,420 ‫So we have an animal table, and I want you to display all of its data in this list box here. 6 00:00:24,420 --> 00:00:26,220 ‫So please go ahead and try that. 7 00:00:26,220 --> 00:00:27,570 ‫It shouldn't be too difficult. 8 00:00:27,570 --> 00:00:33,030 ‫You can pretty much apply what you have seen in the last two videos, so please go ahead and try that. 9 00:00:34,960 --> 00:00:35,440 ‫All right. 10 00:00:35,440 --> 00:00:36,550 ‫I hope you did. 11 00:00:36,730 --> 00:00:41,530 ‫And we are not adding the animal to zoo and kind of stuff, all these buttons. 12 00:00:41,530 --> 00:00:48,880 ‫We're not going to add that now, but we're just going to add this little additional list box there. 13 00:00:49,060 --> 00:00:53,290 ‫And I'm just going to do it like that, actually. 14 00:00:54,190 --> 00:01:03,250 ‫Just gonna copy this list box here, copy paste, drag it over so that I have roughly the same distance 15 00:01:03,250 --> 00:01:04,210 ‫between the two. 16 00:01:04,210 --> 00:01:06,760 ‫And now I'm going to make it a little longer. 17 00:01:07,720 --> 00:01:08,920 ‫Maybe like that. 18 00:01:11,000 --> 00:01:12,910 ‫And later on, I'm going to add a button here. 19 00:01:12,920 --> 00:01:17,870 ‫As you can see, the well, the numbers don't fit perfectly, but that's something that we can apply 20 00:01:17,870 --> 00:01:18,410 ‫later on. 21 00:01:18,410 --> 00:01:24,230 ‫Once we have all the different elements in here, we can change the window size and those kind of things. 22 00:01:24,620 --> 00:01:25,070 ‫All right. 23 00:01:25,070 --> 00:01:29,960 ‫And I'm not going to add a label yet because I'm going to add a button later on, which is just going 24 00:01:29,960 --> 00:01:34,520 ‫to have the add button or add animal button and the delete animal button. 25 00:01:34,940 --> 00:01:35,360 ‫All right. 26 00:01:35,360 --> 00:01:38,140 ‫So now we have this list box. 27 00:01:38,150 --> 00:01:40,520 ‫I'm going to go to this method. 28 00:01:41,480 --> 00:01:50,270 ‫And let's go to our show Zoo Methods and well, we can pretty much just copy that or well, actually, 29 00:01:50,270 --> 00:01:51,560 ‫let's build it ourselves. 30 00:01:51,560 --> 00:01:51,930 ‫Right? 31 00:01:51,950 --> 00:01:59,450 ‫So let's create a new method for that and I'm going to go ahead and call that private void, show all 32 00:01:59,450 --> 00:02:00,320 ‫animals. 33 00:02:01,190 --> 00:02:03,170 ‫And now, once again, what do we need? 34 00:02:03,170 --> 00:02:11,030 ‫First of all, we need a string, which is our query, where we just want to select all animals. 35 00:02:11,030 --> 00:02:15,770 ‫So select all from animal just like that. 36 00:02:16,340 --> 00:02:20,840 ‫And now in order to run the query, we need an SQL data adapter. 37 00:02:21,310 --> 00:02:27,740 ‫I'm going to call it a SQL data adapter, which is a new SQL data adapter. 38 00:02:28,190 --> 00:02:35,240 ‫And in there we need to hand over a query that we want to run, which we created here, and a connection 39 00:02:35,240 --> 00:02:38,180 ‫which we have set up here, this SQL connection. 40 00:02:38,180 --> 00:02:44,870 ‫And as you can see, we initialize it here in our main window, initialize or constructor, and I'm 41 00:02:44,870 --> 00:02:50,300 ‫going to add the query here, query and the SQL Connection. 42 00:02:52,640 --> 00:02:54,650 ‫And now we can use that adapter. 43 00:02:54,650 --> 00:03:01,670 ‫And the advantage of the adapter, as I said, is that you don't need to open and close the SQL connection 44 00:03:01,670 --> 00:03:04,370 ‫manually because we're going to do that later on. 45 00:03:04,370 --> 00:03:10,910 ‫You will see how to do that and you will now see the, let's say, nicer way of having an adapter. 46 00:03:10,910 --> 00:03:16,280 ‫But sometimes it's just useful to still have this SQL connection open and closed and do things manually 47 00:03:16,280 --> 00:03:16,910 ‫in there. 48 00:03:17,030 --> 00:03:28,100 ‫So I'm going to hand over the SQL data adapter and within those curly brackets, I want to display my 49 00:03:28,100 --> 00:03:30,650 ‫stuff onto the list that we have. 50 00:03:30,800 --> 00:03:36,800 ‫So in order to get that, we need, first of all, all the data from the table, which is going to be 51 00:03:36,800 --> 00:03:42,860 ‫a data table and I'm going to call it animal table, which is a new data table. 52 00:03:42,860 --> 00:03:44,600 ‫So I'm going to initialize it straight away. 53 00:03:44,930 --> 00:03:56,690 ‫And then we need to go ahead and fill our SQL adapter or data adapter with this animal table or actually 54 00:03:56,690 --> 00:03:57,440 ‫the other way around. 55 00:03:57,440 --> 00:04:04,550 ‫So this animal table gets our SQL data adapter information, which runs the query onto our database. 56 00:04:04,910 --> 00:04:07,400 ‫So that's what this field method will do. 57 00:04:08,150 --> 00:04:11,240 ‫And now that we have done that. 58 00:04:13,690 --> 00:04:18,070 ‫Actually here it's the small SE, not the Capital One. 59 00:04:18,550 --> 00:04:22,330 ‫Now we need to apply all of that to our list. 60 00:04:22,330 --> 00:04:29,560 ‫So we have a list, associated animals and we have a list with our zoos, but we do not have a list 61 00:04:29,560 --> 00:04:31,580 ‫with all animals yet. 62 00:04:31,600 --> 00:04:34,620 ‫Well, we do, but we have not given the name to it yet. 63 00:04:34,630 --> 00:04:39,910 ‫And as you can see, it's called List Associated Animals Copy, because I just copied and pasted it 64 00:04:39,910 --> 00:04:42,280 ‫and then created this for me automatically. 65 00:04:42,280 --> 00:04:45,610 ‫So I'm going to name it a list, all animals. 66 00:04:46,360 --> 00:04:48,340 ‫So it's the list of all animals. 67 00:04:49,810 --> 00:04:53,960 ‫And once that is done, I can go back to my CSS class. 68 00:04:53,960 --> 00:05:03,430 ‫So my code behind and in here I'm going to call my list, all animals list and I need to display the 69 00:05:03,430 --> 00:05:04,500 ‫member path. 70 00:05:04,510 --> 00:05:08,740 ‫So get subsets of path to value on the source object. 71 00:05:08,740 --> 00:05:18,880 ‫So I need to do that and that will be the name because I want to have the name of my animal right next. 72 00:05:18,880 --> 00:05:21,310 ‫I need to apply the selected value path. 73 00:05:21,310 --> 00:05:23,380 ‫So list all animals. 74 00:05:23,380 --> 00:05:29,140 ‫Selected Value Path is going to be the ID. 75 00:05:31,600 --> 00:05:36,700 ‫So pretty much the first value is 1/2 values too and so forth. 76 00:05:36,700 --> 00:05:46,030 ‫So all those IDs that needs to fill all of them and list all animals that item source. 77 00:05:46,030 --> 00:05:49,160 ‫So where do we get the information from? 78 00:05:49,180 --> 00:05:54,040 ‫Well, we'll get it from our animal table default view property. 79 00:05:54,580 --> 00:06:00,400 ‫So it gets a view of the table that may include a filtered view or a cursor position. 80 00:06:00,400 --> 00:06:01,840 ‫That's exactly what we need. 81 00:06:02,410 --> 00:06:07,180 ‫And this can go well, but it also can go wrong. 82 00:06:07,180 --> 00:06:10,150 ‫So we need to have a tri catch block around it. 83 00:06:10,150 --> 00:06:19,420 ‫So I'm just going to create a try catch block with double tap and instead of throwing the error, I'm 84 00:06:19,420 --> 00:06:21,640 ‫just going to write something on the message box. 85 00:06:21,640 --> 00:06:30,970 ‫So message box, dot show and this exception, I'm going to call it E and it should show e dot to string. 86 00:06:31,480 --> 00:06:36,970 ‫So I just want to show what the error was if there is an error showing all the animals because anything 87 00:06:36,970 --> 00:06:37,530 ‫could go wrong. 88 00:06:37,540 --> 00:06:43,600 ‫This statement here, the query could be wrong or the connection could be wrong or it couldn't work 89 00:06:43,600 --> 00:06:44,410 ‫for now. 90 00:06:44,440 --> 00:06:47,800 ‫Or the string that we have created here could be incorrect. 91 00:06:48,190 --> 00:06:51,400 ‫The display member path could be incorrect. 92 00:06:51,400 --> 00:06:58,810 ‫All of those things could go wrong because we are using hardcoded string names here and that can turn 93 00:06:58,810 --> 00:07:00,250 ‫into plenty of problems. 94 00:07:00,250 --> 00:07:03,010 ‫And yeah, that's why we need this try and catch. 95 00:07:03,010 --> 00:07:08,290 ‫BLOCK Now that we have that, we can go ahead and show all animals from the start. 96 00:07:08,290 --> 00:07:15,820 ‫So let's just call the show All Animals method here in our constructor Main Window and I'm going to 97 00:07:15,820 --> 00:07:20,980 ‫run it again to actually see more beautiful animals in my list. 98 00:07:20,980 --> 00:07:25,720 ‫So there we are, shark, clownfish, monkey, wolf, gecko, crocodile and so forth. 99 00:07:25,720 --> 00:07:27,670 ‫I hope you manage to do that yourself. 100 00:07:27,670 --> 00:07:32,710 ‫Now you can go ahead and add some more data just to see that this is actually updating. 101 00:07:32,980 --> 00:07:38,680 ‫And in order to do that, you can, of course, do it the old fashioned way by, well, changing the 102 00:07:38,680 --> 00:07:42,580 ‫data straight away in the database here and the data connection. 103 00:07:43,090 --> 00:07:49,120 ‫So yeah, first you need to refresh and connect to it and then you can apply. 104 00:07:50,450 --> 00:07:52,300 ‫It changes to the tables. 105 00:07:52,310 --> 00:07:56,390 ‫So in here, animal show table data. 106 00:07:57,050 --> 00:07:57,440 ‫All right. 107 00:07:57,440 --> 00:07:59,690 ‫So that was it for this video. 108 00:07:59,690 --> 00:08:05,600 ‫We are going to add the buttons in the next video and I'm going to show you how to add things and how 109 00:08:05,600 --> 00:08:08,960 ‫to use all the functionality that is still missing. 110 00:08:08,960 --> 00:08:10,910 ‫So see you in the next video.