sqlreader.GetValue(sqlreader.GetOrdinal(columnname) problem

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

    sqlreader.GetValue(sqlreader.GetOrdinal(columnname) problem

    Hi all,
    I am stuck with this problem.
    the code is like this:
    Dim sqlreader As SqlDataReader =
    myCommand.Execu teReader(Comman dBehavior.Close Connection)

    sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) give me the following in
    the watch window:

    sqlreader.GetVa lue(sqlreader.G etOrdinal("enro lldate")) Run-time exception
    thrown : System.InvalidO perationExcepti on - Invalid attempt to read when no
    data is present.
    But I can get the column number: sqlreader.GetOr dinal("enrollda te") is 1.
    What's going on?
    Thank you.
    Betty





    --
    Betty
  • Brian

    #2
    Re: sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) problem

    Do you call sqlreader.Read( ) before trying to get a value? No rows are
    fetched until you do this.

    Comment

    • c676228

      #3
      RE: sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) problem

      When I look at the sqlreader in the watch window, it gave the following info:

      Depth 0 Integer
      FieldCount 62 Integer
      HasRows True Boolean
      IsClosed False Boolean
      Item <cannot view indexed property> Object
      RecordsAffected -1 Integer

      So that means I pull the data through the procedure, right.
      --
      Betty


      "c676228" wrote:
      [color=blue]
      > Hi all,
      > I am stuck with this problem.
      > the code is like this:
      > Dim sqlreader As SqlDataReader =
      > myCommand.Execu teReader(Comman dBehavior.Close Connection)
      >
      > sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) give me the following in
      > the watch window:
      >
      > sqlreader.GetVa lue(sqlreader.G etOrdinal("enro lldate")) Run-time exception
      > thrown : System.InvalidO perationExcepti on - Invalid attempt to read when no
      > data is present.
      > But I can get the column number: sqlreader.GetOr dinal("enrollda te") is 1.
      > What's going on?
      > Thank you.
      > Betty
      >
      >
      >
      >
      >
      > --
      > Betty[/color]

      Comment

      • Phillip Williams

        #4
        RE: sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) problem

        Enclose your code in a while loop like this:

        While sqlreader.Read( )
        'place the code to GetValue here
        End While

        --
        HTH,
        Phillip Williams




        "c676228" wrote:
        [color=blue]
        > Hi all,
        > I am stuck with this problem.
        > the code is like this:
        > Dim sqlreader As SqlDataReader =
        > myCommand.Execu teReader(Comman dBehavior.Close Connection)
        >
        > sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) give me the following in
        > the watch window:
        >
        > sqlreader.GetVa lue(sqlreader.G etOrdinal("enro lldate")) Run-time exception
        > thrown : System.InvalidO perationExcepti on - Invalid attempt to read when no
        > data is present.
        > But I can get the column number: sqlreader.GetOr dinal("enrollda te") is 1.
        > What's going on?
        > Thank you.
        > Betty
        >
        >
        >
        >
        >
        > --
        > Betty[/color]

        Comment

        • sloan

          #5
          Re: sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) problem


          As other people has previous posted:


          this SafeDataReader has these built in checks for you.

          But... yes, you have to call the Read() method ... and put your code in the
          Read() loop ....

          Its kinda like "If not rs.EOF".. but its not quite that exactly either.

          ...

          The SafeDataReader is a nice bit of code to make the IDataReader a little
          easier to deal with.





          "c676228" <bettys@communi ty.nospam> wrote in message
          news:EB2336AA-D67C-443A-9C05-0942F46C319B@mi crosoft.com...[color=blue]
          > Hi all,
          > I am stuck with this problem.
          > the code is like this:
          > Dim sqlreader As SqlDataReader =
          > myCommand.Execu teReader(Comman dBehavior.Close Connection)
          >
          > sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) give me the following[/color]
          in[color=blue]
          > the watch window:
          >
          > sqlreader.GetVa lue(sqlreader.G etOrdinal("enro lldate")) Run-time exception
          > thrown : System.InvalidO perationExcepti on - Invalid attempt to read when[/color]
          no[color=blue]
          > data is present.
          > But I can get the column number: sqlreader.GetOr dinal("enrollda te") is 1.
          > What's going on?
          > Thank you.
          > Betty
          >
          >
          >
          >
          >
          > --
          > Betty[/color]


          Comment

          • c676228

            #6
            Re: sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) problem

            thank you all. I missed that part .
            --
            Betty


            "sloan" wrote:
            [color=blue]
            >
            > As other people has previous posted:
            > http://www.lhotka.net/cslacvs/viewcv...eDataReader.vb
            >
            > this SafeDataReader has these built in checks for you.
            >
            > But... yes, you have to call the Read() method ... and put your code in the
            > Read() loop ....
            >
            > Its kinda like "If not rs.EOF".. but its not quite that exactly either.
            >
            > ...
            >
            > The SafeDataReader is a nice bit of code to make the IDataReader a little
            > easier to deal with.
            >
            >
            >
            >
            >
            > "c676228" <bettys@communi ty.nospam> wrote in message
            > news:EB2336AA-D67C-443A-9C05-0942F46C319B@mi crosoft.com...[color=green]
            > > Hi all,
            > > I am stuck with this problem.
            > > the code is like this:
            > > Dim sqlreader As SqlDataReader =
            > > myCommand.Execu teReader(Comman dBehavior.Close Connection)
            > >
            > > sqlreader.GetVa lue(sqlreader.G etOrdinal(colum nname) give me the following[/color]
            > in[color=green]
            > > the watch window:
            > >
            > > sqlreader.GetVa lue(sqlreader.G etOrdinal("enro lldate")) Run-time exception
            > > thrown : System.InvalidO perationExcepti on - Invalid attempt to read when[/color]
            > no[color=green]
            > > data is present.
            > > But I can get the column number: sqlreader.GetOr dinal("enrollda te") is 1.
            > > What's going on?
            > > Thank you.
            > > Betty
            > >
            > >
            > >
            > >
            > >
            > > --
            > > Betty[/color]
            >
            >
            >[/color]

            Comment

            Working...