Welcome back.
All right.
And now at this point, there's a jump.
We need to make sure that we can click that save button.
So I run the application.
It still works, which is great.
But if we look at it.
Of course, the currency master doesn't do anything.
So if we click save or cancel, nothing will happen because, well, we haven't implemented the functionality
yet.
So if you go to your main window examle, you will see that you have this stack panel with the buttons,
right?
So inside of your second tab, which is the tab item master.
Okay.
So inside of that let me.
Go here.
We can see we have all of these stack panels and text boxes and all of the good stuff.
And now this is where we have the buttons now.
Okay, so we have this button, click save and cancel.
So let's go to button, save, click and go to the definition and you will see that you have this button
here.
What functionality which will be executed once we click on the button.
But as you see, there is nothing happening yet.
And now I'm going to use a try and catch block because I'm working with databases and when I'm working
with databases, something could go wrong.
Whatever it is, the connection could break or whatever.
It's it's weird.
Okay?
And to make sure that we don't run into a colossal break of our application, I'm going to use a try
and catch Block And for regarding the exception, I'm just going to catch a generic exception here.
So I'm just going to say Exception X should be executed and I'm just going to run a little message box
here saying what kind of error appeared?
So message box, show error, message box gives us the button.
Okay.
And the box image should be error.
So now if we click on Save and something goes wrong, then this little message box will appear, which
is just a little pop up box.
All right, so what is it that we want to try when we click on that save button?
Well, there is a bunch of stuff happening, so let me first of all, check if the text amount is not
empty.
So because if we look at it here, the entered amount for both of them should not be empty.
So that's what I'm checking here.
So if text amount is not null and is not empty.
Then we can move on.
But if it is empty, we will say, please enter a mount and we'll say okay and message box information.
That will be the message box image.
Okay.
And we will focus on text amount.
So on this window here, let me actually run this so you can see that in action.
Let me go to the master.
Click save.
You can see this is the message box that appears.
This is the information image.
So if we look at it, information is the text that appears at the top of this box.
So this is information you can see.
Please enter.
Mount is the first entry that you give this string here.
The message button is this okay button, which is this message button, okay.
That I have here.
And then the message box information is this icon that we have here.
Okay?
So I have these two if statements which check that we enter values or didn't we?
And if we didn't, we need to enter something.
So first we check if the text amount is empty.
If it's not, then we check.
Is the currency amount empty?
If it's not, that's when we do the logic of saving the data.
Okay, so this is the else block that we are going to check.
All right.
So let me.
Put a bunch of code in here and let me actually stop the application running.
Otherwise it's a little confusing.
Okay, so what do we do here?
First of all, we check is the currency ID greater than zero?
Because if it's zero, well, then we are trying to edit this.
Select here.
Okay.
Because we don't want to edit select, we only want to edit anything else that is in there.
So that's what we check is currency ID greater zero.
And then we just say, Are you sure you want to update?
Yes.
No.
So the user has two options.
The message box image is a question and we take the message box result.
So we see what has the user clicked.
So you can see this whole thing here, this whole message box execution is inside of an if block.
So what we say is if.
The user clicked on.
Yes.
So the window pops up and he can select yes or no.
If he says no, then this if statement will not be executed and we jump out of this whole iteration.
Okay.
So we basically jump out of any of the safe functionality so we will not save anything.
But if it is true.
So if the user clicked on, yes, that's when we are going to try to actually make changes to our database.
So first of all, we need to establish a connection to our SQLite database and that is this Maicon,
which is the connection that we establish.
So it's this method here where we get the connection via the connection string.
If you recall, we open a connection to the SQLite database and then we start with the magic and that
is here.
Okay, Then we create a data table because, well, we need an object to put the data in.
Then we run a command.
So we say that the CMD, the SQL Command should be a new SQL Command.
So this is another SQL code, this whole line here, this part here, this is SQL code.
All right.
And let me put that into another line here so it's a little more readable.
So this is SQL code and what it says is his Please update the currency master table.
What is the currency master table?
Well, it's this table here that we have.
So please update that and set the amount to be at amount and set the currency to be at currency name
where the ID is at ID.
So what is all of this stuff?
Where do we get that from?
Well, that's what comes up in the next few lines.
But basically this is the line to update something in the database and we are updating.
The row, which has the specific ID and what are we updating?
Well, the different columns.
So the column amount and currency name and we do that with the connection.
So this is the SQL Command line.
So what kind of command type is it?
It's text.
So this is text, right?
This is a string that we're using here.
So we are using the command type text.
Okay, then.
What we say is we add values to it.
So we say we need to add parameters to this command because we are using parameters here.
This is a parameter that we use in this command and we need to give it a value.
So we say add width value and we say add ID should be our currency ID, So what is our currency ID?
Well, if you look at it here at the very top, we created this variable called private int currency
ID, which is the ID of the currency inside of our database.
Okay.
Then we want to update the amount as you see here with add amount.
Where do we get the amount from?
From our text amount dot text.
What is text amount?
Well, you can press control and click on text amount and you will see that it's this.
Text box here.
This one here with the name text amount.
So basically it's this one here.
This is the text amount.
So whatever we have entered in here should now be our amount.
So this add amount here and then we have text currency name.
You can again keep control clicked click on it or held and then you can see this is the text box that
is called text currency name.
Okay, so now.
We are updating it with whatever we have entered into those two boxes.
And where are we updating it?
In the table?
Well, wherever we currently are.
So whatever the current currency ID is.
Okay.
And then we execute this non query.
So execute a transact-sql statement against the connection and returns the number of rows affected.
So you could.
Get the value.
So this will return an integer and it will say how many rows were affected.
But we're not really interested in that.
We just want to make sure that the execution is really done.
And then we say connection close.
So each time that you open a connection after you finished the execution, with that connection, you
need to close the connection again.
And that's what we do.
So here we open the connection with my because this method opens the connection and here we close the
connection once again just to make sure that there are no leakages and we don't run into new errors
that could appear.
And once all of that is done, So once it did, all of the execution and all of that and by the way,
this is the part where it could run into an error.
That's why we are using a try and catch block.
Then we display a message box saying data updated successfully and we inform the user.
We have this okay button.
And the image that we're using is the information image.
Okay?
So that is our code that we want to execute there.
Once we click on the save button.