-1

I get this error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

When running this code:

namespace ProjectInterface
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());   -exception unhandled(error)!!!!
        }
    }
}

When I search a subject name from the database .

Here my textbox code:

private void txtSubject_TextChanged(object sender, EventArgs e)
{
    con.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT  SubjectName From databse.subject WHERE SubjectName LIKE @name", con);
    cmd.Parameters.Add(new MySqlParameter("@name", "%" + txtSubject.Text + "%"));
    MySqlDataReader dr = cmd.ExecuteReader();
    AutoCompleteStringCollection subjectColl = new AutoCompleteStringCollection();
    while (dr.Read())
    {
        subjectColl.Add(dr.GetString(0));
    }
    txtSubject.AutoCompleteCustomSource = subjectColl;
    con.Close();
}

Sometimes it is OK to run, but it often shows this error. How do I solve it?

lily
  • 19
  • 5
  • 2
    What line of code do you see the error on? – d219 Apr 05 '18 at 12:30
  • 1
    Possible duplicate of [Attempted to read or write protected memory. This is often an indication that other memory is corrupt](https://stackoverflow.com/questions/18103444/attempted-to-read-or-write-protected-memory-this-is-often-an-indication-that-ot) – mjwills Apr 05 '18 at 12:36
  • @HenkHolterman They both involve database access and the use of `AutoCompleteStringCollection`. They certainly _smell_ similar. At the very least it is a _possible_ duplicate. – mjwills Apr 05 '18 at 12:57

1 Answers1

0

Issues with the code

The only issues I can identify in the code as you have provided (as some information is lacking) is that every time you type a character, you do a database query and you do that on the same thread (the one the UI is on) and so if the query took 2 seconds to complete, your typing would take 2 seconds per character to type. Very bad experience for the user.

For the MySQL statement itself, there is nothing inherently wrong with it, although I do believe this is the cause of your issue.

Fixing the UI thread

Firstly I would say to understand why you have the thread issue you should first understand threads. I've made a video on it here https://www.youtube.com/watch?v=XXg9g56FS0k and here https://www.youtube.com/watch?v=S0jPzb9kk3o

After you understand the issue you should be able to safely drop the call onto another thread. However you will then have another issue... Then if each call takes 2 seconsd and the user types 10 characters in that time you will get 10 database calls going on. As that is not however the issue in this question I am sure, I do not want to muddy the waters with a long post on solving an unrelated issue.

The underlying problem and solution

So from the given amount of code and detail, I can pretty safely say (99.95%) that the code to cause the memory exception must come from the MySQL calls. As you do not provide the library/dll/reference you are using for the MySQL I can simply presume you are possibly using an old or buggy version.

An example of a driver for MySQL causing this same issue is here Asp.net Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

What I would recommend you use if you have not already, and hopefully to fix your issue, is this version of a maintained library for working with MySQL https://www.nuget.org/packages/Mysql.Data/

If you still get issues and you are using that, I would start to suspect the database itself or the network it is intermittent and causing the error.

An important note is you should in Visual Studio enable all error catching so press Ctrl + Alt + E and check every box. Then when you get this error it should take you directly to the exact line of code it fails on and we can investigate further

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
angelsix
  • 392
  • 1
  • 7