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 is not returning all rows

SqlDataReader is not returning all rows

Scheduled Pinned Locked Moved C#
database
13 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.
  • S Simon_Whale

    I know that isn't an answer to your question but why are you executing this query twice?

    NarVish wrote:

    using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) {} }

    and

    NarVish wrote:

    DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection); da.Fill(ds);

    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

    N Offline
    N Offline
    NarVish
    wrote on last edited by
    #3

    Just to show the difference, I added the SqlDataAdapter code. It will be removed.

    1 Reply Last reply
    0
    • N NarVish

      My SQL command returns 3 rows which is verified in a SQL GUI. I run the same code and SqlDataReader returns only 2 of them. The same sql command returns 3 rows with SqlDataAdapter. Here is my code. ds has 3 rows.

      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
      {
      string sql="SELECT DISTINCT A.account_id, A.fname, A.lname,
      FROM T_Test1 A WITH (NOLOCK) JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
      WHERE account_status = 'A' AND A.card IS NOT NULL
      AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00' AND AF.code = 'INE'";
      SqlCommand command = new SqlCommand(sql.ToString(), connection);
      command.CommandTimeout = 3600;
      connection.Open();
      using (SqlDataReader reader = command.ExecuteReader())
      {
      while (reader.Read())
      {}
      }
      DataSet ds = new DataSet();
      SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection);
      da.Fill(ds);
      }

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #4

      what happens if the you run the query direct on your database?

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      N 1 Reply Last reply
      0
      • N NarVish

        My SQL command returns 3 rows which is verified in a SQL GUI. I run the same code and SqlDataReader returns only 2 of them. The same sql command returns 3 rows with SqlDataAdapter. Here is my code. ds has 3 rows.

        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
        {
        string sql="SELECT DISTINCT A.account_id, A.fname, A.lname,
        FROM T_Test1 A WITH (NOLOCK) JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
        WHERE account_status = 'A' AND A.card IS NOT NULL
        AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00' AND AF.code = 'INE'";
        SqlCommand command = new SqlCommand(sql.ToString(), connection);
        command.CommandTimeout = 3600;
        connection.Open();
        using (SqlDataReader reader = command.ExecuteReader())
        {
        while (reader.Read())
        {}
        }
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection);
        da.Fill(ds);
        }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #5

        What are the three rows, and which one is not being returned?

        This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        N 1 Reply Last reply
        0
        • S Simon_Whale

          what happens if the you run the query direct on your database?

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          N Offline
          N Offline
          NarVish
          wrote on last edited by
          #6

          If I run the query on database (SQL GUI), it is showing three records

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            What are the three rows, and which one is not being returned?

            This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

            N Offline
            N Offline
            NarVish
            wrote on last edited by
            #7

            DataReader didn't read first record.

            OriginalGriffO 1 Reply Last reply
            0
            • N NarVish

              My SQL command returns 3 rows which is verified in a SQL GUI. I run the same code and SqlDataReader returns only 2 of them. The same sql command returns 3 rows with SqlDataAdapter. Here is my code. ds has 3 rows.

              using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
              {
              string sql="SELECT DISTINCT A.account_id, A.fname, A.lname,
              FROM T_Test1 A WITH (NOLOCK) JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
              WHERE account_status = 'A' AND A.card IS NOT NULL
              AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00' AND AF.code = 'INE'";
              SqlCommand command = new SqlCommand(sql.ToString(), connection);
              command.CommandTimeout = 3600;
              connection.Open();
              using (SqlDataReader reader = command.ExecuteReader())
              {
              while (reader.Read())
              {}
              }
              DataSet ds = new DataSet();
              SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection);
              da.Fill(ds);
              }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              NarVish wrote:

              I run the same code and SqlDataReader returns only 2 of them.

              The DataAdapter does not remove records from the resultset that it receives.

              NarVish wrote:

              AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00'

              Are you sure those dates are correctly interpreted? Use a parameterized query to prevent questions like those :) Further, "sql" is already a string (no need to call .ToString) and SqlCommand is a disposable.

              NarVish wrote:

              My SQL command returns 3 rows which is verified in a SQL GUI.

              Also do verify that you're pointing to the same database in both environments.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              M 1 Reply Last reply
              0
              • N NarVish

                DataReader didn't read first record.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #9

                Hm. Unlikely - but suspicious. I use code very similar to yours every day - and it has never missed out a row (particularly not the first row). I notice that the code you show a fragment of isn't the code you are actually using - can you post that bit? It seems unlikely that a "missing the first row" problem would be that specific is it was an SQlReader bug. Oh, and can you show the three rows worth of data and I'll try it here, see what happens.

                This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • L Lost User

                  NarVish wrote:

                  I run the same code and SqlDataReader returns only 2 of them.

                  The DataAdapter does not remove records from the resultset that it receives.

                  NarVish wrote:

                  AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00'

                  Are you sure those dates are correctly interpreted? Use a parameterized query to prevent questions like those :) Further, "sql" is already a string (no need to call .ToString) and SqlCommand is a disposable.

                  NarVish wrote:

                  My SQL command returns 3 rows which is verified in a SQL GUI.

                  Also do verify that you're pointing to the same database in both environments.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  M Offline
                  M Offline
                  Manfred Rudolf Bihy
                  wrote on last edited by
                  #10

                  Eddy Vluggen wrote:

                  Also do verify that you're pointing to the same database in both environments

                  Good thing to point out! I've been bitten by that one before. Cheers!

                  "I had the right to remain silent, but I didn't have the ability!"

                  Ron White, Comedian

                  1 Reply Last reply
                  0
                  • N NarVish

                    My SQL command returns 3 rows which is verified in a SQL GUI. I run the same code and SqlDataReader returns only 2 of them. The same sql command returns 3 rows with SqlDataAdapter. Here is my code. ds has 3 rows.

                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VPO"].ConnectionString))
                    {
                    string sql="SELECT DISTINCT A.account_id, A.fname, A.lname,
                    FROM T_Test1 A WITH (NOLOCK) JOIN T_Test2 AF WITH (NOLOCK) ON A.Account_id=AF.Account_id
                    WHERE account_status = 'A' AND A.card IS NOT NULL
                    AND A.dateFrom >= '09-02-2013 00:00:00' AND A.dateFrom <= '09-30-2013 00:00:00' AND AF.code = 'INE'";
                    SqlCommand command = new SqlCommand(sql.ToString(), connection);
                    command.CommandTimeout = 3600;
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                    while (reader.Read())
                    {}
                    }
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(command.CommandText, connection);
                    da.Fill(ds);
                    }

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #11

                    I'm suspicious that you're missing the code inside reader.Read(). I have the feeling that the problem you are looking at lies in that block. As a test, replace your reader.Read line with this

                    int count = 0;
                    while (reader.Read())
                    {
                    Debug.WriteLine("Count is {0}", ++count);

                    Then, when you are debugging your app, take a look at what appears in the output window in Visual Studio.

                    Chill _Maxxx_
                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    N 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      I'm suspicious that you're missing the code inside reader.Read(). I have the feeling that the problem you are looking at lies in that block. As a test, replace your reader.Read line with this

                      int count = 0;
                      while (reader.Read())
                      {
                      Debug.WriteLine("Count is {0}", ++count);

                      Then, when you are debugging your app, take a look at what appears in the output window in Visual Studio.

                      Chill _Maxxx_
                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      N Offline
                      N Offline
                      NarVish
                      wrote on last edited by
                      #12

                      One of the line in using section is reading the first record. In while loop, it is reading from second record. I removed the below if condition and it worked fine. Thank you all for your replies. Sorry for not posting that line, as I thought that line is handling only exception. if (!reader.Read()) throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database."); while (reader.Read()) {}

                      P 1 Reply Last reply
                      0
                      • N NarVish

                        One of the line in using section is reading the first record. In while loop, it is reading from second record. I removed the below if condition and it worked fine. Thank you all for your replies. Sorry for not posting that line, as I thought that line is handling only exception. if (!reader.Read()) throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database."); while (reader.Read()) {}

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #13

                        At least the mystery has been solved. The effect you were after here could have been achieved by using the following instead:

                        if (!reader.HasRows)
                        {
                        throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database.");
                        }
                        while (reader.Read())
                        {
                        }

                        For future reference, the act of calling Read automatically moves the record to the next available record (if one's present) when it completes. It implements a firehose cursor, whereby data is effectively squirted at you.

                        Chill _Maxxx_
                        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                        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