C # ComboBox and SQL

0

I would like to know how to load values in a combobox, listing an entire column of the database, for example, I have a column that is called "Beer" and has several names, I would like to list that column in a ComboBox. Thanks.

I have a Stored Procedure:

CREATE PROCEDURE [dbo].[LoadCerveja]
(
    @Cerveja varchar(60)
)
AS
    SELECT @cerveja from Ingredientes
RETURN

In the code I need to add in the formLoad for it to pull the ComboBox to the "Beer" column of the database.

I tried the following (no form load)

private void Form1_Load(object sender, EventArgs e)
{
try
{
conexao.Open();
SqlCommand cmdd = new SqlCommand("LoadIngredients", conexao);
cmdd.CommandType = CommandType.StoredProcedure;
cmdd.Parameters.Add("@Cerveja");
SqlDataReader DR;
DR = cmdd.ExecuteReader();
{
 comboBox1.Items.Add(DR.GetValue()); **// Não sei o que fazer aqui pra        adicionar      os dados ao combobox !!!!**
}
else MessageBox.Show("Erro ao buscar receita no DB.");

}

    
asked by anonymous 21.09.2016 / 01:25

1 answer

2

You have an example in the documentation on SqlDataReader .

....
    SqlDataReader DR = cmdd.ExecuteReader();

    if (DR.HasRows)
    {
      while (DR.Read())
      {
          comboBox1.Items.Add(DR.GetString(0)); 
      }
    }
    else
    {
      Console.WriteLine("No rows found.");
    }
    DR.Close();
....

In your parameters you can enter the value in two ways

cmdd.Parameters.Add("@Cerveja", SqlDbType.VarChar).Value = "valor"

or

cmdd.Parameters.AddWithValue("@Cerveja", valor);

I hope I have helped!

    
21.09.2016 / 01:49