locked
Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Data.RefCountingDataReader' to type 'System.Data.SqlClient.SqlDataReader'. RRS feed

  • Question

  • User310329504 posted

    I have this function:

        Public Function AssignedDepartmentDetail(ByVal Did As Integer) As SqlDataReader
            Dim reader As SqlDataReader
            Dim Command As SqlCommand = db.GetSqlStringCommand("select seomthing from somewhere where something = @did")
            db.AddInParameter(Command, "@did", Data.DbType.Int32, Did)
            reader = db.ExecuteReader(Command)
            reader.Read()
            Return reader
        End Function
    

    This gives the following error on db.ExecuteReader line:

    Unable to cast object of type 'Microsoft.Practices.EnterpriseLibrary.Data.RefCountingDataReader' to type 'System.Data.SqlClient.SqlDataReader'.

    How do I go about getting this working? Will I always run into problems when dealing with the reader?

    Thursday, May 20, 2010 3:36 AM

All replies

  • User-25924017 posted

    Try using 

    Dim reader as IDataReader reader = db.ExecuteReader(command); 'and then cast it to SqlDataReader

    or

    Dim reader as RefCountingDataReader  = (RefCountingDataReader) db.ExecuteReader(command);

    if (((SqlDataReader)reader.InnerReader).HasRows) //Its C#, please convert

    {

    }



    Thursday, May 20, 2010 4:56 AM
  • User310329504 posted

    I think the later code sample converts to:

    Public Function ExecuteReader(ByVal db As Database) As SqlDataReader
            Dim reader As RefCountingDataReader = DirectCast(db.ExecuteReader(), RefCountingDataReader)
            Return DirectCast(reader.InnerReader, SqlDataReader)
        End Function

    You then get an error:

    DirectCast(db.ExecuteReader(), RefCountingDataReader)

    Overload resolution failed because no accessible 'ExecuteReader' accepts this number of arguements.

    Thursday, May 20, 2010 4:58 AM
  • User-25924017 posted

    Public Function ExecuteReader(ByVal db As Database, ByVal command as SqlCommand) As SqlDataReader
            Dim reader As RefCountingDataReader = DirectCast(db.ExecuteReader(command), RefCountingDataReader)
            Return DirectCast(reader.InnerReader, SqlDataReader)
        End Function

    It needs command parameter. My VB is not good, so adjust.

    Thursday, May 20, 2010 5:09 AM
  • User693495898 posted

    Dear Sir :


    Thanks for your sharing.

    I've meeted the same problem and I found out another article to solve it.

    For your refernce --  http://entlib.codeplex.com/Thread/View.aspx?ThreadId=211288


    This is my Blog for recording this problem.

    http://www.dotblogs.com.tw/mis2000lab/archive/2010/12/17/refcountingdatareader_20101217.aspx


    My sample code is  ----------------------------------------------------------------------------------

    Imports Microsoft.Practices.EnterpriseLibrary.Data   '-- using EntLib 5.0
    Imports System.Data


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim db As Database = DatabaseFactory.CreateDatabase("Your_ConnectionString")

            Dim sqlstr As String = "Select top 10 id,title From test"
            Using I_dr As RefCountingDataReader = CType(db.ExecuteReader(CommandType.Text, sqlstr), RefCountingDataReader)

                GridView1.DataSource = DirectCast(I_dr.InnerReader, System.Data.SqlClient.SqlDataReader)
                GridView1.DataBind()

                '--Thanks these friends help to solve this problem:http://forums.asp.net/p/1560116/3850542.aspx
                '-- http://entlib.codeplex.com/Thread/View.aspx?ThreadId=211288
            End Using

        End Sub

    Thursday, December 16, 2010 10:54 PM