Load next record in table only iterating between two records Linq C# Npgsql

138 Views Asked by At

On my view/page I am loading 1 record on page load, and each time next button is clicked I intend to load the next single (ONE) record in the table and on and on until theres the last. So I am refreshing the page with the next record.

[View when next button is clicked]

Now when one clicks the next button I have the following

In View

<a class="form-group" style="float: left; margin-right: 5px;">
  <input type="submit" value="Next" name="NextRecordButton" class="btn btn-primary" />
</a>

In Controller

if (NextRecordButton == "Next" && Request.HttpMethod == "POST")
        {
            /*keep track of the current id on page*/
            string idOnView = Request.Form["q_guid"];
            var currentRowId = Guid.Parse(idOnView);

            /*select next id*/
            var myNext = db.q_product.Where(i => i.q_guid != currentRowId)
                .OrderBy(i => i.q_guid)
                .Skip(1)
                .First().q_guid;

            ViewBag.NextProduct = myNext;
            var idNext = Convert.ToString(ViewBag.NextProduct);

            NpgsqlConnection connectionSearch = new NpgsqlConnection("Host=test;Database=test;Username=test;Password=test");
            connectionSearch.Open();

            NpgsqlCommand cmdNext = new NpgsqlCommand
                                ("SELECT * FROM q_product " +                                    
                                "WHERE @idnext ilike '%' || q_guid || '%'" +
                                "FETCH FIRST 1 ROWS ONLY"
                                , connectionSearch);                

            cmdNext.Parameters.AddWithValue("@idnext", idNext);
            cmdNext.ExecuteNonQuery();  

PROBLEM

Now on clicking the next button with this code, - it will load 2nd product perfectly fine onto the page Clicking next again - it will load 3rd product perfectly fine onto the page

but from that point onwards after loading 3rd product and clicking next, it then keeps iterating between 2nd and 3rd product on click. Even though theres more that 3 products in the table.

1

There are 1 best solutions below

0
On BEST ANSWER

Issue was solved by using NpgsqlCommand Query instead of Linq query. So this section

/*select next id*/
var myNext = db.q_product.Where(i => i.q_guid != currentRowId)
      .OrderBy(i => i.q_guid)
      .Skip(1)
      .First().q_guid;

Was changed and implemented in the following way

/*select next id*/
NpgsqlCommand myNextSql = new NpgsqlCommand
       ("Select q_guid FROM q_product WHERE q_guid > :idonview LIMIT 1", connectionSearch);

       myNextSql.Parameters.AddWithValue("idonview", currentRowId);
       NpgsqlDataReader reader = myNextSql.ExecuteReader();
       while (reader.Read())
       {
          var myID = reader[0].ToString();
          ViewBag.NextProduct = myID;
       }
       connectionSearch.Close();

       var idNext = Convert.ToString(ViewBag.NextProduct);

I can now go through all the next rows in table when clicking the next button. I am curious to know from the more knowleagable folks, why the linq implemetation would behave this way for me as it seems to be the widely used method when you google "getting next row" in table.