sqlreader.GetValue(sqlreader.GetOrdinal(columnname) problem

Discussion in 'ASP .Net' started by =?Utf-8?B?YzY3NjIyOA==?=, Feb 24, 2006.

  1. Hi all,
    I am stuck with this problem.
    the code is like this:
    Dim sqlreader As SqlDataReader =
    myCommand.ExecuteReader(CommandBehavior.CloseConnection)

    sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following in
    the watch window:

    sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
    thrown : System.InvalidOperationException - Invalid attempt to read when no
    data is present.
    But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
    What's going on?
    Thank you.
    Betty





    --
    Betty
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 24, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?YzY3NjIyOA==?=

    Brian Guest

    Do you call sqlreader.Read() before trying to get a value? No rows are
    fetched until you do this.
     
    Brian, Feb 24, 2006
    #2
    1. Advertising

  3. 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:

    > Hi all,
    > I am stuck with this problem.
    > the code is like this:
    > Dim sqlreader As SqlDataReader =
    > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following in
    > the watch window:
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
    > thrown : System.InvalidOperationException - Invalid attempt to read when no
    > data is present.
    > But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
    > What's going on?
    > Thank you.
    > Betty
    >
    >
    >
    >
    >
    > --
    > Betty
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 24, 2006
    #3
  4. Enclose your code in a while loop like this:

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

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "c676228" wrote:

    > Hi all,
    > I am stuck with this problem.
    > the code is like this:
    > Dim sqlreader As SqlDataReader =
    > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following in
    > the watch window:
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
    > thrown : System.InvalidOperationException - Invalid attempt to read when no
    > data is present.
    > But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
    > What's going on?
    > Thank you.
    > Betty
    >
    >
    >
    >
    >
    > --
    > Betty
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Feb 24, 2006
    #4
  5. =?Utf-8?B?YzY3NjIyOA==?=

    sloan Guest

    As other people has previous posted:
    http://www.lhotka.net/cslacvs/viewcvs.cgi/*checkout*/CSLA10/CSLA/SafeDataReader.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" <> wrote in message
    news:...
    > Hi all,
    > I am stuck with this problem.
    > the code is like this:
    > Dim sqlreader As SqlDataReader =
    > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following

    in
    > the watch window:
    >
    > sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
    > thrown : System.InvalidOperationException - Invalid attempt to read when

    no
    > data is present.
    > But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
    > What's going on?
    > Thank you.
    > Betty
    >
    >
    >
    >
    >
    > --
    > Betty
     
    sloan, Feb 24, 2006
    #5
  6. thank you all. I missed that part .
    --
    Betty


    "sloan" wrote:

    >
    > As other people has previous posted:
    > http://www.lhotka.net/cslacvs/viewcvs.cgi/*checkout*/CSLA10/CSLA/SafeDataReader.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" <> wrote in message
    > news:...
    > > Hi all,
    > > I am stuck with this problem.
    > > the code is like this:
    > > Dim sqlreader As SqlDataReader =
    > > myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > >
    > > sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following

    > in
    > > the watch window:
    > >
    > > sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
    > > thrown : System.InvalidOperationException - Invalid attempt to read when

    > no
    > > data is present.
    > > But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
    > > What's going on?
    > > Thank you.
    > > Betty
    > >
    > >
    > >
    > >
    > >
    > > --
    > > Betty

    >
    >
    >
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 24, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rico Alexander
    Replies:
    1
    Views:
    735
    William Ryan
    Oct 14, 2003
  2. William F. Robertson, Jr.

    Bug related to ColumnName with DataTable.

    William F. Robertson, Jr., May 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    317
    William F. Robertson, Jr.
    May 20, 2004
  3. tolu45
    Replies:
    2
    Views:
    859
    Roedy Green
    Oct 19, 2005
  4. tolu45
    Replies:
    2
    Views:
    481
    Chris Smith
    Nov 27, 2005
  5. Roger

    Datagrid, SQLReader and Paging? Shouldn't this work?

    Roger, Dec 28, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    127
    R. Thomas, aka Xtreme.Net
    Dec 29, 2004
Loading...

Share This Page