Moving to record X in ASP.NET

Posted on 2011-10-19
Last Modified: 2012-06-21
Hello all!

I remember with standard ASP I used to move the record pointer as follow:

rs.move (20)

or something like that...

How do I do this with ASP.NET?

Question by:pvg1975
    LVL 23

    Expert Comment

    by:Rajkumar Gs
    In ASP.Net, if you binding dataset, you can loop through dataset values.
    Like -

    Using this logic you can move to 20th record in dataset.


    Author Comment

    I dont understand your sample Raj,

    This is how Im accesing the table.  

    Dim conString = ConfigurationManager.ConnectionStrings("MyDataBase").ConnectionString
    Dim con As New SqlConnection(conString)
    Dim strQuery As String = "SELECT ................................."
    Dim cmd As New SqlCommand(strQuery, con)
    Dim RS As SqlDataReader = cmd.ExecuteReader
    if then
    end if

    I need to move to a specific record to paginate a list of thousands of records. For example, if show 20 records per page, and if the user clicks on PAGE 5 I know I have to move the pointer to (5x20)+1 (record 101) and show only the next 20.

    I dont want to loop and unnecesary go thru 100 records till I reach record 101. This is fine for just 100 records, but I need to list thousands of them.

    I remember I did this with MOVE in ASP, but I dont know how to do it in .NET
    LVL 23

    Expert Comment

    by:Rajkumar Gs
    Now I understand your question right. This question is already answered.
    Please check this question -


    Author Comment

    Hi again Raj,

    Thanks for the comment, I checked out the question and the link ( but I still couldnt figure out how to move the pointer straight to an X number of records.

    LVL 20

    Expert Comment

    The problem unfortunately is the use of a datareader. I have no problem with datareaders (I like them alot actually) but it is their nature to read only one line of the results at a time in sequence. For some reason there are no methods added to the datareader to advance multiple records forward or backward (it would have been a good thing to do).

    So you are forced to re-examine how you want to deal with this:

    1. store your data return in an in memory construct such as a dataset and then you should be able to reach in and grab any records you would like at will. The downside of this is it is potentially more resource intensive than a datareader.

    2. Probably the more conventionally "professional" approach would be to continue to use a datareader or even a dataset but limit and choose the records you want through your sql statement. This would give you a more streamlined paging mechanism.

    NOTE: Depending on what you are paging through a simplistic approach can be had using a sql dataadapter. This often gives you the option to page automatically, cache if you want, and or use ajax to minimize individual data reads.

    I don't suppose this is the kind of answer you want. Its certainly not as easy as "just use this method and your done", but I believe it is a good example of the type of options you will have to consider to get this functioning the way you want (unless you are going to loop forward). Loading all that data from sql each time with a datareader with or without a method sounds like it would be kindof "heavy lifting" in your application anyway.

    Good Luck
    LVL 23

    Expert Comment

    by:Rajkumar Gs
    ddayx10 clearly explained everything, I believe, there is no further explanation on this. I suggest you to use paging in SQL as ddayx10 mentioned. That would be more efficient, since it loads only required data and would be 'light-weight'

    If you want to proceed with this way, let me know - I will provide you an example.


    LVL 23

    Accepted Solution

    >> 2. Probably the more conventionally "professional" approach would be to continue to use a datareader or even a dataset but limit and choose the records you want through your sql statement. This would give you a more streamlined paging mechanism. <<

    This is an sample query that fetch ONLY those records based on 'from' record position and 'to' record position that we mention.
    declare @from_position int, @to_position int
    set @from_position = 100000
    set @to_position = 100100
    select * from
    select *, row_number() over(order by temp_po_id) Serial from tblTesting               
    ) a 
    where Serial between @from_position and @to_position

    Open in new window

    When you try to 'MOVE TO' some record, give proper from and to record position.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now