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. how read the prevoius row from the sqldatareader

how read the prevoius row from the sqldatareader

Scheduled Pinned Locked Moved C#
tutorial
12 Posts 6 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.
  • P Offline
    P Offline
    prasadbuddhika
    wrote on last edited by
    #1

    how to read the prevoius row using a sqldatareader(rs) object(opposite of rs.next())

    G 1 Reply Last reply
    0
    • P prasadbuddhika

      how to read the prevoius row using a sqldatareader(rs) object(opposite of rs.next())

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      You can't move backwards in the data stream. Once you move forward, the previous record doesn't exist any more.

      Despite everything, the person most likely to be fooling you next is yourself.

      P 1 Reply Last reply
      0
      • G Guffa

        You can't move backwards in the data stream. Once you move forward, the previous record doesn't exist any more.

        Despite everything, the person most likely to be fooling you next is yourself.

        P Offline
        P Offline
        prasadbuddhika
        wrote on last edited by
        #3

        thanx. then how can i get the previous record , what is the option i can use ..

        _ A G 3 Replies Last reply
        0
        • P prasadbuddhika

          thanx. then how can i get the previous record , what is the option i can use ..

          _ Offline
          _ Offline
          _AK_
          wrote on last edited by
          #4

          Don't use sqldatareader. Use dataset instead.

          Apurva Kaushal

          G 1 Reply Last reply
          0
          • P prasadbuddhika

            thanx. then how can i get the previous record , what is the option i can use ..

            A Offline
            A Offline
            Ashfield
            wrote on last edited by
            #5

            You need to save the current record, then when you read next the record the one you have saved is the prevous record. Obvious really!

            Bob Ashfield Consultants Ltd

            D 1 Reply Last reply
            0
            • A Ashfield

              You need to save the current record, then when you read next the record the one you have saved is the prevous record. Obvious really!

              Bob Ashfield Consultants Ltd

              D Offline
              D Offline
              David Fleming
              wrote on last edited by
              #6

              That's true if you only need to back up one record. Suppose you want to back up two.... The previous answer is, I think, more what the questioner needed. If you want to move forward AND backward, you need to use a DataTable, not a DataReader.

              A 1 Reply Last reply
              0
              • P prasadbuddhika

                thanx. then how can i get the previous record , what is the option i can use ..

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Instead of asking about what you think is the solution, I think that you should rather ask about what you are trying to accomplish. Why would you need the previous record? If you are trying to compare some data, perhaps that would be much faster in the database query than in the C# code?

                Despite everything, the person most likely to be fooling you next is yourself.

                P 1 Reply Last reply
                0
                • _ _AK_

                  Don't use sqldatareader. Use dataset instead.

                  Apurva Kaushal

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Well, you can't actually use a DataSet instead of a DataReader, as you need a DataReader to populate the DataSet with data from the database. Also, if the DataReader reads from a large result, it might not be practically possible to put all the data in a DataSet. Back to prasadbuddhika about what it is that he actually is trying to do...

                  Despite everything, the person most likely to be fooling you next is yourself.

                  1 Reply Last reply
                  0
                  • D David Fleming

                    That's true if you only need to back up one record. Suppose you want to back up two.... The previous answer is, I think, more what the questioner needed. If you want to move forward AND backward, you need to use a DataTable, not a DataReader.

                    A Offline
                    A Offline
                    Ashfield
                    wrote on last edited by
                    #9

                    David Fleming wrote:

                    That's true if you only need to back up one record.

                    Which is what he asked :) The possibilities are endless - datatables, collections, arrays. It all depends on what he wants to do - and as usual there was no clue to that.

                    Bob Ashfield Consultants Ltd

                    1 Reply Last reply
                    0
                    • G Guffa

                      Instead of asking about what you think is the solution, I think that you should rather ask about what you are trying to accomplish. Why would you need the previous record? If you are trying to compare some data, perhaps that would be much faster in the database query than in the C# code?

                      Despite everything, the person most likely to be fooling you next is yourself.

                      P Offline
                      P Offline
                      prasadbuddhika
                      wrote on last edited by
                      #10

                      what i need is to read previous data to compare data with previous data.

                      N G 2 Replies Last reply
                      0
                      • P prasadbuddhika

                        what i need is to read previous data to compare data with previous data.

                        N Offline
                        N Offline
                        N a v a n e e t h
                        wrote on last edited by
                        #11

                        You can keep the data in a generic List which can be enumerated later for checking. Assume you are getting numbers from data reader.

                        List<int> data = new List<int>();
                        using(SqlDataReader reader = getTheReader()){
                        if(reader.HasRows)
                        {
                        int ordinal = reader.GetOrdinal("YourColumnName");
                        while(reader.Read())
                        data.Add(reader.GetInt32());
                        }
                        reader.Close();
                        }

                        // contents of reader is in list _data _ Use that to do comparison

                        Navaneeth How to use google | Ask smart questions

                        1 Reply Last reply
                        0
                        • P prasadbuddhika

                          what i need is to read previous data to compare data with previous data.

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #12

                          prasadbuddhika wrote:

                          what i need is to read previous data to compare data with previous data.

                          So, you want the previous data to... eh... use it? Could you be a bit less specific, please? ;) If you for example need the previous data in order to only display one of each value, that could be done before you get the data from the database. I.e. instead of: select Name from SomeTable order by Name and if (Name <> PreviousName) ListBox.Items.Add(Name); you could do: select distinct Name from SomeTable order by Name and ListBox.Items.Add(Name); This way you would only fetch the data that you actually use from the database, instead of fetching a lot of data only to throw it away. In similar ways you can use the database to group records and calculate things like sum, average, minimum and maximum.

                          Despite everything, the person most likely to be fooling you next is yourself.

                          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