Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SqlDataReader reading beyond end of data

SqlDataReader reading beyond end of data

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheJudeDude
    wrote on last edited by
    #1

    I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record. Code: sqlReader.Read(); //this is after the last row has already been read, should throw exception? if (!sqlReader.HasRows)//sqlReader.HasRows = true...?? After this it loops back to a while and tries to read data, in which I get the error. I would appreciate if someone could explain to me the error of my ways :D

    Jude

    B J M A 4 Replies Last reply
    0
    • T TheJudeDude

      I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record. Code: sqlReader.Read(); //this is after the last row has already been read, should throw exception? if (!sqlReader.HasRows)//sqlReader.HasRows = true...?? After this it loops back to a while and tries to read data, in which I get the error. I would appreciate if someone could explain to me the error of my ways :D

      Jude

      B Offline
      B Offline
      Bassam Saoud
      wrote on last edited by
      #2

      Do you have the following structure ?

      while(sqlReader.Read())
      {
      // do reads here
      }

      1 Reply Last reply
      0
      • T TheJudeDude

        I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record. Code: sqlReader.Read(); //this is after the last row has already been read, should throw exception? if (!sqlReader.HasRows)//sqlReader.HasRows = true...?? After this it loops back to a while and tries to read data, in which I get the error. I would appreciate if someone could explain to me the error of my ways :D

        Jude

        J Offline
        J Offline
        Jimmanuel
        wrote on last edited by
        #3

        HasRows doesn't tell you how many rows you haven't "Read()", it only tells you if there are rows in the reader. Read advances you to the next row, starting from one before the first; the first time you call it you're placed at the first row. To loop through all of the rows use this:

        while (sqlReader.Read())
        {
        // do something with the row
        }

        For Reference: MSDN[^]

        :badger:

        T 1 Reply Last reply
        0
        • T TheJudeDude

          I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record. Code: sqlReader.Read(); //this is after the last row has already been read, should throw exception? if (!sqlReader.HasRows)//sqlReader.HasRows = true...?? After this it loops back to a while and tries to read data, in which I get the error. I would appreciate if someone could explain to me the error of my ways :D

          Jude

          M Offline
          M Offline
          Migounette
          wrote on last edited by
          #4

          In fact you should use it in the other way: DataTableReader dataTableReader = myDatabase.CreateDataReader(); if (dataTableReader.HasRows) { while (dataTableReader.Read()) { Console.Out.Write(...); } } Read will returns a boolean and does not through an exception. Moreover it also should depend on your provider (MySQL, SQL Server...), the cursor method used behind may conduct to the problem. You should read that you can use Read before HasRows but in the past I got some problems with some providers. Hope it helps

          1 Reply Last reply
          0
          • T TheJudeDude

            I received an error ( Invalid attempt to read when no data is present ) while reading data with a SqlDataReader. I went to the data and found the last record, then I debugged from that record. Code: sqlReader.Read(); //this is after the last row has already been read, should throw exception? if (!sqlReader.HasRows)//sqlReader.HasRows = true...?? After this it loops back to a while and tries to read data, in which I get the error. I would appreciate if someone could explain to me the error of my ways :D

            Jude

            A Offline
            A Offline
            ajith k rajagopalan
            wrote on last edited by
            #5

            DataTableReader dataTableReader = myDatabase.CreateDataReader(); if (dataTableReader.HasRows) { while (dataTableReader.Read()) { //// write here... eg : messagebox.show("oUT pUT ");//////// } }

            T 1 Reply Last reply
            0
            • J Jimmanuel

              HasRows doesn't tell you how many rows you haven't "Read()", it only tells you if there are rows in the reader. Read advances you to the next row, starting from one before the first; the first time you call it you're placed at the first row. To loop through all of the rows use this:

              while (sqlReader.Read())
              {
              // do something with the row
              }

              For Reference: MSDN[^]

              :badger:

              T Offline
              T Offline
              TheJudeDude
              wrote on last edited by
              #6

              I have the following

              while (sqlReader.Read())
              {
              //test for data field
              while(sqlReader["SOME FIELD"] == variable)
              {
              //do work, write data to file, yada, yada
              sqlReader.Read()//advance to next record
              if (!sqlReader.HasRows)//there's no more data
              {
              blEnd = true;
              break
              }
              if (blEnd){break} //break out of sqlReader.Read() loop
              //do more work, yada, yada
              }
              //end of sqlReader.Read(), loop
              }

              Jude

              J 1 Reply Last reply
              0
              • A ajith k rajagopalan

                DataTableReader dataTableReader = myDatabase.CreateDataReader(); if (dataTableReader.HasRows) { while (dataTableReader.Read()) { //// write here... eg : messagebox.show("oUT pUT ");//////// } }

                T Offline
                T Offline
                TheJudeDude
                wrote on last edited by
                #7

                Here is the exact error I am receiving: System.InvalidOperationException: Invalid attempt to read when no data is present But sqlReader.HasRows property is True.

                Jude

                1 Reply Last reply
                0
                • T TheJudeDude

                  I have the following

                  while (sqlReader.Read())
                  {
                  //test for data field
                  while(sqlReader["SOME FIELD"] == variable)
                  {
                  //do work, write data to file, yada, yada
                  sqlReader.Read()//advance to next record
                  if (!sqlReader.HasRows)//there's no more data
                  {
                  blEnd = true;
                  break
                  }
                  if (blEnd){break} //break out of sqlReader.Read() loop
                  //do more work, yada, yada
                  }
                  //end of sqlReader.Read(), loop
                  }

                  Jude

                  J Offline
                  J Offline
                  Jimmanuel
                  wrote on last edited by
                  #8

                  What if the data is NULL in the database? Before you check sqlReader["SOME FIELD"] against your target value you should check it against DBNull.Value. Also, this:

                        sqlReader.Read()//advance to next record
                        if (!sqlReader.HasRows)//there's no more data
                  

                  has the same problem as before. HasRows doesn't care if you called Read or how many times it's called. It always returns the same thing. You shouldn't have more than one call to Read and you should always check it's return value. What it looks like you're trying to do if go through all of the rows in the reader and do some work when the value of sqlReader["SOME FIELD"] equals variable. If you found variable then exit the loop. If that's what you're doing then try this:

                  bool found = false;

                  while (sqlReader.Read() && !found)
                  {
                  if (sqlReader["SOME FIELD"] == DBNull.Value)
                  {
                  // advance to the next row
                  continue;
                  }

                  // convert the data to the appropriate type here, I'm using string as an example
                  string dbVal = sqlReader\["SOME FIELD"\] as string;
                  
                  if (dbVal != variable)
                  {
                      // advance to the next row
                      continue;
                  }
                  
                  // value is found!
                  found = true;
                  // do something . . . 
                  

                  }

                  Even if it's not what you're doing, note that there's only one Read, it's return value is checked and we're checking for DBNull.Value. dbValshould be the same type as whatever variable is, I just picked string as an example. Also note that HasRows is not needed.

                  :badger:

                  T 1 Reply Last reply
                  0
                  • J Jimmanuel

                    What if the data is NULL in the database? Before you check sqlReader["SOME FIELD"] against your target value you should check it against DBNull.Value. Also, this:

                          sqlReader.Read()//advance to next record
                          if (!sqlReader.HasRows)//there's no more data
                    

                    has the same problem as before. HasRows doesn't care if you called Read or how many times it's called. It always returns the same thing. You shouldn't have more than one call to Read and you should always check it's return value. What it looks like you're trying to do if go through all of the rows in the reader and do some work when the value of sqlReader["SOME FIELD"] equals variable. If you found variable then exit the loop. If that's what you're doing then try this:

                    bool found = false;

                    while (sqlReader.Read() && !found)
                    {
                    if (sqlReader["SOME FIELD"] == DBNull.Value)
                    {
                    // advance to the next row
                    continue;
                    }

                    // convert the data to the appropriate type here, I'm using string as an example
                    string dbVal = sqlReader\["SOME FIELD"\] as string;
                    
                    if (dbVal != variable)
                    {
                        // advance to the next row
                        continue;
                    }
                    
                    // value is found!
                    found = true;
                    // do something . . . 
                    

                    }

                    Even if it's not what you're doing, note that there's only one Read, it's return value is checked and we're checking for DBNull.Value. dbValshould be the same type as whatever variable is, I just picked string as an example. Also note that HasRows is not needed.

                    :badger:

                    T Offline
                    T Offline
                    TheJudeDude
                    wrote on last edited by
                    #9

                    Thank you much for your reply. What I am confused about in your example is the comment to 'continue to the next row'. How do I advance the cursor without calling Read()?

                    Jude

                    J 1 Reply Last reply
                    0
                    • T TheJudeDude

                      Thank you much for your reply. What I am confused about in your example is the comment to 'continue to the next row'. How do I advance the cursor without calling Read()?

                      Jude

                      J Offline
                      J Offline
                      Jimmanuel
                      wrote on last edited by
                      #10

                      continue[^] The continue; statement returns the code to the while() line which calls Read(), and that's how it advances to the next row :)

                      :badger:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups