I have a ComboBox
with a list of items that contains names that are generated from my database (I draw the name of the coluna
beer that are allocated in the table) :
private void Form1_Load(object sender, EventArgs e)
{
try
{
conexao.Open();
SqlCommand cmdd = new SqlCommand("select Cerveja from Cervejas where Cerveja ='" + comboBox1.SelectedItem + "'", conexao);
SqlDataReader DR;
DR = cmdd.ExecuteReader();
if (DR.HasRows)
{
while (DR.Read())
{
comboBox1.Items.Add(DR.GetString(0));
}
}
else
{
MessageBox.Show("Não tem cervejas no nosso banco de dados!");
}
From this code I can get the name of the "Beers" column in the combobox when I start the program. What I need to do now is when the user selects an item in the combobox, it does a comparison with the name of the combobox item and the beers in the database, pulling the right beer row, I was trying so, but I can not:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
conexao.Open();
SqlCommand cmd = new SqlCommand("select * from Cervejas where Cerveja ='" + comboBox1.SelectedItem + "'", conexao);
SqlDataReader DR;
DR = cmd.ExecuteReader();
if (DR.Read())
{
if (comboBox1.SelectedItem == DR.GetValue(1))
{
MessageBox.Show("teste");
}
else MessageBox.Show("teste2");
}
}
catch (Exception ex)
{
conexao.Close();
MessageBox.Show("Erro de conexão Modbus: \n\n" + ex.Message);
}
In this case, in this part:
if (comboBox1.SelectedItem == DR.GetValue(1))
{
MessageBox.Show("teste");
}
I make this comparison, if the selected item corresponds to some beer in the database should print the word 'test', but that does not happen.