-2

I'm trying to get some data out of Microsoft Acess and check it through MessageBoxes so the user can see if that is the data that he wants but the code its not executing at all. I can't see any of the message boxes and no exceptions are thrown. Below its the code that I put in the Button event. (I've only done the first if, the CNPJ one).

if (TipoConsulta.Text == "CNPJ")
{
    try
    {
        string comando = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
        string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\\\10.7.41.153\\Apoio\\Davi_Database\\Base_Imo.accdb";
        OleDbConnection Connection = new OleDbConnection(conn);
        OleDbCommand Command = new OleDbCommand(comando, Connection);
        Connection.Open();
        OleDbDataReader reader = Command.ExecuteReader();
        while (reader.Read())
        {
            foreach (OleDbDataReader resultado in reader)
            {
                DialogResult resultadoDialogo;
                resultadoDialogo = MessageBox.Show("Encontrada razão social '" + resultado.ToString() + "'.\n Está correto?", "Pesquisa", MessageBoxButtons.YesNo);
                if (resultadoDialogo == System.Windows.Forms.DialogResult.Yes)
                {

                    MessageBox.Show("Executado CarregaImobiliaria().");
                    break;
                }
                else if (resultadoDialogo == System.Windows.Forms.DialogResult.No)
                {
                    continue;
                }
            }
        }
    }
    catch (InvalidOperationException)
    {
        MessageBox.Show("Nada foi encontrado com o CNPJ " + ValorConsulta.Text + ".", "Pesquisa");
    }
}
else if (TipoConsulta.Text == "Razão Social")
{
    MessageBox.Show("Tipo: Razão Social");
    try
    {

    }
    catch (InvalidOperationException)
    {

    }
}
else if (TipoConsulta.Text == "RGI")
{
    MessageBox.Show("Tipo: Sub: RGI");
    try
    {

    }
    catch (InvalidOperationException)
    {

    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
davi.in
  • 21
  • 7
  • 2
    Set a breakpoint and step through the code as it executes. Examine variables in the watch window. See what happens. Put the breakpoint on this line: `if (TipoConsulta.Text == "CNPJ")`. See what `TipoConsulta.Text` is in the watch window. Is it `"CNPJ"`? Go from there, step by step. – 15ee8f99-57ff-4f92-890c-b56153 Nov 13 '17 at 20:21
  • Just debug it. Maybe your foreach don't find anything. Than there will be no MessageBox! – Momo Nov 13 '17 at 20:22
  • `reader.Read()` is returning `false`? – InBetween Nov 13 '17 at 20:23
  • 1
    [Here](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx) you have a guide of navigating through code with the debugger – elirandav Nov 13 '17 at 20:24
  • Also have a read of https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection – mjwills Nov 13 '17 at 20:38

1 Answers1

0

The problem was probably was the lack of a matrix to acess the DataReader value, because of that the foreach block was never executed. I ended up using a while with a int.

string _cmd = "SELECT razao_social FROM tblImobiliarias WHERE cnpj ='" + ValorConsulta.Text + "'";
                    string conn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\\\10.7.41.153\\Apoio\\Davi_Database\\Base_Imo.accdb";
                    OleDbConnection Connection = new OleDbConnection(conn);
                    OleDbCommand Command = new OleDbCommand(_cmd, Connection);
                    Connection.Open();
                    OleDbDataReader reader = Command.ExecuteReader();
                    while (reader.Read())
                    {
                        int _resMatriz = 0;
                        DialogResult result;
                        result = MessageBox.Show("Encontrado imobiliária: " + reader[_resMatriz] + ".\nEstá correto?", "Pesquisa", MessageBoxButtons.YesNo);
                        _resMatriz++;
                        if (result == System.Windows.Forms.DialogResult.Yes)
                        {
                            MessageBox.Show("CarregaImo()");
                            break;
                        }
                        else
                        {
                            continue;
                        } 
                    }
davi.in
  • 21
  • 7