Passing SQLDataReader through a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James P.

    Passing SQLDataReader through a function

    I'm trying to pass a sqlDataReader from a called function to a calling
    function but got nothing back and a message saying "An unhandled
    exception of type 'System.NullRef erenceException ' occurred in
    Throw.exe. Object reference not set to an instance of an object."

    Here is my code:
    Public Function testint() As String
    'Nothing returns here, plus the error above
    Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")
    End Function
    Public Function getStoreProcedu res() As SqlDataReader
    Dim sp As SqlDataReader =
    CCMSDB.DBDataRe ader("procSelec tBatchReports")
    'This sp.Read returns rows displayed in the message box fine.
    While sp.Read
    MsgBox(sp("Stor eProcedure"))
    End While
    'This code works fine until here
    getStoreProcedu res = sp
    'After this, when it returns to Testint, it will blow up
    end function

    Can anyone help please? I'm new to VB.NET. Thanks in advance,
    James
  • Fergus Cooney

    #2
    Re: Passing SQLDataReader through a function

    Hi James,

    There doesn't seem to be anything wrong with the code that you have shown,
    except that it isn't the code that you are using!!

    One getStoreProcedu res takes a parameter.
    Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")

    The other doesn't
    Public Function getStoreProcedu res() As SqlDataReader

    This isn't the cause of the Exception, but it may be the cause of me not
    being able to see what is!!

    Regards,
    Fergus





    Comment

    • Mat

      #3
      Re: Passing SQLDataReader through a function

      try
      Return sp
      instead of getstoreprocedu ces=sp

      "James P." <jpham@sandsexp o.com> wrote in message
      news:bd779bc2.0 310031454.6f7b0 3b1@posting.goo gle.com...[color=blue]
      > I'm trying to pass a sqlDataReader from a called function to a calling
      > function but got nothing back and a message saying "An unhandled
      > exception of type 'System.NullRef erenceException ' occurred in
      > Throw.exe. Object reference not set to an instance of an object."
      >
      > Here is my code:
      > Public Function testint() As String
      > 'Nothing returns here, plus the error above
      > Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")
      > End Function
      > Public Function getStoreProcedu res() As SqlDataReader
      > Dim sp As SqlDataReader =
      > CCMSDB.DBDataRe ader("procSelec tBatchReports")
      > 'This sp.Read returns rows displayed in the message box fine.
      > While sp.Read
      > MsgBox(sp("Stor eProcedure"))
      > End While
      > 'This code works fine until here
      > getStoreProcedu res = sp
      > 'After this, when it returns to Testint, it will blow up
      > end function
      >
      > Can anyone help please? I'm new to VB.NET. Thanks in advance,
      > James[/color]


      Comment

      • Fergus Cooney

        #4
        Re: Passing SQLDataReader through a function

        Hi Mat,

        For backwards compatibility (I believe), assignment to the Function name
        is a legitimate way to return a value

        Function Foo As String
        Foo = "I'm going places!!"
        End Function

        Regards,
        Fergus


        Comment

        • James P.

          #5
          Re: Passing SQLDataReader through a function

          "Fergus Cooney" <filter-1@tesco.net> wrote in message news:<uDaltlgiD HA.1864@TK2MSFT NGP10.phx.gbl>. ..[color=blue]
          > Hi James,
          >
          > There doesn't seem to be anything wrong with the code that you have shown,
          > except that it isn't the code that you are using!!
          >
          > One getStoreProcedu res takes a parameter.
          > Dim xp As SqlDataReader = getStoreProcedu res("StoreProce dure")
          >
          > The other doesn't
          > Public Function getStoreProcedu res() As SqlDataReader
          >
          > This isn't the cause of the Exception, but it may be the cause of me not
          > being able to see what is!!
          >
          > Regards,
          > Fergus[/color]

          Fergus,
          Thanks a lot for pointing out my classic mistake. It works after I modified it.
          James

          Comment

          • Fergus Cooney

            #6
            Re: Passing SQLDataReader through a function

            Hi James,

            Strange, I'd have expected the compiler to pick that one up.

            No matter - you're back on the road again. :-)

            Regards,
            Fergus


            Comment

            • James Pham

              #7
              Re: Passing SQLDataReader through a function

              Fergus,

              Thanks a lot for pointing out the missing parameter in the function. It
              works now.

              James

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

              Comment

              Working...