Fatal error encountered during data read

adi.shoukat's Avatar, Join Date: Jun 2009
Newbie Member
Hi,
I am working on an application in which I need to read all records from a mySql table and process them one-by-one. Unfortunately I have some 850 records and it takes almost 20 minutes to process and print all the records. But after reading almost a 100 records from DB and printing them I get an exception with Error Message: "Fatal error encountered during data read."
Here are the Exception Details:
Code:
MySql.Data.MySqlClient.MySqlException was unhandled
  Message="Fatal error encountered during data read."
  Source="MySql.Data"
  ErrorCode=-2147467259
  Number=0
  StackTrace:
       at MySql.Data.MySqlClient.MySqlDataReader.Read()
       at Askari9.mainForm.performBillPrinting() in C:\Users\adil\Documents\Visual Studio 2008\Projects\Askari9\Askari9\Form1.cs:line 645
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: MySql.Data.MySqlClient.MySqlException
       Message="Reading from the stream has failed."
       Source="MySql.Data"
       ErrorCode=-2147467259
       Number=0
       StackTrace:
            at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
            at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
            at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns)
            at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns)
            at MySql.Data.MySqlClient.ResultSet.GetNextRow()
            at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior)
            at MySql.Data.MySqlClient.MySqlDataReader.Read()
       InnerException: System.IO.IOException
            Message="Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
            Source="MySql.Data"
            StackTrace:
                 at MyNetworkStream.HandleOrRethrowException(Exception e)
                 at MyNetworkStream.Read(Byte[] buffer, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count)
                 at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count)
                 at MySql.Data.MySqlClient.MySqlStream.LoadPacket()
            InnerException: System.Net.Sockets.SocketException
                 Message="An existing connection was forcibly closed by the remote host"
                 Source="System"
                 ErrorCode=10054
                 NativeErrorCode=10054
                 StackTrace:
                      at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
                 InnerException:


and here is my code:

Code:
           string MyConString = "SERVER=localhost;" +
                "DATABASE=askari9;" +
                "UID=root;" +
                "PASSWORD=;" +
                "Connect Timeout=21600;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;



            String strCommandText = "SELECT *";
            strCommandText += " FROM homeowners";

            command.CommandText = strCommandText;
            command.CommandTimeout = 21600;
            connection.Open();    // Opening DB connection
            Reader = command.ExecuteReader();
while (Reader.Read()) // Getting Exception on Reader.Read() after a few hundreds iterations
            {
                (range.Cells[12, 3] as Excel.Range).Value2 = Reader.GetValue(ownerNameAndRankFieldIndexInDB).ToString().Trim(); // Owner Name
                (range.Cells[13, 3] as Excel.Range).Value2 = Reader.GetValue(houseNoFieldIndexInDB).ToString().Trim(); // House Number
                (range.Cells[14, 3] as Excel.Range).Value2 = textBoxBillingMonth.Text; // Billing Month
                (range.Cells[15, 3] as Excel.Range).Value2 = textBoxDueDate.Text; // Due Date
                (range.Cells[18, 6] as Excel.Range).Value2 = textBoxServiceCharges.Text; // Service Charges


                ((Excel._Workbook)xlWorkBook).PrintOut(missing, missing,
                    missing, missing, missing,
                    missing, missing, missing);

                // Time taken by printer to process one document
                Thread.Sleep(2000);
            }
            connection.Close(); // closing DB connection
I've tried increasing Connection and Command TimeOut but that didn't work either. Does anyone know any other way out ???
0
adi.shoukat's Avatar, Join Date: Jun 2009
Newbie Member
Between connection.Open(); and command.ExecuteNonQuery(); I just added two lines like this:

Code:
connection.Open();

MySqlCommand cmd = new MySqlCommand("set net_write_timeout=99999; set net_read_timeout=99999", connection); // Setting tiimeout on mysqlServer
cmd.ExecuteNonQuery();

int numOfRecordsUpdated = command.ExecuteNonQuery();
Problem Fixed