I have a Windows Form app with a handful of fields, including a 'Company' Field and a 'Contact' Field. When you type an item in the company field and hit a button, it makes a query to a SQL database to fill in the contact information for that company in the 'Contact' field. I included really basic autocomplete in the 'Company' field, mostly for convenience.
Problem is that when I load up the form, as soon as I type anything into the 'Company' field, the program crashes. There are no other calls being made on a keystroke and I narrowed it down to autocomplete causing the problems.
The code that manages it all is as follows:
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);
}
}
These are all called separately in a OnLoad Method, like so:
private void Form1_Load(object sender, EventArgs e)
{
try
{
GetRowCount();
GetCustomerList();
PopulateAutofillList();
}
catch (Exception ex)
{
MessageBox.Show("Initial Connection to the Database Failed.");
}
}
And the DB queries themselves:
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;
}
I'm not totally sure what's broken. All my little checks that show up along the way indicate that things are right. For testing, I had all this running on SQLite and it was fine. It broke when I moved it to SQL.
--
Edit:
The full exception that Windows Small Business Server 2011 gives:
Problem signature: Problem Event Name: APPCRASH
Application Name: SSLP.exe
Application Version: 1.0.0.0
Application Timestamp: 5213d1b8
Fault Module Name: shell32.dll
Fault Module Version: 6.1.7600.17038
Fault Module Timestamp: 4fd2d370
Exception Code: c0000005
Exception Offset: 000ac2c5
OS Version: 6.1.7600.2.0.0.305.9
Locale ID: 1033
Additional Information 1: a7aa
Additional Information 2: a7aa91f17ea749d42a4de3b390fa5b3d
Additional Information 3: a7aa
Additional Information 4: a7aa91f17ea749d42a4de3b390fa5b3d