Skip to main content
 首页 » 编程设计

c#之自动完成导致崩溃

2024年02月13日1298°冷暖

我有一个 Windows 窗体应用程序,其中包含一些字段,包括“公司”字段和“联系人”字段。当您在公司字段中键入一个项目并点击一个按钮时,它会查询 SQL 数据库以在“联系人”字段中填写该公司的联系信息。我在“公司”字段中包含了非常基本的自动完成功能,主要是为了方便。

问题是,当我加载表单时,只要我在“公司”字段中键入任何内容,程序就会崩溃。击键时没有其他调用,我将其缩小到自动完成导致问题。

管理这一切的代码如下:

    public void GetRowCount() 
    { 
        try 
        { 
            _DbRows = db.CountRows(); 
            tContact.Text = _DbRows.ToString(); 
        } 
        catch (Exception tEx) 
        { 
            MessageBox.Show("Exception in GetRowCount. Exception: " + tEx.Message); 
        } 
    } 
    private void GetCustomerList() 
    { 
        String customerQuery = "SELECT DISTINCT Name FROM Customers"; 
 
        try 
        { 
            _CustomerList = db.ReturnCustomers(customerQuery, _DbRows); 
        } 
        catch (Exception ex) 
        { 
            MessageBox.Show(ex.Message); 
        } 
    } 
    public void PopulateAutofillList() 
    { 
        try 
        { 
            tCompany.AutoCompleteSource = AutoCompleteSource.CustomSource; 
            tCompany.AutoCompleteCustomSource.AddRange(_CustomerList); 
            MessageBox.Show(_CustomerList.Length.ToString()); 
            tCompany.AutoCompleteMode = AutoCompleteMode.Append; 
        } 
        catch (Exception tEx) 
        { 
            MessageBox.Show("Exception On Autocomplete. Exception: " + tEx.Message); 
        } 
    } 

这些都是在 OnLoad 方法中单独调用的,如下所示:

    private void Form1_Load(object sender, EventArgs e) 
    { 
        try 
        { 
            GetRowCount(); 
            GetCustomerList(); 
            PopulateAutofillList(); 
        } 
        catch (Exception ex) 
        { 
            MessageBox.Show("Initial Connection to the Database Failed."); 
        }  
    } 

然后数据库自己查询:

    public String[] ReturnCustomers(string sqlQuery, int size) 
    { 
        createConnectionString(); 
        StreamWriter file = new StreamWriter("dbCustomerList"); 
        int i = 0; 
        String[] results = new String[size]; 
 
        SqlConnection myConnection = new SqlConnection(_ConnectionString);  
        { 
 
            myConnection.Open(); 
 
            SqlCommand cmd = new SqlCommand(sqlQuery, myConnection);  
            { 
                SqlDataReader reader; 
                reader = cmd.ExecuteReader(); 
 
                while (reader.Read()) 
                { 
                    Console.WriteLine(reader.GetString(0)); 
                    results[i] = reader.GetString(0); 
                    //file.WriteLine(i ": " + results[i]); 
                    i++; 
                }      
                return results; 
            } 
        } 
    } 
    public int CountRows() 
    { 
        createConnectionString(); 
        int rows; 
 
        SqlConnection myConnection = new SqlConnection(_ConnectionString);  
        { 
 
            myConnection.Open(); 
 
            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Customers;", myConnection);  
 
            rows = Convert.ToInt32(cmd.ExecuteScalar()); 
 
            Console.Write("Row Count: " + rows); 
        } 
 
        return rows; 
    } 

我不太确定哪里出了问题。我一路上出现的所有小支票都表明一切正常。为了进行测试,我在 SQLite 上运行了所有这些,一切正常。当我将它移动到 SQL 时它坏了。

--

编辑:

Windows Small Business Server 2011 给出的完整异常:

问题签名: 问题事件名称:APPCRASH

应用程序名称:SSLP.exe

应用程序版本:1.0.0.0

申请时间戳:5213d1b8

故障模块名称:shell32.dll

故障模块版本:6.1.7600.17038

故障模块时间戳:4fd2d370

异常代码:c0000005

异常偏移:000ac2c5

操作系统版本:6.1.7600.2.0.0.305.9

区域设置 ID:1033

附加信息 1:a7aa

附加信息 2:a7aa91f17ea749d42a4de3b390fa5b3d

附加信息 3:a7aa

附加信息 4:a7aa91f17ea749d42a4de3b390fa5b3d

请您参考如下方法:

我遇到了类似的问题 - 从数据库中获取数字列表以用作自动完成源。特别是,我使用了 Linq to SQL 并返回了一个不同的列表。

该不同列表中的一个值是 null,这导致我的程序在我使用自动完成源向文本框中输入值时崩溃。但是,找不到捕获任何异常的方法,因此调试它有点麻烦。

只需在我的 Linq to SQL 查询中添加“where number != null”即可解决我的问题。