Sandip
Sandip

Reputation: 481

Unable to cast object of type 'Npgsql.Forward Only DataReader' to type 'Object'

I am getting following error when trying to insert data into list.

"Unable to cast object of type 'Npgsql.Forward Only DataReader' to type 'object'"

here is my code.RDLCReportBase is my base class used to inherit in another class.

public List<RDLCReportBase> GetList(RDLCReportBase reportbase,string )
{
                List<RDLCReportBase> list = new List<RDLCReportBase>();

                Database.AddInParameter(reportbase.command, "culturecode", NpgsqlDbType.Varchar, cultureCode);

                using (IDataReader rdr = Database.ExecuteReader(reportbase.command))
                {
                    while (rdr.Read())
                    {                    
                        list.Add((RDLCReportBase)rdr);
                    }
                }


                return list;
}

anybody knows how to solve this issue ?

Thanks

Upvotes: 0

Views: 1236

Answers (1)

Deffiss
Deffiss

Reputation: 1136

I think your problem in this line:

list.Add((RDLCReportBase)rdr);

You can't cast IDataReader directly to your classes. Instead IDataReader helps you to access columns of the current row through the indexers and methods so you may process all received data row by row. To go to the next row you use rdr.Read(). So your code should looks like this (of course, you must replace properties and column names with your own):

using (IDataReader rdr = Database.ExecuteReader(reportbase.command))
{
    while (rdr.Read())
    {                    
        var value = new RDLCReportBase
            {
                Property1 = rdr.GetInt32(0), // access zero column value wich is type of int
                Property2 = (string)rdr["Column2"], // access column value with name Column2 wich is type of string
                // and so on...
            };
        list.Add(value);
    }
}

Upvotes: 1

Related Questions