Results 1 to 6 of 6

Thread: DataReader is Closed! Why?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    DataReader is Closed! Why?

    This function call to a custom class returns a SqlDataReader but why do I get an error saying that MyReader is closed? I have verified that the reader contains data before I exit the function call. Am I doing something wrong in terms of creating a reader object to accept the return?

    Code:
    Dim MyReader As SqlClient.SqlDataReader = GroupManager.GetAnnouncementTypesForGroupName(strGroupName)

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Whats the code in GroupManager.GetAnnouncementTypesForGroupName(strGroupName)?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    I just figured it out. I am closing the connection in the Function so all I had to do was close the connection after I use the Reader!

    lol

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Code:
    Namespace GroupManager
    
        Public Class Groups
    
            Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("SQLConn"))
            Dim reader As SqlDataReader
    
            Public Sub CloseConnection()
                conn.Close()
            End Sub
    
            Public Function GetAnnouncementTypesForGroupName(ByVal strCookieValue As String) As SqlDataReader
                Try
                    Dim cmd As New SqlCommand("GetAnnouncementTypesForGroupName", conn)
                    'add parameter
                    cmd.Parameters.Add("@GroupName", SqlDbType.VarChar, 50)
                    cmd.CommandType = CommandType.StoredProcedure
                    conn.Open()
                    'set the parameter
                    cmd.Parameters("@GroupName").Value = strCookieValue.ToLower
                    'execute the reader
                    reader = cmd.ExecuteReader
                    Return reader
                Finally
    
                End Try
            End Function
    
    etc.......

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As a note , this would have saved you time creating close sub !
    It closes the connection right after command execution .
    VB Code:
    1. cmd.ExecuteReader(CommandBehavior.CloseConnection)

    Just a thought ...

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216
    Originally posted by Pirate
    As a note , this would have saved you time creating close sub !
    It closes the connection right after command execution .
    VB Code:
    1. cmd.ExecuteReader(CommandBehavior.CloseConnection)

    Just a thought ...
    Never knew you could do that but in my case I need to leave it open while the reader is being used by the caller then the caller calls the close ub.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width