Cannot bind web controls to IDataReader in Ent Lib 5

Topics: Data Access Application Block
Jun 7, 2010 at 11:52 AM
Edited Jun 7, 2010 at 12:23 PM

I just started testing Ent Lib 5 data access block, and I can no longer bind a web control to an IDataReader.  I get the following error: Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource

 Code is below.  Using .Net framework 4.  Can anyone shed any light on this?

Dim strSQL As String = "SELECT ..."
Dim db As Database = DatabaseFactory.CreateDatabase()
DropDownList1.DataSource = db.ExecuteReader(db.GetSqlStringCommand(strSQL))
DropDownList1.DataBind()
db=Nothing

Jun 8, 2010 at 6:38 AM

As far as I know you cannot explicitly bind the IDataReader object to a controls like a dropdownlist. I haven't personally tried it in .NET Framework 4.0 and I know there could be more approaches out there though you can try this if it will work for you.  

	   DataTable dt = new DataTable();
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
            DbCommand command = db.GetSqlStringCommand("Select * From Products");

            using (IDataReader reader = db.ExecuteReader(command))
            {
                dt.Load(reader);
            }

            myDDL.DataSource = dt;
            myDDL.DataValueField = "ProductName";
            myDDL.DataBind();

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
 entlib.support@avanade.com

 

Jun 8, 2010 at 11:10 AM

Yes, you could bind the IDataReader to web controls using previous versions of the DAAB. I just tried it in Framework 4.0 with a previous version and that works. The issue seems to be with version 5. I used this type of binding extensively in Framework 3.5 with Ent Lib 4.1. I will next try to use Ent Lib 5 in Framework 3.5.

Michael Keck | Edaptive Systems

400 Red Brook Blvd, Ste 220, Owings Mills, MD 21117

O: 410.327.3366 x103 | C: 443.324.0011 | keckm@edaptivesys.com

The contents of this e-mail and any attachments are intended solely for the use of the named addressee(s) and may contain confidential and/or privileged information. Any unauthorized use, copying, disclosure, or distribution of the contents of this e-mail is strictly prohibited by the sender and may be unlawful. If you are not the intended recipient, please notify the sender immediately and delete this email. Thank you.

Jun 8, 2010 at 11:58 AM

I was under the assumption that you are already using EntLib 5. Anyway, what I have posted above was using Entlib5 in .NET Framework 3.5.

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 8, 2010 at 12:41 PM

I discovered this issue when testing version 5. I just confirmed that the IDataReader can be bound to web controls using Ent Lib 4.1 in both framework 3.5 and 4.0, but when using Ent Lib 5, it fails in both frameworks.

Jun 9, 2010 at 9:03 AM
Edited Jun 9, 2010 at 9:57 AM

This is due to a fix made for a bug in EntLib 3.0.  If you look at the source code for entlib 5.0, the ExecuteReader now wraps first the datareader instance to an instance of RefCountingDataReader.  More info on this can be found in this thread.

So, to sum up the workaround, you would create an extension method like this:

<System.Runtime.CompilerServices.Extension()> _
 Public Function AsDataReader(ByVal reader As IDataReader) As IDataReader
        Return DirectCast(reader, RefCountingDataReader).InnerReader
End Function

Your code now  will look like this:

Dim strSQL As String = "SELECT ..."
Dim db As Database = DatabaseFactory.CreateDatabase() 
DropDownList1.DataSource = db.ExecuteReader(db.GetSqlStringCommand(strSQL)).AsDataReader()
DropDownList1.DataBind()

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
 entlib.support@avanade.com