Reading users from Access into a list returns only one user
-
12-11-2019 - |
Question
I'm working on a WPF App in C# with VS 2010 SP1.
I've looked at quite a few examples on here and elsewhere around the web, and my code seems correct, but when I attempt to read rows from an Access DB into a class then into an ObservableCollection (I've also tried just a List), only one ends up in the collection. I then attempt to display a list of usernames in a ListBox, and of course only the one user in the collection appears.
I've been stuck on this for a while now. Any help would be greatly appreciated. I'm hoping it's something very trivial that I'm just overlooking.
User Class:
public class User
{
public User() { }
public int UserID { get; set; }
public string UserName { get; set; }
public string UserTitle { get; set; }
public string UserArea { get; set; }
}
Code:
// Setting up DB stuff.
string s_ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data " +
"Source=|DataDirectory|\\CloseoutApp.accdb";
OleDbConnection AccessConn = new OleDbConnection(s_ConnString);
string s_Query = "SELECT UserID, UserName, UserTitle, UserArea FROM Users " +
"ORDER BY UserID;";
OleDbCommand AccessCmd = new OleDbCommand(s_Query, AccessConn);
OleDbDataReader rdr;
AccessConn.Open();
rdr = AccessCmd.ExecuteReader();
// Collection of Users.
ObservableCollection<User> userList = new ObservableCollection<User>();
try
{
// Read each user from DB into a User instance, then add that instance to the userList.
while(rdr.Read())
{
User newUser = new User();
newUser.UserID = rdr.GetInt32(0);
newUser.UserName = rdr.GetString(1);
newUser.UserTitle = rdr.GetString(2);
newUser.UserArea = rdr.GetString(3);
userList.Add(newUser);
}
}
catch(Exception e)
{
//Update Statusbar
}
// Close the DB connection.
rdr.Close();
AccessConn.Close();
// Add users to the ListBox.
foreach(User u in userList)
{
lb_Users.Items.Add(u.UserName);
}
Solution
I had something similar happen to me when I was handling my database connection in a similar way.
Give the below code a try:
// Setting up DB stuff.
string s_ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data " +
"Source=|DataDirectory|\\CloseoutApp.accdb";
string s_Query = "SELECT UserID, UserName, UserTitle, UserArea FROM Users " +
"ORDER BY UserID;";
ObservableCollection<User> userList = new ObservableCollection<User>();
using (OleDbConnection AccessConn = new OleDbConnection(s_ConnString))
{
using (OleDbCommand AccessCmd = AccessConn.CreateCommand())
{
AccessCmd.CommandText = s_Query;
try
{
AccessConn.Open();
OleDbDataReader rdr = AccessCmd.ExecuteReader();
while (rdr.Read())
{
User newUser = new User();
newUser.UserID = rdr.GetInt32(0);
newUser.UserName = rdr.GetValue(1).ToString();
newUser.UserTitle = rdr.GetValue(2).ToString();
newUser.UserArea = rdr.GetValue(3).ToString();
userList.Add(newUser);
}
rdr.Close();
}
catch(Exception ex)
{
//do something with ex
}
}
}
// Add users to the ListBox.
foreach (User u in userList)
{
lb_Users.Items.Add(u.UserName);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow