display data from a mysql database column in label [closed]

0

How do I give a select in a data from my column a table and show it in a label?

EX:

id = 10 -> Banana
Select * From tabela where id = 10
label.Text = Banana


 MySqlConnection conexao = new MySqlConnection();
            conexao.ConnectionString = ("server=localhost; user id=root; pwd=root ;database=semi");
            conexao.Open();
            MySqlCommand command = new MySqlCommand();
            command.Connection = conexao;
            command.CommandText = "Select Valor_Venda from produtos where    Codigo = '" + comboBox4.Text + "'";
            MySqlDataReader dr = command.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            label20.Text = dr;

I found an example on the internet and tried to adapt more believe that I'm not on the right path. Can you help me? Any doubt on my question comment that I reply.

    
asked by anonymous 04.06.2017 / 11:29

1 answer

0

In your code, since you want only one returned value, you can do it directly through ExecuteScalar.

It would look like this (I got your code as a base):

MySqlConnection conexao = new MySqlConnection();
conexao.ConnectionString = ("server=localhost; user id=root; pwd=root ;database=semi");
conexao.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = conexao;
command.CommandText = "Select Valor_Venda from produtos where    Codigo = '" + comboBox4.Text + "'";
string valor = command.ExecuteScalar().ToString("C");
label.Text = valor;
    
04.06.2017 / 15:14