0

I am using the below syntax in my webform .net 4.0 project. However, once it hits the dropdown1.DataSource = tasks[0].Result.Tables[0]; I get a NullReferenceException error.

I know generally this error is thrown because a proper assignment was not made, however, in my syntax it looks to me like I made all applicable assignments needed. What do I need to alter/change/modify in my syntax below in order to have the drop down populated appropriately?

namespace NETAsyncLowVersion
{
public partial class WebformLVAsync : System.Web.UI.UserControl
{
    private class1 _class1 = new NC1();
    private DataSet DS = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Task.Factory.ContinueWhenAll(new[]{ SqlQuery1() }, tasks =>
            {
                try 
                {
                    dropdown1.DataSource = tasks[0].Result.Tables[0];
                }
                catch (Exception exception) { throw exception; }
            }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
    private void SqlQuery1()
    {
        dropdown1.DataSource = _class1.SqlQuery1(databaseconn);
    }

public class class1
{
    private DataSet DS = new DataSet();
    private Databaselayer _Databaselayer = new Databaselayer();

    public DataSet SqlQuery1(string databaseConnection)
    {
        DS = new DataSet();
        _Databaselayer.SqlQueryBuilder = new StringBuilder();
        _Databaselayer.SqlQueryBuilder.Append("Select managername from salesdatabase");
        return _Databaselayer.FDS(databaseConnection, _Databaselayer.SqlQueryBuilder.ToString());
    }           
}
public class Databaselayer
{
    public System.Threading.Tasks.Task<DataSet> FDS(string connectionString, string sqlQuery)
    {
        return System.Threading.Tasks.Task.Factory.StartNew(() =>
        {
            var dataSet = new DataSet();
            using (var adapter = new SqlDataAdapter(sqlQuery, connectionString))
                adapter.Fill(dataSet);

            return dataSet;
        });
    }
}
}    

1 Answers1

0

I talked about this in another question: Use 1 DataReader For Multiple Database Calls. In your case, you're in a user control, then you need to call Page.RegisterAsyncTask method instead of simply RegisterAsyncTask

Community
  • 1
  • 1