same code, different providers => different behaviours??

Discussion in 'ASP .Net' started by Bart, Mar 22, 2007.

  1. Bart

    Bart Guest

    Hi,

    I ran the same code with two different providers (oledb abd sqlclient), and
    i got two different behaviours.
    The code with OLEDB runs perfect without error.
    The same code with SQLClient gives an error at line: "dtreader =
    comd.ExecuteReader" (the second)
    "There is already an open DataReader associated with this Command which must
    be closed first."

    Why must the DataReader be closed with provider SqlClient and not with
    Oledb?
    Any explanation for that?

    Thanks
    Bart

    1) with OleDb:
    -------------
    Imports System.Data.OleDb
    Partial Class studalres
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim oConnection As OleDbConnection
    Dim comd As OleDbCommand
    Dim dtreader As OleDbDataReader
    Dim sql As String

    oConnection = New OleDbConnection()
    oConnection.ConnectionString =
    System.Configuration.ConfigurationManager.ConnectionStrings("oledbdemo").ToString()
    oConnection.Open()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    'dtreader.Close()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New OleDbCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    dtreader.Close()
    oConnection.Close()
    End Sub
    End Class

    2) with SqlClient :
    ----------------
    Imports System.Data
    Imports System.Data.sqlclient
    Partial Class studalres
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim oConnection As SqlConnection
    Dim comd As SqlCommand
    Dim dtreader As SqlDataReader
    Dim sql As String

    oConnection = New SqlConnection()
    oConnection.ConnectionString =
    System.Configuration.ConfigurationManager.ConnectionStrings("sqlclientdemo").ToString()
    oConnection.Open()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    'dtreader.Close()

    sql = "SELECT pc.naam FROM pc ;"
    comd = New SqlCommand(sql, oConnection)
    dtreader = comd.ExecuteReader
    'here is the error
    dtreader.Close()
    oConnection.Close()
    End Sub
    End Class
    Bart, Mar 22, 2007
    #1
    1. Advertising

  2. Each reader needs a connection, and the provider may either open a new
    connection in the background or refuse to run the query. This can be
    controlled from a setting in the connection string, and the default
    value of this setting is obviously different for these providers.

    Bart wrote:
    > Hi,
    >
    > I ran the same code with two different providers (oledb abd sqlclient), and
    > i got two different behaviours.
    > The code with OLEDB runs perfect without error.
    > The same code with SQLClient gives an error at line: "dtreader =
    > comd.ExecuteReader" (the second)
    > "There is already an open DataReader associated with this Command which must
    > be closed first."
    >
    > Why must the DataReader be closed with provider SqlClient and not with
    > Oledb?
    > Any explanation for that?
    >
    > Thanks
    > Bart
    >
    > 1) with OleDb:
    > -------------
    > Imports System.Data.OleDb
    > Partial Class studalres
    > Inherits System.Web.UI.Page
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim oConnection As OleDbConnection
    > Dim comd As OleDbCommand
    > Dim dtreader As OleDbDataReader
    > Dim sql As String
    >
    > oConnection = New OleDbConnection()
    > oConnection.ConnectionString =
    > System.Configuration.ConfigurationManager.ConnectionStrings("oledbdemo").ToString()
    > oConnection.Open()
    >
    > sql = "SELECT pc.naam FROM pc ;"
    > comd = New OleDbCommand(sql, oConnection)
    > dtreader = comd.ExecuteReader
    > 'dtreader.Close()
    >
    > sql = "SELECT pc.naam FROM pc ;"
    > comd = New OleDbCommand(sql, oConnection)
    > dtreader = comd.ExecuteReader
    > dtreader.Close()
    > oConnection.Close()
    > End Sub
    > End Class
    >
    > 2) with SqlClient :
    > ----------------
    > Imports System.Data
    > Imports System.Data.sqlclient
    > Partial Class studalres
    > Inherits System.Web.UI.Page
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim oConnection As SqlConnection
    > Dim comd As SqlCommand
    > Dim dtreader As SqlDataReader
    > Dim sql As String
    >
    > oConnection = New SqlConnection()
    > oConnection.ConnectionString =
    > System.Configuration.ConfigurationManager.ConnectionStrings("sqlclientdemo").ToString()
    > oConnection.Open()
    >
    > sql = "SELECT pc.naam FROM pc ;"
    > comd = New SqlCommand(sql, oConnection)
    > dtreader = comd.ExecuteReader
    > 'dtreader.Close()
    >
    > sql = "SELECT pc.naam FROM pc ;"
    > comd = New SqlCommand(sql, oConnection)
    > dtreader = comd.ExecuteReader
    > 'here is the error
    > dtreader.Close()
    > oConnection.Close()
    > End Sub
    > End Class
    >


    --
    Göran Andersson
    _____
    http://www.guffa.com
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Mar 22, 2007
    #2
    1. Advertising

  3. Bart

    Bart Guest

    Thanks

    "Göran Andersson" <> schreef in bericht
    news:...
    > Each reader needs a connection, and the provider may either open a new
    > connection in the background or refuse to run the query. This can be
    > controlled from a setting in the connection string, and the default value
    > of this setting is obviously different for these providers.
    >
    > Bart wrote:
    >> Hi,
    >>
    >> I ran the same code with two different providers (oledb abd sqlclient),
    >> and i got two different behaviours.
    >> The code with OLEDB runs perfect without error.
    >> The same code with SQLClient gives an error at line: "dtreader =
    >> comd.ExecuteReader" (the second)
    >> "There is already an open DataReader associated with this Command which
    >> must be closed first."
    >>
    >> Why must the DataReader be closed with provider SqlClient and not with
    >> Oledb?
    >> Any explanation for that?
    >>
    >> Thanks
    >> Bart
    >>
    >> 1) with OleDb:
    >> -------------
    >> Imports System.Data.OleDb
    >> Partial Class studalres
    >> Inherits System.Web.UI.Page
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim oConnection As OleDbConnection
    >> Dim comd As OleDbCommand
    >> Dim dtreader As OleDbDataReader
    >> Dim sql As String
    >>
    >> oConnection = New OleDbConnection()
    >> oConnection.ConnectionString =
    >> System.Configuration.ConfigurationManager.ConnectionStrings("oledbdemo").ToString()
    >> oConnection.Open()
    >>
    >> sql = "SELECT pc.naam FROM pc ;"
    >> comd = New OleDbCommand(sql, oConnection)
    >> dtreader = comd.ExecuteReader
    >> 'dtreader.Close()
    >>
    >> sql = "SELECT pc.naam FROM pc ;"
    >> comd = New OleDbCommand(sql, oConnection)
    >> dtreader = comd.ExecuteReader
    >> dtreader.Close()
    >> oConnection.Close()
    >> End Sub
    >> End Class
    >>
    >> 2) with SqlClient :
    >> ----------------
    >> Imports System.Data
    >> Imports System.Data.sqlclient
    >> Partial Class studalres
    >> Inherits System.Web.UI.Page
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Me.Load
    >> Dim oConnection As SqlConnection
    >> Dim comd As SqlCommand
    >> Dim dtreader As SqlDataReader
    >> Dim sql As String
    >>
    >> oConnection = New SqlConnection()
    >> oConnection.ConnectionString =
    >> System.Configuration.ConfigurationManager.ConnectionStrings("sqlclientdemo").ToString()
    >> oConnection.Open()
    >>
    >> sql = "SELECT pc.naam FROM pc ;"
    >> comd = New SqlCommand(sql, oConnection)
    >> dtreader = comd.ExecuteReader
    >> 'dtreader.Close()
    >>
    >> sql = "SELECT pc.naam FROM pc ;"
    >> comd = New SqlCommand(sql, oConnection)
    >> dtreader = comd.ExecuteReader 'here is the error
    >> dtreader.Close()
    >> oConnection.Close()
    >> End Sub
    >> End Class
    >>

    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    Bart, Mar 22, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bluebaron
    Replies:
    3
    Views:
    735
    Jonathan N. Little
    Nov 4, 2009
  2. Michael Tsang
    Replies:
    32
    Views:
    1,102
    Richard Bos
    Mar 1, 2010
  3. Michael Tsang
    Replies:
    54
    Views:
    1,189
    Phil Carmody
    Mar 30, 2010
  4. Dave
    Replies:
    4
    Views:
    82
    Lasse Reichstein Nielsen
    Sep 5, 2003
  5. Yves-Alain NICOLLET

    Different behaviours when script included or called

    Yves-Alain NICOLLET, Apr 23, 2004, in forum: Javascript
    Replies:
    5
    Views:
    95
    Thomas 'PointedEars' Lahn
    May 3, 2004
Loading...

Share This Page