I am trying to read an Excelfile with OleDB in C# and put it into a listbox. I followed a lot of guides and it works! However, it keeps showing the data with a delay of 50 milliseconds. This is very small, but I am making this for some colleagues of mine and I want it to work better and even more smooth i.e. reduce the delay. The excel file is not very big, it contains ~840 items so I can imagine it could go faster.
First I will show you my code for filling the data into the listboxes:
const int straatmeubilair = 0;
const int boomproducten = 1;
const int dekkenEnBruggen = 2;
String queryTempCategorie;
private void lbCategorie_SelectedIndexChanged(object sender, EventArgs e)
{
switch (lbCategorie.SelectedIndex)
{
case straatmeubilair:
queryCategorie = "A%";
break;
case boomproducten:
queryCategorie = "B%";
break;
case dekkenEnBruggen:
queryCategorie = "C%";
break;
}
if (queryTempCategorie != queryCategorie)
{
queryTempCategorie = queryCategorie;
updateTable(dbProductInfo.getData("SELECT DISTINCT [Productfamilie] FROM [Bestelinfo$] WHERE [Pagina] LIKE '" + queryCategorie + "' ORDER BY [Productfamilie] ASC"), lbFamilie, "Productfamilie");
}
}
private void updateTable(DataTable tempTable, ListBox tempListbox, String column)
{
tempListbox.DataSource = tempTable;
tempListbox.DisplayMember = column;
}
So when the listbox changes it's index it tries to fill another listbox with a datatable requested from the database. It also has an extra if/else statement to stop it from requesting data when his last request was the same. The request for the data from Excel is shown in the code below.
public DataTable getData(String query)
{
// Test features
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Console.WriteLine(testAmount);
testAmount += 1;
// Clear the datatable
Console.WriteLine("CLEARING DATATABLE");
DataTable data = new DataTable();
// Create the connectionstring
strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + databasePathFile + @";Extended Properties=""Excel 12.0 xml;HDR=Yes;IMEX=1""";
// Create adapter
Console.WriteLine("CREATING ADAPTER");
adapter = new OleDbDataAdapter();
// Create connection
Console.WriteLine("CREATING CONNECTION");
connection = new OleDbConnection(strConnection);
// Create the command for the given connection
Console.WriteLine("CREATING COMMAND");
command = new OleDbCommand(query, connection);
try
{
// Open the connection
connection.Open();
// Give the command to the adapter
adapter.SelectCommand = command;
// Fill the dateset with the adapter
adapter.Fill(data);
}
catch (Exception e)
{
MessageBox.Show("Something went wrong, contact IT");
Console.WriteLine(e.Message);
}
finally
{
// Close connection
connection.Close();
// Dispose of adapter
adapter.Dispose();
TimeSpan ts = stopwatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
Console.WriteLine("Runtime " + elapsedTime);
}
return data;
}
Note: The "databasePathFile" at the connectionstring is defined earlier in the program.
I think I'm doing something weird that it keeps giving me a delay. Does anyone have any suggestions? (Something like: "You need to change your method completely" is also ok :P I'm still learning! However I would like to keep this method.