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;
        });
    }
}
}    
4
  • Where did yo got tasks[0]? Make sure it exists and also check if it's not null Commented Mar 14, 2016 at 1:53
  • @Saleem - to my understanding isn't that the Task<DataSet> that I am returning from my DatabaseLayer class? I know the sqlQuery returns results if I run it directly in SQL Server. Commented Mar 14, 2016 at 1:58
  • Your code is not complete: you cannot cast System.Threading.Tasks.Task<DataSet> to DataSet Commented Mar 14, 2016 at 5:28
  • @Jeroen - so the issue stems from my Databaselayer where I use System.Threading.Tasks.Task<DataSet> but try to return a dataSet? Commented Mar 14, 2016 at 12:15

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@NadineSmithJonesPicard Has you added the extension method's class in your codebase?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.