Connection Error When try to Update Datagrid


New member
Jun 16, 2016
Programming Experience
Hi, i'm filling a datagridView using DataRow and everything works fine

MySqlDataReader readerNotas;
                       DataRow row = null;
            DataTable dtNotas = new DataTable("Anota??es");

            string query = "SELECT * FROM " + Controle.anotacoes;

            if (estado != "")
                query += " WHERE estado=@estado";

                        this.GridNotas.DataSource = null;

            cmd = new MySqlCommand(query, con.liga());



            readerNotas = cmd.ExecuteReader();


                row = dtNotas.NewRow();
                row[0] = readerNotas["id"];
                row[1] = readerNotas["nota"].ToString().ToUpper();

            this.GridNotas.DataSource = dtNotas;

            this.GridNotas.Columns[1].Visible = false;


When i change the values on another windows Form and return to this Form ,i have a method that updates the gridView, it works fine using Datatable.Fill, but i want to use DataRow, but after update the value when execute the refresh give an error
conexion Timeout, and application crashes.

Method to Update
        private void Principal_Activated(object sender, EventArgs e) // Ac??es do utilizador (outros forms) 
           if (Controle.controlarUpdate == 1)
                Controle.controlarUpdate = 0;

Can someone help me , i want to use DataRow (because i can control the returned data from database ).


* Sorry my english i'm , portuguese.
Last edited:
It's hard to know where to start here because you're going about this very, very wrongly. Firstly, the point of a data reader is to get data in a read-only, forward-only fashion. You apparently want to be able to save changes so you should not be using a data reader at all.

If you were going to use a data reader then you would not use a loop to read the data and populate a DataTable manually. You would call the Load method of the DataTable and pass the data reader as an argument and the DataTable would be populated automatically.

You say that it works when you use Datatable.Fill. There is no such method. You first create a data adapter and call Fill on it, passing the DataTable as an argument. Again, the DataTable is populated automatically. That is what you should be doing. You seem to be under the impression that that is somehow not using DataRows but it is. If you're using a DataTable then you have no choice but to use DataRows because that's how a DataTable stores its data. The DataTable object has a Rows property that exposes a DataRowCollection object and that collection contains the DataRows that contain the data.

You should also pretty much never call AcceptChanges. That method specifically states that there are no longer any changes to save. In this case, you should be using a data adapter to retrieve the data and populate a DataTable. Any changes you want to make should be made to the DataTable; more specifically to the DataRows in the DataTable. Once you're done, you call Update on the same data adapter and it will save the changes and then implicitly call AcceptChanges, only after those changes have been saved.
Top Bottom