MySqlDataAdapter Fill giving error

1

When using database connection in C #, I saw many ways to do it.

I can make insertion of data in the database very quiet, but the selection of data is leaving me with white hair. Below the code and I will explain the error soon.

public void AtualizarGrid()
{
    grdclientes.DataSource = "";
    try
    {
        using (MySqlConnection mConn = new MySqlConnection())
        {
            MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
            conn_string.Server = "localhost";
            conn_string.UserID = "root";
            conn_string.Password = "guijermo10";
            conn_string.Database = "cadastro";
            conn_string.Port = 3306;

            mConn.ConnectionString = conn_string.ToString();
            mConn.Open();

            using (MySqlCommand cCommand = new MySqlCommand())
            {
                cCommand.Connection = mConn;
                cCommand.CommandType = CommandType.Text;
                cCommand.CommandText = "SELECT * FROM cadastro.clientes";

                MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(cCommand);
                DataTable dtRetorno = new DataTable();
                sqlAdapter.Fill(dtRetorno);
                grdclientes.DataSource = dtRetorno;
            }
            mConn.Close();
        }
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

My problem is sqlAdapter.Fill . It works both times and sometimes the reference error:

  

Exception thrown: 'System.NullReferenceException' in MySql.Data.dll   Additional information: Object reference not set to a   instance of an object.

And I have debugged and I could not solve the problem, I know that the error occurs with the reference loss of the datatable, but I do not know how to solve it.

    
asked by anonymous 06.01.2017 / 14:02

0 answers