Error bringing a Log Data to a label

Asked

Viewed 56 times

0

Hi, I’m trying to bring in for a Label a database registration data, for example: when the person selects the product on ComboBox the price of the product on label

However, I don’t think I’m getting the code question right, if someone can shed some light

code used:

private void cbxPeca_SelectedValueChanged(object sender, EventArgs e)
    {
        string strSelect = "SELECT Preco FROM Produto WHERE Preco LIKE (@Preco)";
        using (MySqlConnection conn = new MySqlConnection("server=127.0.0.1;database=ProdPacote; Uid=root; pwd=1234;"))
        {
            MySqlDataAdapter da = new MySqlDataAdapter(strSelect, conn);
            //Passagem por parâmetros.
            da.SelectCommand.Parameters.AddWithValue("@preco", cbxPeca.Text + "%");
            DataSet ds = new DataSet();
            da.Fill(ds, "Preco");
            label4.Text = ds.Tables["Preco"];
        }
    }
  • 2

    label4.Text = ds.Tables["Preco"] you’re trying to put an object Tables label? It would have to be the column value in a row, something like label4.Text = ds.Tables["Preco"].Rows[0]["nomedacoluna"]

  • actually I wanted to pull this column(Price) from the product table to a variable, but I decided to try to pull to a label to test out first

  • in the way you showed the ds.Tables["Preco"].Rows[0]["nomedacoluna"] I put as ds.Tables["Preco"].Rows[0]["Preco"] but gets it wrong (that red line at the bottom)

  • 1

    need to see what error is, pass the mouse and take a look. I would guess that it is a type conversion, maybe for a .ToString() at the end solve, but first we need to know if returned something, then check if the Rows has some return

  • It worked, The mistakes are gone, but still does not appear in label the price of the product when selecting the name of the product in the ComboBox

  • I tried to pass using this code label4 = passando.ToString(); but he speaks that he cannot convert implicitly type string in system.windows.forms.label

Show 1 more comment

1 answer

4

Try it this way:

DataTable ds = new DataTable();
        da.Fill(ds, "Preco");
        if (ds.Rows.Count>0 )
            label4.Text = ds.Rows[0]["Preco"];
  • I put it, yet it didn’t compile gets that red line in the ds and in part ds.Rows[0]["Preco"];

  • uses datatable instead of dataset

  • i gave a Ctrl C and Ctrl v in the code but still did not compile

Browser other questions tagged

You are not signed in. Login or sign up in order to post.