Reader does not read in the first if c # mysql

1
namespace Regext.operaciones
{
    class grillas
    {



        MySqlDataReader dr = null;

        public void llenargrillaclienteempresa(DataGridView grilla_empresa, Bunifu.Framework.UI.BunifuMaterialTextbox txt_buscar_empresa)
        {

            MySqlCommand cmd = new MySqlCommand("select * from tb_empresa where rut_empresa = '" + txt_buscar_empresa.Text + "'", con);
            con.Open();
            try
            {
                dr = cmd.ExecuteReader();

                if(dr.Read() == true){

                 MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        grilla_empresa.DataSource = dt;

                }else{
                    MessageBox.Show("no se encontro rut");
                }
            }
            catch (Exception f)
            {
                MessageBox.Show("error");
            }
            finally
            {
                con.Close();
            }



        }


    }
}

I have this class that serves to fill in the grids but the reader does not read that does not enter the first if but if deference if the rut is in the database or not.

    
asked by sebastian bizama inostroza 21.09.2017 в 23:28
source

1 answer

0

I'm just going to leave you with a piece of code that you can rehearse to correct the problem you're raising. But keep in mind that your code has several bad practices, when working with connections to databases, this I write to you to investigate later.

MySqlCommand cmd = new MySqlCommand("select * from tb_empresa where rut_empresa = '" + txt_buscar_empresa.Text + "'", con);
DataTable dt = new DataTable();
try
{
    con.Open();
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);    
    da.Fill(dt);            
}
catch (Exception f)
{
    MessageBox.Show("error");
}
finally
{
    con.Close();
}


if(dt.Rows.Count == 0)
    MessageBox.Show("no se encontro rut");
else
   grilla_empresa.DataSource = dt;
    
answered by 21.09.2017 в 23:46