I have user data (name, username, image) in a database and I need to list the name and id in a combo box. When an item in the combo box is selected it should display a picture. I have created a User and a UserDB class. In my User class I have the following code:
public class User
{
private String u_firstname;
private String u_lastname;
private String u_username = string.Empty;
private Byte [] u_image;
private Byte [] u_template;
public User(OleDbDataReader dr)
{
u_firstname = dr["USER_FIRST_NM"].ToString();
u_lastname = dr["USER_LAST_NM"].ToString();
u_username = dr["USER_NAME"].ToString();
u_image = (Byte [])dr["USER_IMAGE"];
u_template = (Byte [])dr["USER_TEMPLATE"];
}
}
In my UserDB class I have the following code:
public class UserDB
{
public static String workingDirectory = System.IO.Directory.GetCurrentDirectory();
public static String dbProvider = "Microsoft.ACE.OLEDB.12.0";
public static String dbName = "Users_DB.accdb";
public static String dbConnString = String.Format(@"Provider={0};Data Source={1}\Data\{2}", dbProvider, workingDirectory, dbName);
public User[] usersInDatabaseList()
{
User u;
List<User> clist = new List<User>();
OleDbConnection cn = new OleDbConnection(dbConnString);
String strSQL = "SELECT USER_FIRST_NM, USER_LAST_NM, USER_NAME, USER_IMAGE, USER_TEMPLATE FROM KF001_USERS";
OleDbCommand cm = new OleDbCommand(strSQL, cn);
OleDbDataReader dr;
cn.Open();
dr = cm.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
u = new User(dr);
clist.Add(u);
}
cn.Close();
return clist.ToArray();
}
}
and on my mainForm I have the following method that I am using for my combo-box:
public void FillDropDownList()
{
try
{
//int i;
UserDB db = new UserDB();
User[] userList = db.usersInDatabaseList();
comboBoxUsersEnrolled.Items.AddRange(userList);
comboBoxUsersEnrolled.DisplayMember = ?
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I am at a total loss as to how to get my database values to be displayed in the combobox. I’m also unsure of how to display a picture when an Item is selected. Can anyone help or steer me in the right direction please.
So I changed filldropdownlist method to the following:
public void FillDropDownList()
{
UserDB db = new UserDB();
User[] userList = db.cList();
for (int i = 0; i < userList.Length; i++)
{
comboBoxUsersEnrolled.Items.Add(userList[i]);
comboBoxUsersEnrolled.DisplayMember = "ComboName";
if (comboBoxUsersEnrolled.SelectedIndex > 0 )
{
byte[] pictureByteReader = (userList[i].UserImage);
MemoryStream ms = new MemoryStream(pictureByteReader);
Image picture = Image.FromStream(ms);
picBoxCapture.Image = picture;
}
}
}
I’m trying to add to my combobox and at the same time send a picture to a picturebox if that item is selected. My code does nothing. Gives no errors or anything. Help!
You have done most of the work already.
The
DisplayMemberproperty that you’ve marked with?indicates the property that should be used to display in the ComboBox. Your class doesn’t have any properties – it only has fields – so first you’ll want to create a propertyThen you can set the
DisplayMember = "Name";to use the Name as the display property.Using an image requires more work – you will need to use a template in the combobox instead of a string. Try googling that to find an example.