views:

86

answers:

4

Look at this:

foreach(Object Item in comboBox1.Items)
{
    if (Convert.ToString(Item) == Convert.ToString(dsGirasol.Tables["DatosGirasol"].Rows[contador][0]))
    {
        repetido = true;
        break;
    }
    else
    {
        repetido = false;
    }
}​

Note that both of the possible outputs have a messagebox. However when I run this, nothing appears at all, and the rest of the code continues to run...

EDIT: Added the surrounding loop!

+2  A: 

Why do you want the break there for? Try this:

if (Convert.ToString(Item) == Convert.ToString(dsMaiz.Tables["DatosMaiz"].Rows[contador][0]))
{
    repetido = true;
    MessageBox.Show("Encontre uno igual");
}
else
{
    repetido = false;
    MessageBox.Show("Encontre uno diferente");
}
Floyd Pink
The break is to break the foreach loop this code is in. The "item" is an object inside a ComboBox. This code compares an entry in a table to the entries already in the ComboBox. If the entry can be already found in the comboBox, then the loop is broken.
fede
+1  A: 

Try evaluating the left and right parts of the condition before you evaluate the equality. I can only imagine that it must be throwing an exception that is silently caught. This will help you debug the issue.

eg:

var left = Convert.ToString(Item);
var right = Convert.ToString(dsMaiz.Tables["DatosMaiz"].Rows[contador][0]);
if (left == right)
{
    ...
}
else
{
  ...
}

EDIT: Now that I see you are using a loop, go back to basics, is the loop even running? Low tech debugging, check that there are some Items in the combobox and that you are referencing the combo you intended :)

Simon Francesco
+1 for debugging procedures and heuristics, someone should really start a page of these!
Nescio
A: 

I tried Simon's solution, but it didnt work. The strange this is that the code I showed is actually the second time the ComboBox is compared to the table entries. On the first loop, the entries are added correctly. On this second loop, the IF is ignored:

First Loop (table "Girasol" is used):

foreach (Object Item in comboBox1.Items)
            {
                string a = Convert.ToString(Item);
                string b = Convert.ToString(dsGirasol.Tables["DatosGirasol"].Rows[contador][0]);
                if (a == b)
                {
                    repetido = true;
                    break;
                }
                else
                {
                    repetido = false;
                }
            }

This one works.

The second one doesnt (Table "maiz" is used"):

foreach (Object Item in comboBox1.Items)
            {
                string a = Convert.ToString(Item);
                string b = Convert.ToString(dsMaiz.Tables["DatosMaiz"].Rows[contador][0]);
                if (a == b)
                {
                    MessageBox.Show("Hello");
                    repetido = true;
                    break;
                }
                else
                {
                    MessageBox.Show("Hello");
                    repetido = false;
                }
            }

The hello! message is never shown.

fede
Given these results, it would seem DatosMaiz does not exist, and the code is throwing an exception that is being caught silently.
Nescio
A: 

Ok this is strange, I managed to solve the problem by puting each loop on a separate method, and now it works, thanks for the help anyways!

fede