❔ Filling a combo box in a class that is from a form

So I have a project that requires me to use databases, I chose to use access database since I am familiar with it. But i am trying to put in some combo boxes with the data from the database. When I did that it worked, but my teacher asked me to put it in the same class that I read my data from. Is this possible?
29 Replies
AdmiraalSamba
AdmiraalSambaOP2y ago
so this is the code I used to get the data from the database into the combo box and my teacher asked to do it in clsData (the class i am using to read data from the database to then use for things like my login page)
Omnissiah
Omnissiah2y ago
that would be a good idea one more layer could be even a better idea
AdmiraalSamba
AdmiraalSambaOP2y ago
what do you mean with one more layer?
Omnissiah
Omnissiah2y ago
just start with ui and repository, then we'll see if other improvements are required
AdmiraalSamba
AdmiraalSambaOP2y ago
being honest i don't really understand what you mean. Do you mean like improving the ui?
Omnissiah
Omnissiah2y ago
so, ui would be you form and controls repository would be the class that reads and writes data from the table this makes sense?
AdmiraalSamba
AdmiraalSambaOP2y ago
yes, make the form and controls then the class and then improve them both
Omnissiah
Omnissiah2y ago
you will also need another class for the data returned by the repository you could think of making some folders in your solution
AdmiraalSamba
AdmiraalSambaOP2y ago
Being honest never made a folder before in my solution
Omnissiah
Omnissiah2y ago
if you have like 1 or 2 files who cares, but when you start having a bunch of files that works on different stuff one from another you could start thinking about it
AdmiraalSamba
AdmiraalSambaOP2y ago
oh oke 👍 So i made a folder with all my classes, but i dont know if i could do it better. So i have a class that does the calculations of how much insuline the user must use (program is for people with diabetes) and then i have a class for getting the things out of the database and then i have a class that contains all the info so it can be used globally So all of my forms are connected to the clsData (database class) and one of them is connected to clsBerekeningen (aka the class that does the calculations) and i also wanted to add a query that updates the product from the access database and the code does not work
public void EditProduct(clsGegevens geg)
{

string queryString = "UPDATE tblProduct SET " +
geg.Productnaam + " = @productNaam, " +
geg.Bereiding + " = @bereiding, " +
geg.Eenheid + " = @eenheid, "+
geg.AantalKoolhydraten + " = @aantalKoolhydraten, "+
geg.AantalCalorieen + " = @aantalCalorieen, "+
geg.Favorieten + " = @favoriet" +
"WHERE" + geg.Productnaam + " = @productNaam";

using (OleDbConnection con = new OleDbConnection(_strConnect))
{
con.Open();
using (OleDbCommand command = new OleDbCommand(queryString, con))
{
command.Parameters.AddWithValue("@productNaam", geg.Productnaam);
command.Parameters.AddWithValue("@bereiding", geg.Bereiding);
command.Parameters.AddWithValue("@eenheid", geg.Eenheid);
command.Parameters.AddWithValue("@aantalKoolhydraten", geg.AantalKoolhydraten);
command.Parameters.AddWithValue("@aantalCalorieen", geg.AantalCalorieen);
command.Parameters.AddWithValue("@favoriet", geg.Favorieten);

command.ExecuteNonQuery();
}
}
}
public void EditProduct(clsGegevens geg)
{

string queryString = "UPDATE tblProduct SET " +
geg.Productnaam + " = @productNaam, " +
geg.Bereiding + " = @bereiding, " +
geg.Eenheid + " = @eenheid, "+
geg.AantalKoolhydraten + " = @aantalKoolhydraten, "+
geg.AantalCalorieen + " = @aantalCalorieen, "+
geg.Favorieten + " = @favoriet" +
"WHERE" + geg.Productnaam + " = @productNaam";

using (OleDbConnection con = new OleDbConnection(_strConnect))
{
con.Open();
using (OleDbCommand command = new OleDbCommand(queryString, con))
{
command.Parameters.AddWithValue("@productNaam", geg.Productnaam);
command.Parameters.AddWithValue("@bereiding", geg.Bereiding);
command.Parameters.AddWithValue("@eenheid", geg.Eenheid);
command.Parameters.AddWithValue("@aantalKoolhydraten", geg.AantalKoolhydraten);
command.Parameters.AddWithValue("@aantalCalorieen", geg.AantalCalorieen);
command.Parameters.AddWithValue("@favoriet", geg.Favorieten);

command.ExecuteNonQuery();
}
}
}
that's the code i use to update the data geg. is the information class i use to get global variables
Omnissiah
Omnissiah2y ago
do you have a naming convention? if clsData deals with tblProduct, probably it could named ProductTable or ProductRepository same goes for clsBerekeningen, to know what it is at first sight, it could be for example InsulineCalculator or something does not work means that nothing happens? well for example this is wrong "WHERE" + geg.Productnaam + " = @productNaam"; first because there is no space after WHERE then because geg.Productnaam is the value, not the field name? and the same is for all other fields, you shouldn't be using geg into the query if you are passing it in the command.Parameters so it would be something like
string sql = @"UPDATE tblProduct SET
Productnaam = @productNaam,
Bereiding = @bereiding,
Eenheid = @eenheid,
AantalKoolhydraten = @aantalKoolhydraten,
AantalCalorieen = @aantalCalorieen,
Favorieten = @favoriet
WHERE Productnaam = @productNaam";
string sql = @"UPDATE tblProduct SET
Productnaam = @productNaam,
Bereiding = @bereiding,
Eenheid = @eenheid,
AantalKoolhydraten = @aantalKoolhydraten,
AantalCalorieen = @aantalCalorieen,
Favorieten = @favoriet
WHERE Productnaam = @productNaam";
i don't know the fields names but kind of like this also, is there not a ProductId, instead of searching for name?
AdmiraalSamba
AdmiraalSambaOP2y ago
Its both things so i got tblProducts and tblLogin both coming from clsDate clsData* There is
Omnissiah
Omnissiah2y ago
i bet they are used in entirely different places, one is for authentication and one is the products, so maybe they could be splitted in two diffrent classes
AdmiraalSamba
AdmiraalSambaOP2y ago
they are used in different places indeed, just in one form they both used (for the main form where i will have a tabcontrol with the products etc where they can select, edet products but also see their profile) the clsBerekeningen is used for multiple calculations and its a requirement to have a class this was my first thought on putting something in a class for the combo box?
Omnissiah
Omnissiah2y ago
yes but also in general, for using stuff that you get/put int a table by requirement you mean having one class with all the methods to calculate stuff in it?
AdmiraalSamba
AdmiraalSambaOP2y ago
sort of yes, so our main requirements where have a extern database for full points, intern would be -15 points, a text file would be -25 points no database is 0 points we needed atleast one class
Omnissiah
Omnissiah2y ago
so you can have more classes
AdmiraalSamba
AdmiraalSambaOP2y ago
Yes Just minimum 1 if we don't have any then 0 points on that
Omnissiah
Omnissiah2y ago
the practice is to organize code that works on the same stuff in a class or eventually same namespace
AdmiraalSamba
AdmiraalSambaOP2y ago
Oke 🙂 My teacher told me also that i needed to do some stuff on the naming labels and stuff like making code efficient
Omnissiah
Omnissiah2y ago
that would be a good thing to do the basis of it is essentially how do you explain your intentions in your code
AdmiraalSamba
AdmiraalSambaOP2y ago
So what it does?
Omnissiah
Omnissiah2y ago
what it does in the sense of what it does in the application
AdmiraalSamba
AdmiraalSambaOP2y ago
Ooo hey dont, i got a file with all the requierments of what the projects needs to have.
Omnissiah
Omnissiah2y ago
and what about it
AdmiraalSamba
AdmiraalSambaOP2y ago
If you want i can send them 🙂
Omnissiah
Omnissiah2y ago
wouldn't it be better if they were available here for everyone?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server