I created an application which loads data into a ListView by executing a procedure that is stored in SQL Server. The procedure takes some time to execute, when I click the button on application to execute the procedure my application just freeze until the data is loaded in the ListView.
How can I create a splash screen that loads when I press the button until the data is populated in the List View?
I appreciate any feedback.
public async Task<ListView> GetDataAsync()
{
var dt = new ListView();
//var cn = @"Your Connection String";
//var cmd = @"SELECT * FROM Category";
//var da = new SqlDataAdapter(cmd, cn);
SqlConnection conn = new SqlConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("[Loyalty_Swipe]", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
SqlDataReader reader;
cmd.Parameters.AddWithValue("@StartDate", txtStartDate.Text);
cmd.Parameters.AddWithValue("@EndDate", txtEndDate.Text);
cmd.Parameters.AddWithValue("@CustomerID", txtCustID.Text);
reader = cmd.ExecuteReader();
metroListView1.Items.Clear();
await Task.Run(() =>
{
while (reader.Read())
{
metroListView1.Columns[0].Text = "Date";
metroListView1.Columns[1].Text = "Store Code";
ListViewItem listview = new ListViewItem(reader["Date"].ToString());
listview.SubItems.Add(reader["StoreCode"].ToString());
listview.SubItems.Add(reader["StoreName"].ToString());
listview.SubItems.Add(reader["TillNumber"].ToString());
listview.SubItems.Add(reader["TranNumber"].ToString());
listview.SubItems.Add(reader["Amount"].ToString());
listview.SubItems.Add(reader["Discount"].ToString());
listview.SubItems.Add(reader["ProcessedFlag"].ToString());
listview.SubItems.Add(reader["Processed Date"].ToString());
metroListView1.Items.Add(listview);
};
});
conn.Close();
pbLoad.Hide();
btnExport.Enabled = true;
return dt;
}