简体   繁体   中英

No errors in code, but data retrieved from database doesn't show up in listbox

I'm trying to retrieve data from database and display it in a listbox. I've got the following code and when I run it, it gives no error or something but no data is showing up in the listbox.

connection.Open();

DataTable dt = new DataTable();

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select * from Appointments where PersonID = '" + textBox4.Text + "'";

OleDbDataReader reader = command.ExecuteReader();

dt.Load(reader);

foreach (DataRow Dr in dt.Rows)
{
    listBox1.Items.Add(Dr["PersonID"].ToString());
}

connection.Close();

You don't show your connection string, but it sounds like one of the old gotchas when working with file based databases (Access as it seems you're using) from within Visual Studio.

If your MDB file is part of the project, and its "Action" is set to "Copy always", then every time you run your application, the MDB file in the BIN folder will get overwritten by the one in your source folder, thus overwriting any changes you made in the last run.

Please verify that this is not the case as it's one common source of problem.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM