A
aurquiel
Guest
Hello i have problems binding an inherit object of the type List<User>, i can't figure out how to set the DisplayMember and ValueMember to the ComboBox
here is my class UserObject
using ComissionsTwo.Backend.Permissions; using ComissionsTwo.Backend.SQL.User; using System.Collections.Generic; using System.Data; namespace ComissionsTwo.Backend.User { internal class UserObject { protected string userId; protected string userAlias; protected string userPassword; protected string userName; protected string userLastName; protected string userMail; protected string userIdRol; protected DataTable userData; protected DataTable userPermissions; protected SQLUser sqlUser = new SQLUser(); protected RolesPermissionsOperations rolesPermissionsControl = new RolesPermissionsOperations(); protected static List<UserObject> listOfUsers = new List<UserObject>(); public UserObject(string userAlias, string userPassword) { this.userAlias = userAlias; this.userPassword = userPassword; } public UserObject(string userAlias) { this.userAlias = userAlias; } public UserObject() { } }
Here is my class from that i inherit my UserObject class, i call the method getListUsers() to populate the list of users that will be binded to the combobox
using ComissionsTwo.Backend.Messages; using System.Collections.Generic; using System.Data; using System.DirectoryServices; namespace ComissionsTwo.Backend.User { internal class UserOperations : UserObject { public UserOperations(string userAlias, string userPassword) : base(userAlias, userPassword) { } public UserOperations(string userAlias) : base(userAlias) { } public UserOperations() : base() { } internal bool getUsers() { listOfUsers.Clear(); DataTable aliasUsers = sqlUser.getUsers(); foreach (DataRow row in aliasUsers.Rows) { UserOperations user = new UserOperations(row[0].ToString()); if (user.getInfo() == false) { MessagesInfo.StatusMessage = "No se pudo obtener la información de los usuarios activos"; return false; } listOfUsers.Add(user); } return true; } internal List<UserObject> getListUsers() { return listOfUsers; } internal bool addUser(DataTable userData, string userActiveId) { return sqlUser.addUser(userData, userActiveId); } internal bool editUser(DataTable userData, string userActiveId) { return sqlUser.editUser(userData, userActiveId); } internal bool eraseUser(string userId, string userActiveId) { return sqlUser.eraseUser(userId, userActiveId); } internal bool getUserPermissions() { userPermissions = rolesPermissionsControl.getUserPermissions(userIdRol); if (userPermissions == null || userPermissions.Rows[0][0].ToString() == string.Empty) { return false; } else { return true; } } internal bool getInfo() { if (loginInDatabase() == false) { MessagesInfo.StatusMessage = "No se pudo obtener la información del usuario a editar"; return false; } else if (getUserPermissions() == false) { MessagesInfo.StatusMessage = "No se pudo obtener los permisos de usuario."; return false; } else { return true; } } internal bool login() { if (loginInActiveDirectory() == false) { MessagesInfo.StatusMessage = "El usuario no pudo ser logeado en el directorio activo."; return false; } else if (loginInDatabase() == false) { MessagesInfo.StatusMessage = "El usuario no pudo ser logeado en la base de datos."; return false; } else if (getUserPermissions() == false) { MessagesInfo.StatusMessage = "No se pudo obtener los permisos de usuario."; return false; } else { return true; } } private bool loginInActiveDirectory() { string path = @"LDAP://192.168.0.15"; //CAMBIAR POR VUESTRO PATH (URL DEL SERVICIO DE DIRECTORIO LDAP) //Por ejemplo: 'LDAP://ejemplo.lan.es' string domain = @"grupototal99.local"; //CAMBIAR POR VUESTRO DOMINIO string domainUser = domain + @"\" + userAlias; //CADENA DE DOMINIO + USUARIO A COMPROBAR return autenticaUsuario(path, domainUser, userPassword); } private bool autenticaUsuario(string path, string user, string pass) { //Los datos que hemos pasado los 'convertimos' en una entrada de Active Directory para hacer la consulta DirectoryEntry directoryEntry = new DirectoryEntry(path, user, pass, AuthenticationTypes.Secure); try { //Inicia el chequeo con las credenciales que le hemos pasado //Si devuelve algo significa que ha autenticado las credenciales DirectorySearcher ds = new DirectorySearcher(directoryEntry); ds.FindOne(); return true; } catch { //Si no devuelve nada es que no ha podido autenticar las credenciales //ya sea porque no existe el usuario o por que no son correctas return false; } } private bool loginInDatabase() { userData = sqlUser.getUser(userAlias); if (userData == null || userData.Rows[0][0].ToString() == string.Empty) { return false; } else { userId = userData.Rows[0][0].ToString(); userName = userData.Rows[0][2].ToString(); userLastName = userData.Rows[0][3].ToString(); userMail = userData.Rows[0][4].ToString(); userIdRol = userData.Rows[0][5].ToString(); return true; } } } }
Here is my method to bind the comboBox
private void userEditComboBoxBinding() { BindingSource usersEditComboBoxBindingSource = new BindingSource(); usersEditComboBox.SelectedIndexChanged -= new System.EventHandler(this.usersEditComboBox_SelectedIndexChanged); usersEditComboBoxBindingSource.DataSource = user.getListUsers(); usersEditComboBox.DataSource = usersEditComboBoxBindingSource.DataSource; usersEditComboBox.DisplayMember = "userAlias"; usersEditComboBox.ValueMember = "userId"; usersEditComboBox.SelectedIndex = -1; usersEditComboBox.SelectedIndexChanged += new System.EventHandler(this.usersEditComboBox_SelectedIndexChanged); }
I have tried
usersEditComboBox.DisplayMember = "userAlias";
usersEditComboBox.ValueMember = "userId";
And
usersEditComboBox.DisplayMember = "UserAlias";
usersEditComboBox.ValueMember = "UserId";
But i always get and error if i commented usersEditComboBox.ValueMember the comboBox display this
ComissionsTwo.Backend.User.UserOperations the name of my class
Continue reading...
here is my class UserObject
using ComissionsTwo.Backend.Permissions; using ComissionsTwo.Backend.SQL.User; using System.Collections.Generic; using System.Data; namespace ComissionsTwo.Backend.User { internal class UserObject { protected string userId; protected string userAlias; protected string userPassword; protected string userName; protected string userLastName; protected string userMail; protected string userIdRol; protected DataTable userData; protected DataTable userPermissions; protected SQLUser sqlUser = new SQLUser(); protected RolesPermissionsOperations rolesPermissionsControl = new RolesPermissionsOperations(); protected static List<UserObject> listOfUsers = new List<UserObject>(); public UserObject(string userAlias, string userPassword) { this.userAlias = userAlias; this.userPassword = userPassword; } public UserObject(string userAlias) { this.userAlias = userAlias; } public UserObject() { } }
Here is my class from that i inherit my UserObject class, i call the method getListUsers() to populate the list of users that will be binded to the combobox
using ComissionsTwo.Backend.Messages; using System.Collections.Generic; using System.Data; using System.DirectoryServices; namespace ComissionsTwo.Backend.User { internal class UserOperations : UserObject { public UserOperations(string userAlias, string userPassword) : base(userAlias, userPassword) { } public UserOperations(string userAlias) : base(userAlias) { } public UserOperations() : base() { } internal bool getUsers() { listOfUsers.Clear(); DataTable aliasUsers = sqlUser.getUsers(); foreach (DataRow row in aliasUsers.Rows) { UserOperations user = new UserOperations(row[0].ToString()); if (user.getInfo() == false) { MessagesInfo.StatusMessage = "No se pudo obtener la información de los usuarios activos"; return false; } listOfUsers.Add(user); } return true; } internal List<UserObject> getListUsers() { return listOfUsers; } internal bool addUser(DataTable userData, string userActiveId) { return sqlUser.addUser(userData, userActiveId); } internal bool editUser(DataTable userData, string userActiveId) { return sqlUser.editUser(userData, userActiveId); } internal bool eraseUser(string userId, string userActiveId) { return sqlUser.eraseUser(userId, userActiveId); } internal bool getUserPermissions() { userPermissions = rolesPermissionsControl.getUserPermissions(userIdRol); if (userPermissions == null || userPermissions.Rows[0][0].ToString() == string.Empty) { return false; } else { return true; } } internal bool getInfo() { if (loginInDatabase() == false) { MessagesInfo.StatusMessage = "No se pudo obtener la información del usuario a editar"; return false; } else if (getUserPermissions() == false) { MessagesInfo.StatusMessage = "No se pudo obtener los permisos de usuario."; return false; } else { return true; } } internal bool login() { if (loginInActiveDirectory() == false) { MessagesInfo.StatusMessage = "El usuario no pudo ser logeado en el directorio activo."; return false; } else if (loginInDatabase() == false) { MessagesInfo.StatusMessage = "El usuario no pudo ser logeado en la base de datos."; return false; } else if (getUserPermissions() == false) { MessagesInfo.StatusMessage = "No se pudo obtener los permisos de usuario."; return false; } else { return true; } } private bool loginInActiveDirectory() { string path = @"LDAP://192.168.0.15"; //CAMBIAR POR VUESTRO PATH (URL DEL SERVICIO DE DIRECTORIO LDAP) //Por ejemplo: 'LDAP://ejemplo.lan.es' string domain = @"grupototal99.local"; //CAMBIAR POR VUESTRO DOMINIO string domainUser = domain + @"\" + userAlias; //CADENA DE DOMINIO + USUARIO A COMPROBAR return autenticaUsuario(path, domainUser, userPassword); } private bool autenticaUsuario(string path, string user, string pass) { //Los datos que hemos pasado los 'convertimos' en una entrada de Active Directory para hacer la consulta DirectoryEntry directoryEntry = new DirectoryEntry(path, user, pass, AuthenticationTypes.Secure); try { //Inicia el chequeo con las credenciales que le hemos pasado //Si devuelve algo significa que ha autenticado las credenciales DirectorySearcher ds = new DirectorySearcher(directoryEntry); ds.FindOne(); return true; } catch { //Si no devuelve nada es que no ha podido autenticar las credenciales //ya sea porque no existe el usuario o por que no son correctas return false; } } private bool loginInDatabase() { userData = sqlUser.getUser(userAlias); if (userData == null || userData.Rows[0][0].ToString() == string.Empty) { return false; } else { userId = userData.Rows[0][0].ToString(); userName = userData.Rows[0][2].ToString(); userLastName = userData.Rows[0][3].ToString(); userMail = userData.Rows[0][4].ToString(); userIdRol = userData.Rows[0][5].ToString(); return true; } } } }
Here is my method to bind the comboBox
private void userEditComboBoxBinding() { BindingSource usersEditComboBoxBindingSource = new BindingSource(); usersEditComboBox.SelectedIndexChanged -= new System.EventHandler(this.usersEditComboBox_SelectedIndexChanged); usersEditComboBoxBindingSource.DataSource = user.getListUsers(); usersEditComboBox.DataSource = usersEditComboBoxBindingSource.DataSource; usersEditComboBox.DisplayMember = "userAlias"; usersEditComboBox.ValueMember = "userId"; usersEditComboBox.SelectedIndex = -1; usersEditComboBox.SelectedIndexChanged += new System.EventHandler(this.usersEditComboBox_SelectedIndexChanged); }
I have tried
usersEditComboBox.DisplayMember = "userAlias";
usersEditComboBox.ValueMember = "userId";
And
usersEditComboBox.DisplayMember = "UserAlias";
usersEditComboBox.ValueMember = "UserId";
But i always get and error if i commented usersEditComboBox.ValueMember the comboBox display this
ComissionsTwo.Backend.User.UserOperations the name of my class
Continue reading...