Home > Back-end >  C# Error : Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSo
C# Error : Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSo

Time:11-01

I update my project Microsoft.Practices.EnterpriseLibrary.Data.dll version 4.0.0.0 to 5.0.414.0. Then I got this error. Previously it worked without issue.

Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.

                this.CategoryDropDownList.DataTextField = "CAT_NAME";
                this.CategoryDropDownList.DataValueField = "CAT_ID";
                this.CategoryDropDownList.DataSource = mgr.GetCategories();// here error occurred 
                this.CategoryDropDownList.DataBind();

Inside GetCategories method -

        public IDataReader GetCategories()
        {
            IDataReader reader = null;

            using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
            {
                reader = MyDB.ExecuteReader(cmd);
            }

            return reader;
        }

ExecuteReaderalways return IDataReader object.

I tried to cast IDataReader to 'IDataSource'.but it gives me an error like this.

Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Data.RefCountingDataReader' to type 'System.Web.UI.IDataSource'.

        public IDataSource GetCategories()
        {
            IDataSource reader = null;

            using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
            {
                reader = (IDataSource)MyDB.ExecuteReader(cmd);
            }

            return reader;
        }

Please provide me a solution. Thank you

CodePudding user response:

The advice from @jmcilhinney might work, especially if this code worked before the update:

public IDataSource GetCategories()
{
    IDataSource reader = null;

    using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
    {
        reader = (IDataSource)MyDB.ExecuteReader(cmd).InnerReader;
    }

    return reader;
}

But I have no environment that old to test with (And I have some really old code bases hanging around). Instead of executing a Reader, you can return a DataSet from the command using ExecuteDataSet, the DataSet then has an array of DataTable that you can return the first element from:

public IDataSource GetCategories()
{
    DataTable table = null;

    using (var cmd = MyDB.GetSqlStringCommand(MySqlScriptor.GetSql("QueryCategories")))
    {
        table = MyDB.ExecuteDataSet(cmd).Tables[0];
    }

    return table;
}
  • Related