Welcome back.
So this is when the currency ID is greater.
Zero.
If the currency ID is not greater zero, we want to make sure that we are adding a new line.
So this is what happens here.
So this is where we're actually creating a new line and we're saying open a new connection.
And by the way, of course, first of all, we ask the user, does he really want to save it or not?
And if he says yes, then we execute this code.
So we say open a connection, establish a new command is equal command, and this is our SQL code once
again.
And we need to run it on a connection.
And we say insert into currency master the amount and currency name with the values of amount and currency
name.
So this here is the Or is a SQL command.
So if you don't know any SQL, I would recommend to check out SQL Basics.
But basically if you want to update something well, you use this update keyword in capital letters
and you run this line here.
This where clause, by the way, says at a specific position or where a specific condition is met.
And in this case, where the ID is the ID that we have used, and here we say we want to insert something
so we don't have a where clause.
But what we do is we insert something into something.
Where do we insert it to, to our currency master, what is currency master?
Well, it's our database here.
So the database was what is table?
To be more precise, the table is called Currency Master.
The database had a different name, but this is the name of the table.
So insert into this table, add the columns amount and the column currency.
Name the values of amount and currency name.
So you use the brackets here because we are entering multiple values.
If you wanted to enter or not multiple values.
But we are entering multiple columns into multiple columns.
So if you had three columns, for example, so let's say you had another column called what would it
be?
Currency name and country code.
Okay.
For example, then you would have something like a comma and the third column name that you want to
use.
But we only have two column names that are relevant for us.
That's amount and currency name.
So we assign values of amount and currency name again, and we use this add keyword again because we
are adding parameters later on again.
So you see we are adding a new parameter with the value at amount.
Where do we get the amount from?
Well from our text amount which is this text field and from our text currency name, which is this text
field, which is a text box to be precise.
That's the name at least that they decided to use in WPF or in XML.
Okay.
And then we execute that on query and we close the connection.
So we do the same approach as we did here with the minor change or difference that we're not updating
a value, but we're actually inserting into it.
And that is why we use this currency ID greater zero because we.
At position zero.
What we do is we basically just have this select, right?
So if we go to this select here, but at position one now, we would now insert something and then the
next time we want to save something again after we made changes to it, for example, by editing it,
well then we would go ahead and.
Update it.
Okay, then we will do the updating part.
But here we're actually adding something to the database and then I'm using Clear Master.
What is clear, Master?
Well, Clear Master is basically a function which allows us to clear everything.
So let me just add that here as well real quick.
It's not very complicated.
So this method is used to clear all the input which the user entered in the currency master tab.
Okay.
So we reset text amount to be empty text currency name to be empty.
We set the button, save content to save because later on we will see how we can change it to be called
update.
So basically we say that this button here should have the content of save again, because once we click
on this edit button, let me show you.
So I click on edit.
You can see now it says update.
It's not saying save, it's saying update, but we are putting it back to save.
And then we call this method get data.
So we still need to implement get data.
We set the currency ID back to zero because well, we are clearing everything.
We bind the currency.
So what was bind currency?
It's this method here where we get the data table.
So we basically assign the data table correctly and we display the data and then we set the focus on
text amount.
So we set the focus to this enter amount again.
And in case something goes wrong, we just show this message box saying error and the user can press
the okay button.
These are the best kind of errors.
All right, so now we need to get this get data method.
So what is this get data method?
Well, because the thing is, we're not really at this point, we're not really filling any of this
information in here or we're not displaying any of the information.
So in get data, which is a new function that we need to create or a new method, let me quickly add
that here we bind the data in the grid view data grid view, because if you look at it, this thing
here, this big thing here, this is our data grid view and I need to make sure that something is displayed
there.
And that's what we do with this get data function.
So in order to display anything in the data grid, we of course need to get the data.
And how do we get the data from a database?
Well, first of all, we establish a connection to it with our function icon, which we created.
Then we create a data table so that we can bring it into a format that C-sharp understands it.
Then we have this command, the SQL Command, which basically says Select star from Currency Master.
So what does it do actually?
Well, it selects everything.
Star means everything.
So select means give me everything or give me, in this case, everything from where?
From this table called Currency master.
Okay, so we have this table called currency Master.
And in that table we have all of the data.
And now I want to get all of the data with this one line of code.
And the command type is again text.
Then I need to establish a SQL data adapter.
I need to fill our DT and this is what this data adapter is even for.
Write an adapter.
As you might know, it converts from one to another, from one thing to another thing.
That's exactly what we do.
We convert whatever we get in SQL format.
We convert it into a data table, which is C-sharp format, which our program understands and which
we then can display.
Because basically what we're doing is we have the data in a SQL format.
We convert it into a data format, which C-sharp understands, and then we give it to Examle, which
understands how to display stuff.
Okay.
So we have a bunch of adapters in between there, even though we don't see all of them.
But the SQL adapter, that's the one that we need to do manually.
Okay.
And then we fill the data table with whatever we have from our SQL adapter and then we check is it not
empty or is it at least greater than zero?
If that's the case, then set the data grid view currency.
Item source to be the default view of it.
Otherwise make the item source null.
So make it empty and close the connection.
Okay, so what is this?
Dgv currency?
Well, it's this.
Data grid view this whole thing and we say if there is something to display, then set the item source.
So the data source, so to speak, to be whatever we have in our data table that we just created.
So this data table which we fill with data from the database via those four lines and then we check
is there data in this data table?
If there is data in the data table, then make sure that you display this data table in the data grid
view.
And if there is no data, then make sure to make the data grid view empty.
So say that the item source should be null, so empty.
And then close the connection.
Because as I said, if you open a connection, you need to make sure that you close the connection because
if you don't close it and you open up another one, then you might run into new problems.