HELP: Custom DataReader Function - Tricky issue

Ask a Question related to ASP.NET General, Design and Development.

  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. Similar Questions and Discussions

    1. Custom tag <cfx_image> issue
      I have a problem with the <cfx_image> custom tag, when resizing an image of type jpeg. The image becomes almost completely black. I will be glad...
    2. Custom Tag and thisTag.AssocAttribs issue
      Hi all, What I am trying to do is very simple. I am building a couple of custom tags (we will call cf_ParentTag and cf_ChildTag). The parent tag...
    3. Need Custom DataReader Loop in User Control: inline vs. code-behind
      I'm trying to evaluate the benefits of designing user controls completely inline vs. using a code-behind file. I already have a user control...
    4. Custom control viewstate (?) issue!
      I've been struggling for a day and yet haven't found a solution to such a problem: I have, let's say, an apsx page, which contains a UserControl,...
    5. Custom Web Control Deployment Issue
      I have a custom web control in which I have created a setup project in the same solution. In the setup project, I have created a custom folder in...
  3. #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-SPAM@go-intech.com> wrote in message
    news:uiF0PTQYDHA.2620@TK2MSFTNGP09.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

Posting Permissions

  • You may not post new threads
  • You may 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