Search code examples
c#sqldatareaderdatareadermysql.data

Fatal error encounterd when using MySqlDataReader


I'm getting the following error at my C# WinForms application:

fatal error encountered during data read

I'm using mysql.data V4.0.30319.

Here's my code:

int catCount = 1;     
while (catCount < 13)
{
    MySqlConnection con = new MySqlConnection("server=server;user id=user;password=password;database=db;");
    con.Open();
    MySqlDataReader Rd;
    string query = "SELECT oc_newsletter_old_system.email FROM oc_newsletter_old_system WHERE oc_newsletter_old_system.cat_uid = "  + catCount;
    MySqlCommand mC = new MySqlCommand(query, con);
    Rd = mC.ExecuteReader();
    try
    {      
        while (Rd.Read())
        {
            string email = Rd.GetString(0);
            Boolean insert = true;
            int realCat = 0;
            switch (catCount)
            {
                case '1':
                    fileBrian += email + "\r\n";
                    break;
                case '2':
                    fileDunamis += email + "\r\n";
                    break;
                case '3':
                    fileFrohlich += email + "\r\n";
                    break;
                case '4':
                    fileGaithers += email + "\r\n";
                    break;
                case '5':
                    fileGospel7 += email + "\r\n";
                    break;
                case '6':
                    fileLifeshop += email + "\r\n";
                    realCat = 1;
                    break;
                case '7':
                    fileMeyer += email + "\r\n";
                    break;
                case '8':
                    fileOpwekking += email + "\r\n";
                    break;
                case '9':
                    filePelgrimKerken += email + "\r\n";
                    realCat = 2;
                    break;
                case 10:
                    filePelgrimKlanten += email + "\r\n";
                    realCat = 2;
                    break;
                case 11:
                    filePelgrimPers += email + "\r\n";
                    insert = false;
                    break;
                case 12:
                    filePelgrimScholen += email + "\r\n";
                    insert = false;
                    break;
            }

            if (insert == true)
            {    
                string salt = "h1bsqnp6wyxjxhf29ziuwsihg8nixe05";
                byte[] asciiBytes = ASCIIEncoding.ASCII.GetBytes(salt + email);
                byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
                string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
                string query2 = "INSERT IGNORE INTO `oc_ne_marketing` (`email`,`code`, `subscribed`, `store_id`) VALUES ('" + email + "', '" + hashedString + "', '1', '" + realCat + "')";
                MySqlConnection connection = new MySqlConnection("server=server;user id=user;password=password;database=db;");
                connection.Open();
                try
                {
                    MySqlCommand cmd1 = new MySqlCommand(query2, connection);
                    cmd1.CommandText = query2;
                    cmd1.ExecuteNonQuery();
                    cmd1.Dispose();
                }
                finally
                {
                    connection.Close();
                }
            }
            Application.DoEvents();
        }
    }
    finally
    {
        Rd.Close();
        con.Close();
    }

    catCount++;
    progressBar1.PerformStep();
    progressBar1.Refresh();
    Application.DoEvents();
}

I'm getting the error at this line:

while (Rd.Read())

The only thing that i found out so far was an problem with closing the reader and connection, but as far as i know is that all setup right...

So do you guys got any idea what's the matter???


Solution

  • A2A, for the sake of completeness.

    From here:

    The workaround then was to customize the net_xxx_timeout settings. For example, issue the following after openning your connection and before executing the datareader:

    var c = new MySqlCommand("set net_write_timeout=99999; set net_read_timeout=99999", con);
    c.ExecuteNonQuery();
    

    Here's source from MySql.Data in connection class:

    protected override void Dispose(bool disposing)
    {
      if (State == ConnectionState.Open)
        Close();
      base.Dispose(disposing);
    }
    

    which basically means disposing is enough to close it (its bizarre otherwise). Same holds true for SqlConnection too..