Professional Web Applications Themes

HELP: Custom DataReader Function - Tricky issue - ASP.NET General

I have a function that returns a datareader. The problem is that in the function's FINALLY I close the datareader and set it to nothing. But, BEFORE the Finally I "Return" the datareader (in the Try). The problem is, by the time it's "returned" it says I already closed it. Any ideas how I can overcome this? How can I clean up the objects nicely WHILE returning it back to the caller???? Example: Private Sub x Dim dr as SqlDataReader .... .... dr = GetDr(...) ' FAILS BECAUSE THE DATAREADER IS CLOSED BY NOW! .... End Sub Private Function GetDr(...) ...

  1. #1

    Default HELP: Custom DataReader Function - Tricky issue

    I have a function that returns a datareader. The problem is that in the
    function's FINALLY I close the datareader and set it to nothing. But,
    BEFORE the Finally I "Return" the datareader (in the Try).

    The problem is, by the time it's "returned" it says I already closed it. Any
    ideas how I can overcome this? How can I clean up the objects nicely WHILE
    returning it back to the caller????

    Example:

    Private Sub x
    Dim dr as SqlDataReader
    ....
    ....
    dr = GetDr(...) ' FAILS BECAUSE THE DATAREADER IS CLOSED BY NOW!
    ....
    End Sub

    Private Function GetDr(...) as SqlDataReader
    Dim MyDataReader as SqlDataReader
    Try
    .....
    ....
    Return MyDataReader
    Catch
    Finally
    MyDataReader.Close
    MyDataReader = Nothing
    End Function



    VB Programmer Guest

  2. #2

    Default Re: Custom DataReader Function - Tricky issue

    You can't. That's why you don't want to be returning datareaders. Because
    then you are relying on the client to close the datareader and the
    underlying connection.

    Use a datatable or dataset instead.

    "VB Programmer" <growNO-SPAMgo-intech.com> wrote in message
    news:uiF0PTQYDHA.2620TK2MSFTNGP09.phx.gbl...
    > I have a function that returns a datareader. The problem is that in the
    > function's FINALLY I close the datareader and set it to nothing. But,
    > BEFORE the Finally I "Return" the datareader (in the Try).
    >
    > The problem is, by the time it's "returned" it says I already closed it.
    Any
    > ideas how I can overcome this? How can I clean up the objects nicely
    WHILE
    > returning it back to the caller????
    >
    > Example:
    >
    > Private Sub x
    > Dim dr as SqlDataReader
    > ....
    > ....
    > dr = GetDr(...) ' FAILS BECAUSE THE DATAREADER IS CLOSED BY NOW!
    > ....
    > End Sub
    >
    > Private Function GetDr(...) as SqlDataReader
    > Dim MyDataReader as SqlDataReader
    > Try
    > .....
    > ....
    > Return MyDataReader
    > Catch
    > Finally
    > MyDataReader.Close
    > MyDataReader = Nothing
    > End Function
    >
    >
    >

    Marina Guest

  3. #3

    Default Re: Custom DataReader Function - Tricky issue

    "VB Programmer" <com> wrote in message
    news:phx.gbl... 
    Any 
    WHILE 

    Why are you returning it to the caller? So the caller can use it? But he
    can't use it if it's closed!

    And, btw, you don't need to set it to Nothing! It's a total, worthless,
    waste of time and energy, so stop doing it! The variable itself is going to
    disappear w hen you exit the function!
    --
    John Saunders
    Internet Engineer
    com



    John Guest

  4. #4

    Default Re: Custom DataReader Function - Tricky issue

    Don't close the datareader in the GetDr function. Close the datareader in
    the Sub X when you finish reading it.


    "VB Programmer" <com> wrote in message
    news:phx.gbl... 
    Any 
    WHILE 


    JohnG Guest

  5. #5

    Default Re: Custom DataReader Function - Tricky issue

    Hi,

    1) As others say donít close the dataReader on the "server" side let the
    "client" do the work just use the CommandBehavior to set that the
    connection will be closed when the datareader will be closed:

    System.Data.SqlClient.SqlDataReader = oc.ExecuteReader
    (System.Data.CommandBehavior.CloseConnection );

    2) I personally prefer Data Access Layer (DAL) classes create by
    programmers or any ORM tool over datasets. I wonít recommend to switch
    to DataSet or to return DataReader to presentation layer (since both of
    them holds database structure). You can create your own data structure
    fill it by iterating readerReader and return the data structure to the
    caller.

    Natty Gur, CTO
    Dao2Com Ltd.
    34th Elkalay st. Raanana
    Israel , 43000
    Phone Numbers:
    Office: +972-(0)9-7740261
    Fax: +972-(0)9-7740261
    Mobile: +972-(0)58-888377


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Natty Guest

Similar Threads

  1. Custom tag <cfx_image> issue
    By SilverStrike in forum Macromedia ColdFusion
    Replies: 0
    Last Post: May 29th, 05:33 PM
  2. Custom Tag and thisTag.AssocAttribs issue
    By MindChild in forum Macromedia ColdFusion
    Replies: 0
    Last Post: May 9th, 02:15 PM
  3. Need Custom DataReader Loop in User Control: inline vs. code-behind
    By Jordan in forum ASP.NET Building Controls
    Replies: 1
    Last Post: November 10th, 07:15 PM
  4. Custom Web Control Deployment Issue
    By rick in forum ASP.NET Building Controls
    Replies: 0
    Last Post: December 3rd, 12:06 AM
  5. Custom control issue: Where do I put this code?
    By John Saunders in forum ASP.NET Building Controls
    Replies: 1
    Last Post: August 7th, 02:26 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139