Help with moving to First and Last Record

Hi,

I am using code below to move to Next and Previous records. How do I modify the code to move to First and Last Records?

Next:

 dtMaster = Session("dtMaster")
        dtKeyMaster = Session("dtKeyMaster")
        Dim rowId As Integer = Session("currentIndex") + 1
        ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))

Previous:

 dtMaster = Session("dtMaster")
        dtKeyMaster = Session("dtKeyMaster")
        Dim rowId As Integer
        If Session("currentIndex") >= 1 Then
            rowId = Session("currentIndex") - 1
        Else
            rowId = 0
        End If
        '   ViewDataA(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
        Session("currentIndex") = rowId

Victor
vcharlesAsked:
Who is Participating?
 
Miguel OzConnect With a Mentor Software EngineerCommented:
Can you post ViewDataA and B implementations? I am interrested in the session assignment.
One posible solution
First
dtMaster = Session("dtMaster")
dtKeyMaster = Session("dtKeyMaster")
Dim rowId As Integer = 0
ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
Session("currentIndex")  = 0;

Open in new window

First
dtMaster = Session("dtMaster")
dtKeyMaster = Session("dtKeyMaster")
Dim rowId As Integer =dtKeyMaster.Rows.Count - 1
ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
Session("currentIndex")  = rowId;

Open in new window

0
 
Alan WarrenApplications DeveloperCommented:
your Next block should probably test for current record being the last record, before navigating to next.

You can use the datatable.rows.rowcount.
        If Session("currentIndex") < dtKeyMaster.Rows.Count - 1 Then
            rowId = Session("currentIndex") + 1
        Else
            rowId = Session("currentIndex") 
        End If

Open in new window

Alan ";0)
0
 
Rajar AhmedConsultantCommented:
Hi ,
    As a continuation of our discussion , You can  replace all Session variable which we used on previous thread to Viewstate  has session can hampers the server memory  when the application grows .

The below code is exactly same as @mas_oz2003 nothing i did except replacement of session to viewstate.
    Protected Sub btnFirst_Click(sender As Object, e As EventArgs)
        dtMaster = ViewState("dtMaster")
        dtKeyMaster = ViewState("dtKeyMaster")
        Dim rowId As Integer = 0
        ViewData(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
        ViewState("currentIndex") = rowId
    End Sub
    Protected Sub btnLast_Click(sender As Object, e As EventArgs)
        dtMaster = ViewState("dtMaster")
        dtKeyMaster = ViewState("dtKeyMaster")
        Dim rowId As Integer = dtKeyMaster.Rows.Count - 1
        ViewData(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
        ViewState("currentIndex") = rowId
    End Sub

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
vcharlesAuthor Commented:
Hi,

I replaced session with ViewState for Next, Previous also, but I received the following error:

Object reference not set to an instance of an object.

On line:

ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))


        dtMaster = ViewState("dtMaster")
        dtKeyMaster = ViewState("dtKeyMaster")
        Dim rowId As Integer = Session("currentIndex") + 1
        ViewDataB(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))

Same error occurs with code to MoveFirst.

Any ideas what is causing this error?

Thanks,

V.
0
 
Rajar AhmedConsultantCommented:
this looks like more of generic error, do debug and check whether your getting values for  respective object

ie dtmaster,dtkeymaster,
Also change session("currentindex") to viewstate("currentindex")

Meeran03
0
 
Rajar AhmedConsultantCommented:
Have you change this on Searchfuze1 method?
0
 
vcharlesAuthor Commented:
I did not, will do so in a few minutes. I am only testing three records, but it seems that Session works for going to First and Last. But I will take your advice and try to use ViewState.

Protected Sub btnFirst_Click(sender As Object, e As EventArgs)
        dtMaster = Session("dtMaster")
        dtKeyMaster = Session("dtKeyMaster")
        Dim rowId As Integer = 0
        ViewData(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
        ViewState("currentIndex") = rowId
    End Sub
    Protected Sub btnLast_Click(sender As Object, e As EventArgs)
        dtMaster = Session("dtMaster")
        dtKeyMaster = Session("dtKeyMaster")
        Dim rowId As Integer = dtKeyMaster.Rows.Count - 1
        ViewData(dtKeyMaster.Rows(rowId)(1), dtKeyMaster.Rows(rowId)(2))
        ViewState("currentIndex") = rowId
    End Sub
V.
0
 
vcharlesAuthor Commented:
Did you mean change Session("currentIndex") = 0 to ViewState("currentIndex") = 0?
0
 
Rajar AhmedConnect With a Mentor ConsultantCommented:
yes , and also below change respectively,

     ViewState("dtMaster") = dtMaster
        ViewState("dtKeyMaster") = dtKeyMaster
0
 
vcharlesAuthor Commented:
It Works!
Thanks!
V.
0
 
Alan WarrenApplications DeveloperCommented:
mas_oz2003 got my vote, he clearly answered the question as originally stated: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27833879.html#a38308525

Alan ";0)
0
 
vcharlesAuthor Commented:
Hello,

Sorry, I over looked the initial answer, I would like to split the points (250 each)  between post

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27833879.html#a38308525

and post

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27833879.html#a38309204


Thank  You both for your Help.

Victor
0
 
Rajar AhmedConsultantCommented:
Yes, i deserve only a assisted solution  for this thread.
 
Meeran03
0
 
Miguel OzSoftware EngineerCommented:
the split is OK for me. thanks
0
 
vcharlesAuthor Commented:
Please close this issue. Thank You!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.