المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Showing data from a database to a listbox during runtime



C# Programming
02-01-2010, 01:21 AM
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?