Welcome back.
In this video, we are going to add the rest of our user interface and we're going to add one functionality
for now, which is going to delete ZUS.
So let's go ahead and do that.
As you can see, this is how the interface should more or less look like and we're going to build it
almost the same.
Well, we could build it the same by typing in the same information here at the bottom, but this is
more about dragging and dropping it in so we can execute all the SQL code that we need or in general
the database code that we need in C sharp.
So first of all, let's open up the toolbox so we can see the stuff that we need.
And we're going to need buttons and I'm going to drag a button right in here, just like that with a
little distance.
And this button should say something like delete content.
Then I'm going to actually copy that button.
Copy paste.
I'm going to drag it next to it.
And this one was remove animal because we're not deleting it, but we're just removing it anymore.
And this one is not delete content, but delete zoo, by the way.
So we have delete zoo and we have remove animal.
Now copy and paste.
I'm going to put it right here.
Copy and paste again.
Actually, it's just create the buttons before we rename them.
And in here and then another one at the bottom here.
And the last one at the top right there.
So maybe we should drag everything a little further down.
Like that.
So there's enough space for the delete button and this one can go like that.
All right.
So now what we actually could do as well is drag those to little down as well so that the lists come
up the same height as the one on the right hand side.
Well it's och it's not the perfect, perfect user interface here, it's about other topics, as you
know.
So 500 is too much.
450 should be fine.
And now here, let's look at 600.
That's still too much.
Or 625.
So 600 should be a bit better, maybe 580 or something like that.
Okay, so now all the buttons are in there.
Our main window is looking good.
We could even change the name of our title.
Let's make it a zoo manager.
All right, we have the zoo list.
Delete zoo.
Now we can change all of those buttons.
Contents.
So that one was add zoo.
That one is update zoo.
Actually that one was not updated to that was an animal because they're not in the order from top to
bottom.
They just need to click in here.
And as you can see, it's selecting the correct one for me straight away.
So this one should be update zoo.
That button here is update animal.
That one is delete animal
animal.
And that one here is gonna be.
Add animal to zoo, head animal to zoo.
And now I think we are done.
Update Zoo actually this one update zoo that one should be changed.
Update Any more.
All right, so now all our buttons are fine.
We just need a text box here in the middle, and I'm going to reposition it a little bit like that,
make it wider, and just like that should be fine.
We could even change the font size of the text box so that it's a bit bigger.
But that should be fine.
As I said, not about perfectionism here or perfectionism in this UI.
Okay, so now that we have that, we can go ahead and add the delete zoo button.
So I'm going to double click on the button.
And I get this button click event and actually I don't like this name, so maybe this is not the best
one.
Let's go back and let's change it.
So we have this button and it received this event called Button Click and I'm going to call it Delete
Zoo
and maybe even something like Underscore Click.
This is going to be the new event handler that I'm going to create.
I'm going to copy that name and I'm going to implement the method myself.
It's a private void, delete, zoo, click.
And as you know, it needs an object which is the sender and it needs a rooted event.
ARGs
And here as well.
Now in here we can simply check first if the button works, message box or show delete zoo was clicked
or something like that.
Delete Zoo was clicked and now let's run it just to see if the button works correctly.
All right.
That we are.
So this is our interface.
All seems good already.
Now let's delete the zoo.
Delete zoo was clicked.
Perfect.
So the button works.
Now we can go ahead and implement it correctly in here.
What I'm going to do is I'm going to create a new query because I wanted to be well, I want it to delete
something.
So I'm going to use the delete keyword from which table, from the zoo table where the ID is equal to.
And now we are going to use something called zoo ID at zoo ID and that one will be our own variable
that we need to create.
So we need to create a new parameter kind of thing as we have seen with SQL parameters in here.
So we hit this SQL Command parameters, add with value, and I'm going to show you the other way of
handling this SQL stuff.
So without using this adapter.
All right.
So we're just going to use the following.
SQL Command is going to be a new SQL Command.
And and here, the SQL Command needs some information.
So first of all, it needs a query and it needs the SQL connection.
So nothing new here.
We've seen that up here already.
SQL Command Query and SQL Connection, and now we can go ahead and start this connection, SQL Connection
open.
So we're opening a connection to SQL and now we are adding the parameter to this SQL Command.
So parameters dot add with value.
And in here I used this at Zoom ID that I had here at the top.
So it has to be exactly the same as we have here.
And then I simply say what I want to delete and I'm going to say List Zeus Dot selected.
Value because selected value is not the name of the animal, because it's actually the ID and now we
can execute that code.
So SQL Command Dot Execute Scalar executes the query and returns the first column of the first row and
the result set returned by the query.
Additional columns or roles are ignored.
So what we need this for is, well, it's pretty much just the simplest way to execute this SQL Command,
which is going to execute the query onto our SQL connection.
And that's just not going to have overhead.
It's going to be super simple execution.
All right.
And now we, of course, need to close the SQL connection as well.
So SQL Connection that close, because as you can see, we need to open a connection.
We need to add the parameters, execute the code, and then we need to close the connection again because
otherwise we might run into errors.
Right now what I want to show is the Zeus.
And there is one thing actually to consider here, because we have plenty of zoos, we have plenty of
animals, and we even have this relationship table which has both of them combined.
So the animals that are in a specific zoo, but if I delete a zoo, I want to also apply a delete to
all the different zoo animal entries.
So.
Let's go to the data so it's a little more obvious.
Let's look at the zoo table and the zoo animal table.
So let's say I'm deleting New York, which has the idea of one.
There is no use of keeping zoo ID one in here.
So this zoo ID one and this zoo ID one entry.
So ID one and two, those entries are not required anymore because there is no animal in the zoo, because
the zoo doesn't exist anymore.
And that's why we can simply get rid of those two entries.
But we don't need to do it manually.
By applying it in code.
We can actually adjust our data table as well and adjusting it will pretty much do the job for us.
So open table definition.
So what?
I'm going to open up.
And what I simply need to add here at the end is on delete cascade.
Right.
And I do that for both pros here.
So for both constraints for our animal, for inky and our zoo for Inky.
So that will do is it will simply delete this entry pretty much that is there if the corresponding entry
is not available, more so the corresponding ID in the zoo that we are connected to.
So let's say if we delete a zoo, well then we don't need all the animals in that zoo anymore, not
the animals themselves, but this correlation or this relationship between the zoo and the animals.
So we can get rid of those as well.
And that's what these two entries here are.
This one on delete is Cascade allows us to do so now before we can execute the code, we need to update
just for you to see that this actually can cause errors.
So I'm going to simply execute the code now.
I'm going to start it and I'm going to try to delete something and you'll see the error that applies
here.
So I'm going to delete Kyra.
And it says the delete statement conflicted with the reference constraint.
Zoo f k the conflict incurred in the database table zoo and so column zoo ID, the statement has been
terminated.
So we have this error and well, if we search for the error, we find that this is because we are lacking
exactly that.
So on delete cascade and that's because we haven't updated it yet.
So now let's update the database with the new design here with our new updated table constraints.
And once that is done, update completed and successful, that's great.
We can start it again and we'll see that deleting Chiro works now.
And now we didn't get an error.
So why is Cairo still there?
And that has to do with us not casting or calling the method that shows this list or updates this list.
So let's go back to our code and it's actually here.
So we have this code and we need now to show all animals or actually show all zoos like that.
Show zoos.
Now let's start it again.
And you will see that the kyros was already gone.
So it's not there anymore.
As you can see, Cairo has gone with deleted it.
Now let's delete Berlin.
Boom and we get an SQL error.
But it disappeared.
So as you can see, there was an error because we deleted something and this error was applied by our
show zoos or associated animals somewhere here where we call the exception, right?
We had this catch exception and they showed us this error.
But now that's quite a step forward.
So now what I'm going to do is I'm going to update this code so that it doesn't well stop our program
if we run into an error.
So I'm going to build a try catch and finally.
BLOCK.
So we haven't used finally that often yet, but this time it really makes sense.
So what is the code that I want to execute in my try?
BLOCK Well, everything up to executing.
So all of this could go wrong and then.
If we want to close the connection here.
And finally, that's the right way, because if we open it up here, we need to close it at one point
and we should close it in finally, because even if it worked or it didn't work, we simply want to
close it and make sure that it will.
All is fine.
The same shows us so if it worked or it didn't work, doesn't matter.
We just call the show Zoo Method and that will well show the zoos that are in our table still.
And now, instead of just throwing an error, I'm going to use an exception name here.
I'm going to call it E and I'm going to show that onto my message box.
So a message box show and this one will be e dot to string like that.
All right.
Now, this one, we can call it X, maybe.
Maybe that's better.
So because problem here is that our root event is already called E, so we should call this one x OC.
Now let's start it again and see if we can delete components from our list.
And we don't have many left right.
So let's delete Tokyo.
We get this and var char for animal in a join expects the parameter zone was which was not supplied
and that's actually an error that doesn't help us at all.
So I'm going to get rid of this message box here because well, we do get an error here, but I'm just
going to ignore it for now because it's just it's still working, right?
Our program is still working and does what it should.
So it's just here that this code is still executed, show associated animals.
And in this case, we don't have anything in there.
Right.
So we have not selected something that should show the associated animals.
So I'm just going to get rid of this exception here.
And by the way.
Yeah.
Everything else should be fine.
All right, so.
Yeah, that's pretty much it.
Now you know how to delete components from your application, and in the next video, we're going to
add stuff to our list.
So see you there.