Navigating Records Problem

anyone know why the code below does not work?  i have 4 buttons on the form which have the following OnClick values
First onclick="ToFirstRecord"
Previous onclick="ToPreviousRecord"
Next onclick="ToNextRecord"
Last onclick="ToLastRecord"

when i click on the "next" button if goes to the next record.  if i click next again it does not go to the 3rd record.  if i click on any of the other buttons it always ends up going back to the first record.

anyone know whats going on?

Dim m_cnADONetConnection As New SqlConnection
Dim m_daDataAdapter As New SqlDataAdapter
Dim m_dtContacts As New DataTable
Dim m_rowPosition As Integer

sub Page_Load(sender as Object, e as EventArgs)
      m_cnADONetConnection.ConnectionString = "MyconnectionString"
            m_daDataAdapter = New SqlDataAdapter("SELECT * FROM TABLE1",m_cnADONetConnection)

            m_daDataAdapter.Fill(m_dtContacts)

            Me.ShowCurrentRecord()
end sub

Private Sub ShowCurrentRecord()
      If m_dtContacts.Rows.Count = 0 Then
            testbox1.text = ""
            testbox2.text = ""
            Exit Sub
      End If

      testbox1.text = m_dtContacts.Rows(m_rowPosition)("reviewer").ToString()
      testbox2.text = m_dtContacts.Rows(m_rowPosition)("date").ToString()
End Sub

Sub ToFirstRecord(Source as Object, E as EventArgs)
      m_rowPosition = 0
      Me.ShowCurrentRecord()
End Sub

'if not at the first row, go back one row and show the record
Sub ToPreviousRecord(Source as Object, E as EventArgs)
      If m_rowPosition <> 0 Then
            m_rowPosition = m_rowPosition - 1
            Me.ShowCurrentRecord()
      End If
End Sub

'if not on the last row, advance one row and show the record
Sub ToNextRecord(Source as Object, E as EventArgs)
      If m_rowPosition <> (m_dtContacts.Rows.Count - 1) Then
            m_rowPosition = m_rowPosition + 1
            Me.ShowCurrentRecord()
      End If
End Sub

'if there are any rows in the data table, move to the last and show the record
Sub ToLastRecord(Source as Object, E as EventArgs)
      If m_dtContacts.Rows.Count <> 0 then
            m_rowPosition = m_dtContacts.Rows.Count - 1
            Me.ShowCurrentRecord()
      End If
End Sub

thanks
LVL 1
narmi2Asked:
Who is Participating?
 
RejojohnyConnect With a Mentor Commented:
where is m_rowPosition maintained ? it might always contain the value 0 .. so next is moving to the first record (0 + 1) .. and all the other buttons behave in the way u have defined ...
0
 
dharmesh_amityConnect With a Mentor Commented:
Put m_rowPosition in viewstate everytime and get it everytime from viewstate
0
 
narmi2Author Commented:
about an hour ago it came to me that it will be much better to do this using a datagrid!!!

so i tried it out and it works perfectly.  i will post up the code soon so you all can have a look

thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
narmi2Author Commented:
the code

Sub BindData()
      MySQL = "SELECT * from MyTable"

      MyDataSet = new DataSet()
      MyDataAdapter = New SqlDataAdapter(MySQL, MyConn)
      MyDataAdapter.Fill(MyDataSet, "PhotoDataSet")
      
      MyDataGrid.datasource=MyDataSet
      MyDataGrid.datamember="PhotoDataSet"
      
      MyDataGrid.DataBind()
      
      lblCurrentRecord.text = MyDataGrid.CurrentPageIndex + 1
      lblTotalRecords.text = MyDataGrid.pageCount
End Sub

Sub GoToFirstRecord(sender as object, e as eventargs)
      MyDataGrid.CurrentPageIndex = 0
      RecordNumbers()
End Sub

Sub GoToPreviousRecord(sender as object, e as eventargs)
      MyDataGrid.CurrentPageIndex = Math.Max(MyDataGrid.CurrentPageIndex -1,0)
      RecordNumbers()
End Sub

Sub GoToNextRecord(sender as object, e as eventargs)
      MyDataGrid.CurrentPageIndex = Math.Min(MyDataGrid.CurrentpageIndex + 1, MyDataGrid.pageCount - 1)
      RecordNumbers()
End Sub

Sub GoToLastRecord(sender as object, e as eventargs)
      MyDataGrid.CurrentPageIndex = MyDataGrid.PageCount - 1
      RecordNumbers()
End Sub

Sub GoToChosenRecord(sender as object, e as eventargs)
      '## should only allow integers, currently gives an error if not(integers) are typed in!
      if (txtGoTo.text > 0) and (txtGoTo.text <= MyDataGrid.pageCount) then
            MyDataGrid.CurrentPageIndex = txtGoTo.text - 1
            RecordNumbers()
      end if
End Sub

Sub RecordNumbers()
      lblCurrentRecord.text = MyDataGrid.CurrentPageIndex + 1
      lblTotalRecords.text = MyDataGrid.pageCount
      BindData()
End Sub
0
 
RejojohnyCommented:
I agree that narmi2 went with a different approach and was able to solve his problem .. but I did spend time trying to understand his old code and commenting on where he has to correct it .. pls comment .,..
0
 
narmi2Author Commented:
Rejojohny - i have no problem giving you and dharmesh_amity some points if that is possible.  i was not sure what th admin reaction would be.

please advice admin!
0
All Courses

From novice to tech pro — start learning today.