Connection Error When try to Update Datagrid

N3lson

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

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

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

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

                        dtNotas.Rows.Clear();
                        this.GridNotas.DataSource = null;

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

            con.abre();

            cmd.Parameters.AddWithValue("@estado",estado);

            readerNotas = cmd.ExecuteReader();

            dtNotas.Columns.Add("id");
            dtNotas.Columns.Add("Nota");

            while(readerNotas.Read())
            {
                row = dtNotas.NewRow();
                row[0] = readerNotas["id"];
                row[1] = readerNotas["nota"].ToString().ToUpper();
                dtNotas.Rows.Add(row);
            }
            readerNotas.Close();

            this.GridNotas.DataSource = dtNotas;

            this.GridNotas.Columns[1].Visible = false;
                         dtNotas.AcceptChanges();

            con.fecha();
}

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
C#:
        private void Principal_Activated(object sender, EventArgs e) // Ac??es do utilizador (outros forms) 
        {
           if (Controle.controlarUpdate == 1)
            {
               this.UltimasOrdens();
                this.PreventivasEfectuar();
               this.ReturnNotas("1");
                Controle.controlarUpdate = 0;
            }
            
        }

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

Thanks

* 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.
 
Back
Top Bottom