Question

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???

Was it helpful?

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..

OTHER TIPS

While not a direct answer to your problem, per your request here's some info and a code example on using.

It's generally accepted best practice to use using statements with objects that implement IDisposable - it essentially does the same thing as a try/finally block.

Once the scope of the using statement is exited (either the operation completes or an exception is thrown), Dispose will be called on the object. See using Statement for more details.

Here's an abbreviated example based on your code:

int catCount = 1;     
string connectionString = "server=server;user id=user;password=password;database=db;"
string query = "SELECT email FROM oc_newsletter_old_system WHERE cat_uid=@cat_uid";

while (catCount < 13)
{

    using (MySqlConnection con = new MySqlConnection(connectionString))
    {

        con.Open();

        MySqlCommand mC = new MySqlCommand(query, con);
        mC.Parameters.AddWithValue("@cat_uid", catCount);

        using (MySqlDataReader Rd = mC.ExecuteReader())
        {

            while (Rd.Read())
            {

                 // rest of your code here
            }
        }
    }    
}

Note that the above is based on the .NET SqlConnection, SqlCommand and SqlDataReader classes, but should be the same (or very similar) for MySql.

I moved the connection string and query string to their own variables (but that's a personal preference). I also gave an example of how to do parameterized queries (the mCParameters.AddWithValue line - getting in the habit of using parmaeterized queries is (IMO) a good thing, especially if you move from WinForms to web-based apps, as it prevents SQL Injection attacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top