Mysqldataadapter Fill giving error

Asked

Viewed 194 times

1

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

I can insert data into the database very smoothly, but the selection of data is making me gray. Below the code and soon I will explain the error.

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 the sqlAdapter.Fill. It works the times and times of reference error:

Exception thrown: 'System.Nullreferenceexception' in Mysql.Data.dll Additional information: Object reference not defined for a instance of an object.

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

  • Strange, if it works sometimes, seems to me error in Mysql. Which Provider are you using? You are using the latest version of it?

  • Provider is the connector?? because if using 6.8.3 I think the latest version is 6.9.9

  • That. Try to update or search for some bug on the site or github of the connector. Your code seems normal, even because it works sometimes.

  • OK Murilo, I will update the connector and see if it changes, because I tried anyway to understand why I can make 2 to 5 insertions after this null reference error..

  • Hello Lorena, take a look at this link and see if the examples help you

  • Guys I appreciate the help of everyone who took a little time to help me, and really the problem was in mysql, I updated everything I could and could not solve, so I preferred to switch to sql server and now it is working right. Thank you very much

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.