Fill a Textbox with a selected item in a ComboBox C #

0

I have a database in Access in which there are 2 fields called Base de Datos and another one called Tipo de Usuario . I would like that when selecting an item in the combobox, the type of user that it is appears in the textbox of Tipo de Usuario . There are 2 possible Administrator or User options. I understand a little the logic that I must use, the code that I use is the following but it throws me an error, which says that some parameters are missing, which I can not understand what it refers to. Beforehand, thank you.

private void base_de_DatosComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        OleDbConnection cnx = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\BD_Usuarios.accdb");

        try
        {
            cnx.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = cnx;
            string query = "select * from Usuarios where Tipo_de_Usuario='"+base_de_DatosComboBox.Text+"';";
            command.CommandText = query;

            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                tipo_de_UsuarioTextBox.Text = reader["Tipo de Usuario"].ToString();
            }
            cnx.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error " + ex);
        }
    }

I enclose a photo of the error:

    
asked by Emmanuel Aguilar 25.04.2018 в 05:39
source

1 answer

0

It seems that your error may come from trying to collect data from a non-existent column in the table.

Fixing me in the code that you show us I notice that in the query SQL you put:

select * from Usuarios where Tipo_de_Usuario='"+base_de_DatosComboBox.Text+"';

Having in the WHERE the column Tipo_de_Usuario and in the reader you have the following:

reader["Tipo de Usuario"].ToString();

If you notice, you are not taking the column correctly since the reader's does not have _ .

Your reader should look like this:

reader["Tipo_de_Usuario"].ToString();
    
answered by 25.04.2018 в 09:29