0

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;
        }
  • 2
    Winforms? WPF? tags would help. [Googling would help also](http://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application) – Hank Oct 12 '16 at 18:47
  • You are looking for a BackgroundWorker and a ProgressBar with marquee style. – LarsTech Oct 12 '16 at 18:49
  • Yes Winforms. I did google but couldnt fing anything relevant to my situation. – Dylan Davids Oct 13 '16 at 05:22
  • [Animation load from thread, when the form is frozen](http://stackoverflow.com/a/39142535/3110834) – Reza Aghaei Oct 13 '16 at 06:39
  • Animation load looks plausible, I will try it out and provide feedback. – Dylan Davids Oct 13 '16 at 06:55
  • GetDataAsync() does not pickup in my application. please note i am busy with desktop application not mobile can you please advise. – Dylan Davids Oct 13 '16 at 09:06
  • @DylanDavids When posting a comment, mention the user using `@` character before its name, otherwise they wont be notified of your comment. In the linked post, the `GetDataAsync` is a method which I created myself and is included in answer. In the method I load data asynchronously. Let me know if you have any question about the answer. By the way, using `DataGridView` is really easier than `ListView`. – Reza Aghaei Oct 13 '16 at 19:36
  • @RezaAghaei thanks for the tip I am new here so please forgive me. Thanks for clarifying on GetDataAsync, I will try using DataGridView and let you know if i have any more problems. – Dylan Davids Oct 14 '16 at 07:27
  • @RezaAghaei I tried with listview and cant get it working. I would really like to use Listview rather than DataGridView. Would you please assist me in designing your solution for a ListView? – Dylan Davids Oct 14 '16 at 09:13
  • The example I shared is really really simple and straight forward, so I can't share an easier code, But if you share your code which show how you are using my sample code, I'll help you to solve the problem. – Reza Aghaei Oct 14 '16 at 10:46
  • @RezaAghaei I have added the code please have a look and let me know. – Dylan Davids Oct 14 '16 at 12:32
  • @RezaAghaei The only problem i get is when i click the button to execute the procedure it says: Cross-thread operation not valid: Control 'metroListView1' accessed from a thread other than the thread it was created on. I took out the connection string as it is confidential. – Dylan Davids Oct 14 '16 at 12:54
  • You should not put the code of filling list view in `Task.Run` you will receive an exception and if to remove the exception use `Invoke`, then your UI thread will be busy again and your window will be frozen. Just follow the sample I provided. `GetDataAsync` should just get data. Then after getting data, add data table rows to listview. The only change which you need to make in the sample code is replacing `dataGridView1.DataSource = ...` with a for loop which you use to add items to listview. – Reza Aghaei Oct 14 '16 at 12:58
  • @RezaAghaei Thanks for the update. I cant put SqlCommand in SqlDataAdapter the same goes for the SqlConnection, To populate the ListView I need SqlDataReader and this forces me to you SqlCommand. Can you please provide me with code as I am lost. – Dylan Davids Oct 14 '16 at 13:30
  • You are not forced to use data reader. Use data adapter and fill a data table then use a for loop on datatable.rows to add items to list view. I can't post a code for this requirement because it's realy a duplicate of the linked post. try what I said and surely you can solve the problem :) – Reza Aghaei Oct 14 '16 at 13:46

0 Answers0