Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am have a litle problems firing my datagridview using the async / await method.

Button click event:
C#
private async void button2_Click(object sender, EventArgs e)
        {
            datagridview1.Visible = true;
            //Progressbar  
            Progressbar1  .Visible = true;
            Progressbar1  .Style = ProgressBarStyle.Marquee;
            var period2 = cb1.SelectedValue.ToString();
            var period1 = cb2.SelectedValue.ToString();
           

            /*A variable to hold the parameters*/
            //var table = await loadTable((period1, period2));

            await Task.Run(() => loadTable(period1, period2));

             /*back to the UI */
            //datagridview1.DataSource = datatable;
            
            //datagridview1.DataSource = table;

            Progressbar1  .Visible = false;
        }


I haved tried fetching the DGV by using the using the two // datagridview.DataSource line without success.

My Task method:

C#
 private async Task Zugänge(string period1, string period2) 

{

    //
    string C = ConfigurationManager.ConnectionStrings["123"].ConnectionString;
    using (var con = new SqlConnection(C))
    using (var cmd = new SqlCommand())
    {
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spInfo]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Periode2", period2);
        cmd.Parameters.AddWithValue("@Periode1", period1);
        // open the connection
        con.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable );
       
        await Task.Run(() => adapter.Fill(datatable ));
        
    }
}
Posted
Comments
Afzaal Ahmad Zeeshan 17-Jun-15 4:19am    
What is the failure report? Can you share why you believe this code isn't working?

Also remember, async/await operators just allow you to use the Asynchronous programming model in your application. Otherwise, they are of no use to work with graphics.
mikybrain1 17-Jun-15 4:36am    
Ok. If i use the line var table = await loadTable((period1, period2)); and set it to the DGV it underline table saying "void" can not implicitly typed local variables are allocated.

And if I use this line: datagridview1.DataSource = datatable; it states the name database does not exist in the current context.
Afzaal Ahmad Zeeshan 17-Jun-15 4:40am    
It would state, datatable does not exist in the current context, there is no identifier database being used here.

Also, void cannot be saved to a variable value. loadTable might be returning a void (signature might be, private async void loadTable), thus it does not allow you.
mikybrain1 17-Jun-15 4:54am    
Thnx to CHill60 who make me think further even when I#m desperated.

1 solution

I added a return to end of my Task method:

private async Task Zugänge(string period1, string period2) 
 
{
 
    //
    string C = ConfigurationManager.ConnectionStrings["123"].ConnectionString;
    using (var con = new SqlConnection(C))
    using (var cmd = new SqlCommand())
    {
        cmd.Connection = con;
        cmd.CommandText = ("[dbo].[spInfo]");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Periode2", period2);
        cmd.Parameters.AddWithValue("@Periode1", period1);
        // open the connection
 // Here open the connection async:
        await con.OpenAsync();
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable );
       
        await Task.Run(() => adapter.Fill(datatable ));
return dt;
        
    }
}
 
Share this answer
 
v2
Comments
George Swan 17-Sep-16 1:46am    
Are you sure this solution is correct? You are calling the same adapter.Fill method twice in the last 3 lines of your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900