Showing data from a database to a listbox during runtime
-
Hi. I'm having problems in showing all the data to a listbox. all of the data will be coming from a database and will be shown depending on the choices of the user. i tried to code it but it didn't work. here's my code:
public frmMainForm()
{
InitializeComponent();
string sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Schedule.mdb";using (OleDbConnection dbConnect = new OleDbConnection(sConnection)) { dbConnect.Open(); if (prog == "CS") { string sqlString = "SELECT cs\_firstcourse, cs\_secondcourse, cs\_thirdcourse, cs\_fourthcourse, cs\_fifthcourse, cs\_sixthcourse, cs\_seventhcourse, cs\_eighthcourse, cs\_ninthcourse FROM CS WHERE cs\_year = @chosenYear AND cs\_term = @chosenTerm;"; OleDbCommand dbCmd = new OleDbCommand(); dbCmd.CommandText = sqlString; dbCmd.Connection = dbConnect; dbCmd.Parameters.AddWithValue("chosenYear", chosenYear); dbCmd.Parameters.AddWithValue("chosenTerm", choseTerm); using (OleDbDataReader dbReader = dbCmd.ExecuteReader()) { if (dbReader.Read() == true) { for (int a = 0; a < 9; a++) { lstBoxCourses.Items.Add(dbReader\[a\].ToString()); } } else { MessageBox.Show("An error has occured while trying to retrieve data from the database", "Class Scheduling: Database Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } dbReader.Close(); } } dbConnect.Close(); } }
can someone help me?
When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
OriginalGriff wrote:
Your computer burst into flames
Ouch, remind me never to run any of your code :laugh:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
OriginalGriff wrote:
Your computer burst into flames
Ouch, remind me never to run any of your code :laugh:
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)I've built hardware destroyers into embedded code before - if the software believes the hardware has been copied and the firmware pirated, it blows the PSU, and anything else it can get it's hands on. One of our asian distributors and a paraniod MD prompted that one... :laugh:
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
Hi. I'm having problems in showing all the data to a listbox. all of the data will be coming from a database and will be shown depending on the choices of the user. i tried to code it but it didn't work. here's my code:
public frmMainForm()
{
InitializeComponent();
string sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Schedule.mdb";using (OleDbConnection dbConnect = new OleDbConnection(sConnection)) { dbConnect.Open(); if (prog == "CS") { string sqlString = "SELECT cs\_firstcourse, cs\_secondcourse, cs\_thirdcourse, cs\_fourthcourse, cs\_fifthcourse, cs\_sixthcourse, cs\_seventhcourse, cs\_eighthcourse, cs\_ninthcourse FROM CS WHERE cs\_year = @chosenYear AND cs\_term = @chosenTerm;"; OleDbCommand dbCmd = new OleDbCommand(); dbCmd.CommandText = sqlString; dbCmd.Connection = dbConnect; dbCmd.Parameters.AddWithValue("chosenYear", chosenYear); dbCmd.Parameters.AddWithValue("chosenTerm", choseTerm); using (OleDbDataReader dbReader = dbCmd.ExecuteReader()) { if (dbReader.Read() == true) { for (int a = 0; a < 9; a++) { lstBoxCourses.Items.Add(dbReader\[a\].ToString()); } } else { MessageBox.Show("An error has occured while trying to retrieve data from the database", "Class Scheduling: Database Access Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } dbReader.Close(); } } dbConnect.Close(); } }
can someone help me?
i dont know but if you put break point and detecting the code.
-
I've built hardware destroyers into embedded code before - if the software believes the hardware has been copied and the firmware pirated, it blows the PSU, and anything else it can get it's hands on. One of our asian distributors and a paraniod MD prompted that one... :laugh:
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
I did something less severe several years ago. A company I worked for that I didn't completely trust used some software that I wrote in my own time that they relied on. I knew they wouldn't pay me for it (I was right) but it made my life so much easier whilst I was there so I permitted it's use making sure the T&Cs stated clearly that only I had the right to use the application at anytime and it was owned by me. I wrote a 'bomb' in it that corrupted the exe if I didn't log on for a certain period. All the data that was used however (the data is of course their property) was placed at this point in unencrypted text files so they could access it but could no longer process it using *my* software. After we inevitably parted company, the software went 'boom' atfter the expected ammount of time :-D I pointed them to the text files when they rang to ask WTF and told them to go **** themselves. One of the highlights of my life :laugh:
Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
When you say "It didn't work" what did happen? An Exception? Nothing on the display? Your computer burst into flames?
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
In that case, since it appears to be in your form constructor:
public frmMainForm()
{
...
}I assume that
if (prog == "CS")
is failing - presumably because "prog" is not "CS" yet. Where are you assigning that? Have you put a breakpoint at the if statement, and looked as what "prog" is?
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
In that case, since it appears to be in your form constructor:
public frmMainForm()
{
...
}I assume that
if (prog == "CS")
is failing - presumably because "prog" is not "CS" yet. Where are you assigning that? Have you put a breakpoint at the if statement, and looked as what "prog" is?
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
I declared the variable prog as global and used it to assign the value "CS" when the user has clicked the radio button. it is goes like this:
private void rdioProgCS_CheckedChanged(object sender, EventArgs e)
{
prog = "CS";
rdioProgCS.Enabled = false;
rdioProgIT.Enabled = false;
} -
I declared the variable prog as global and used it to assign the value "CS" when the user has clicked the radio button. it is goes like this:
private void rdioProgCS_CheckedChanged(object sender, EventArgs e)
{
prog = "CS";
rdioProgCS.Enabled = false;
rdioProgIT.Enabled = false;
}So since your code that tests it is in your constructor it will not be equal to "CS" until after the form is displayed... By that time, it is too late, the constructor is only run once per instance.
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
So since your code that tests it is in your constructor it will not be equal to "CS" until after the form is displayed... By that time, it is too late, the constructor is only run once per instance.
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
ok. i understand now. i'll just transfer the code to a method (which didn't occured to me a while ago). what i first thought is if i put the code inside the method of the listbox the database will be opened once i clicked the listbox and since i need the data to be in the listbox before i click the listbox it will be too late so i put it in the constructor which i think is the best place because once the method of the form has ran the database will be open until i close the form.