Welcome back.
Let's go all the way to the top of our partial class main window, because this is where all of our
action is going to happen.
All of the magic is going to happen, and I'm going to create a bunch of objects here.
So I need to have a SQL connection.
Then I'll need to have a SQL Command and a SQL data adapter because I'm going to need them in order
to use with the Or to work with the database and to adjust values inside of that database.
Now, for this to work, I of course need to make sure that I have the right namespaces added.
So show potential fixes and I need to have using system data.
Well, SQL client.
It's this one here.
Once I add that you can see all of the classes are here.
So SQL Connection allows me to create a connection to my SQL database, which is this database here
which we created inside of this currency, MDF, this table.
So this is our database and this currency master is our table.
And in order to get access to it, I need to make a connection.
Now, in order to run SQL commands on it, I need this CMD, which stands for SQL Command.
Okay.
It's a SQL Command object.
And then I need the SQL data adapter in order to.
Basically communicate the data in the right format.
Okay.
DA stands for data adapter.
Okay.
So you could potentially give it better names than I have here.
Con cmd and DA are very short names that are used.
Maybe not the best practices here, but if you want to give it a more complex names, feel free to do
so.
Okay.
Con cmd and DA stands for SQL Connection Command and Data Adapter.
Okay.
Now we need to have more variables as we it more fields, so to speak.
So what private integer currency ID which I set to zero from amount which I set to zero and to amount
which I also set to zero.
So these are going to be fields that I will use throughout the class.
That's why I create them here at the top of my main window class so that I can use them in different
methods.
So there I think they are self-explanatory what they do.
And then.
The next thing that we need to do is we need to establish a function or a method which allows us to
connect to our SQL database.
So let me create this method for us.
So I create this public void, my con, my constants for my connection.
This is not me calling someone, it's just creating a connection.
So I create a string which I call Con.
And here, maybe this should be a well, I could give this a better name as well, but let's just leave
it at that Configuration Manager complaints because we need to add this using system configuration namespace.
Okay.
So I'm going to add that now the error disappears.
You can see that we now should have this system configuration here.
This namespace has been added.
All right?
And now what I'm doing is I'm using the configuration manager connection.
String.
Connection.
String.
The connection string.
So three times.
Connection.
String.
What the heck is going on here?
Well.
This is our database connection string.
So if we want to connect to a specific database and we could have multiple databases, right?
Well, if we want to connect to one, then we need to make sure that we know which connection string
we are using.
And in this case, let's go to our app.config.
And we gave this connection string to our database that we have here.
So the to this currency converter, we give it the name connection string.
So this name here is now exactly the same name as this name here.
Otherwise it wouldn't work.
Okay, so now we're connecting to that particular currency converter database and we do this with our
con.
So we now basically initialize this con variable, this SQL connection variable with that connection
string that we created.
And then we open the connection.
So this point, we open the connection to our SQL connection.
So each time we use Conn, we establish a connection to the database and.
We can then start running commands on it.
We can then start running SQL commands, such as putting data into the database, such as getting data
outside of the database or updating data.
Okay.
Or deleting data.
So it's always those.
Yeah, the crude executions.
So create read, update, delete crude.
Crude.
Okay.
If you ever heard that, now you know what they stand for.
So create, read, update, delete.
All right.
Now what we need to do is we need to get the data from the database and assign it to our.
You see our combo boxes.
So we have this bind currency method, but we will need to make quite some changes to it because before
that what we used is this data table that we manually filled with data.
That's what we did in the last video and we used that as the item source of our combo box.
But now we want to use a database to do the same thing so we can get rid of all of that code because
well, now we want to use the database instead.
And we now of course need to give it a data source.
So our or an item source, our combo boxes, they don't know what to do now because they have no data.
So let's give it some data by, first of all, establishing a connection to our SQL database, then
creating a new data table, then running a command.
Which says select ID currency name from currency master with the current connection.
So we are running a SQL command.
Now we create a new command.
We're not actually executing it yet, but we are saying create a new SQL Command, which is basically
saying create a new SQL execution line and we say select the ID and the currency name.
So select both.
I want to have the ID and the currency name from the table called Currency Master.
So where do we have this table called Currency Master?
It's this one here.
So if you gave it a different name, you would have to make changes to this particular line.
So that's the problem with SQL code.
It is very fragile because if there is a mistake in this particular code here or this string that we
have here, then it wouldn't work.
So you have to be very careful with this and on which.
SQL connection.
Do I want to run this?
I want to run it on my conn.
Okay.
On this SQL connection that we have opened up by running my conn.
Okay, so we initialized it and now we can use it.
So now we have this command and we need to define the command type.
So we want to use this command type text so you can check out.
There are different command types here.
You can see stored procedure, table, direct or text, and we're just going to use simple text.
We make it simple.
And now what we can do is we can use this DA, which is our SQL data adapter in order to actually establish
a connection and execute something.
Okay.
So basically I'm going to say.
Create a SQL adapter with that command that we prepared and assign it to our data adapter.
And now we can use this fill method and it adds or refreshes rows in a specific range in the data set
to match those in the data source using the data table name.
Okay.
That's what this fill method does.
So basically we're using the data table here and we fill the data table.
So this object that we created out of this data table, which is a C sharp object, it's not a data.
Um, yeah, it's not a database, but it's really just a table, but in a format that's C-sharp understands.
Well, okay, so we say, okay, this data table, it should be filled with the data that we get from
this command, the CMD command, which internally runs this command line here, which is connected to
this.
SQL Light connection.
So this is how everything is connected.
So this is basically always the setup that you have to do.
You have to establish a connection, you have to run a SQL command and then you can execute the or create
a SQL data adapter with that command and you can fill your data object, your data table object, C-sharp
object, so to speak, with that newly created data adapter.
And that's how everything is connected.
Okay.
And now at that point, we can get the individual rows.
Okay, so we get the first row and how do we get a row?
We get it from our DT, which is now filled.
So this data table was filled in this line and now we can create a new row out of that data table.
So we get the data from the data table and we can change the data in that data table as well.
So by creating a new row, I'm saying add a new row to that data table.
Okay?
And new row should have an ID, so I'm assigning an ID to it and I'm assigning.
Into the currency name column, I add select.
Okay.
So if we look at it again here, this is the select here.
So I'm saying it the first position there should always be select and then the other ones, they should
be manually created in the currency master.
Okay.
So I will now insert that row.
So here, basically I created a new row and now I add that to my data table, our data table where I
say rows, insert a new row and insert a new row at position zero.
So at your very first.
Entry, your first row, so to speak.
And now the next thing is we can check if that worked.
So we check is data table not null and is data table rows dot count greater than zero?
If that's the case now we can use our combo box from currency item source and define DT as the.
Itemssource.
So the default view, which is basically just a format that our combo box understands.
Okay.
Because combo boxes are limited in their brain capacity, so to speak.
We say we give you the default view, which gets a customized view of the table that may include a filtered
view or cursor position.
So basically we say use this select here.
Okay.
And if there is more data in it, well, even better.
But for now, this is going to be fine.
We just say add, select or display the select part in there.
And then.
For the combo box to currency.
We also say that the item source should be a default view.
So after that we can close the database or the connection to our SQL database.
It's not the database is closed, but the connection to it is closed.
And now we can get rid of this line here because we did it in there and we can get right rid of this
line here because we did it in here.
And yeah, that's basically it for now.
Even though maybe we should change the names here.
So combo from currency should be maybe currency name.
The same goes for this one here.
And it shouldn't be value, but it should be ID.
And ID.