0

I'm getting this weird AccessViolationException in my C# program that uses a MySQL database connection. I use values from the database to generate an autocorrect facility for a textbox. Here's the code snippet:

private void textBoxName_TextChanged(object sender, EventArgs e)
    {

            AutoCompleteStringCollection asc = Database.getSuggestedNames(textBoxName.Text);
            if (asc != null)
                textBoxName.AutoCompleteCustomSource = asc;                         
    }

The getSuggestedNames(string) method is implemented in Database.cs as follows:

public static AutoCompleteStringCollection getSuggestedNames(string namepart)
    {
        string query = "SELECT name FROM worker WHERE name LIKE '%"+namepart+"%';";
        string[] namecolumn = { "name" };
        List<string>[] names = getValues(query,namecolumn);
        AutoCompleteStringCollection namec = new AutoCompleteStringCollection();
        for (int i = 0; i < names[0].Count; i++)
            namec.Add(names[0][i]);
        return namec;
    }

The actual query is done with a separate method in the same file called getValues:

private static List<string>[] getValues(string query,string[] columnNames)
    {
        if (connection == null)
            initialize();

        if (connection.State == ConnectionState.Closed)
            connection.Open();

        MySqlCommand cmd = new MySqlCommand(query, connection);
        MySqlDataReader dataReader = cmd.ExecuteReader();
        List<string>[] list = new List<string>[columnNames.Length];

        for (int i = 0; i < columnNames.Length; i++)
            list[i] = new List<string>();

        while (dataReader.Read())
        {
            for(int i=0;i<columnNames.Length;i++)
                list[i].Add(dataReader[columnNames[i]] + "");
        }

        dataReader.Close();
        connection.Close();
        return list;
    }

The exception always occurs when the user enters data into the textbox textBoxName. The error is not very frequent and the call stack shows its the external code that causes the problem. So I assume it should be MySQL that causes the problem.

Can somebody please help me to overcome this problem since there is nothing I can do.

The exact line the error occurs on is line 17 in the following code,which is not much use for me for debugging. Visual Studio does not show any other place. The stack trace shows [external code]. But here it is anyway:

1    using System;
2    using System.Collections.Generic;
3    using System.Windows.Forms;
4    
5    namespace LabourManagement
6    {
7        static class ManagementProgram
8        {
9            /// <summary>
10            /// The main entry point for the application.
11            /// </summary>
12            [STAThread]
13            static void Main()
14            {
15                Application.EnableVisualStyles();
16                Application.SetCompatibleTextRenderingDefault(false);
17                Application.Run(new General());
18                         
19            }
20        }
21    }
4

2 回答 2

0

我在处理 DataGridView 自动完成功能时遇到了类似的问题。最后,我发现奇怪的问题'访问冲突读取位置 0x00000000' 是由于 .net 中的一个小半错误,如果您将null字符串添加到 AutoCompleteStringCollection 会导致此异常!

因此,当您从诸如数据库之类的源加载自动完成数据时,重要的是确保null在向集合中添加数据时避免/跳过项目。

编辑您的代码如下(注意 if 条件:)if (names[0][i]!=null) ...

public static AutoCompleteStringCollection getSuggestedNames(string namepart)
{
    string query = "SELECT name FROM worker WHERE name LIKE '%"+namepart+"%';";
    string[] namecolumn = { "name" };
    List<string>[] names = getValues(query,namecolumn);
    AutoCompleteStringCollection namec = new AutoCompleteStringCollection();
    for (int i = 0; i < names[0].Count; i++)
        if (names[0][i]!=null) namec.Add(names[0][i]);
    return namec;
}
于 2013-12-08T08:26:54.000 回答
0

我在 ComboBox 中遇到了同样的问题,我在 SelectedIndexChanged 事件期间编辑了收集的项目。堆栈跟踪将是非托管代码,但编辑 ComboBox.items 集合绝对是罪魁祸首。也许编辑 AutocompleteSource 项目会导致您的问题(因为“建议”模式像 ComboBox 一样工作)。

于 2016-09-23T23:18:24.983 回答