Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

cycle through dataset

Posted on 2006-11-09
9
Medium Priority
?
599 Views
Last Modified: 2010-08-05
Hello, I have a dataset populated from a dataadapter (code below) the dataset could have any amount of rows from 1-10.
I want to use four buttons: first, next, previous, last to cycle/iterate through the dataset; going foward and backward... You get the idea. Like paging with an asp.net formview or gridview.

How can this be accomplished with the click events of the four buttons?

Here is my code:

Private Sub fillLifeIssuesCB()
        Dim theSQL As String
        Dim DA As OleDb.OleDbDataAdapter
        Dim dsTitle As DataSet
        theSQL = "SELECT DISTINCT ltrim(Title) as Title from LifeIssues"
        DA = New OleDb.OleDbDataAdapter(theSQL, CN)
        dsTitle = New DataSet
        DA.Fill(dsTitle)

        cboTitle.DataSource = dsTitle.Tables(0).DefaultView
        cboTitle.DisplayMember = dsTitle.Tables(0).Columns("Title").ToString

    End Sub

    Private Sub cboTitle_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTitle.SelectedValueChanged
        Dim title As String = cboTitle.SelectedValue.Item("Title")

        Dim theSql As String
        Dim DA As OleDb.OleDbDataAdapter
        Dim dsLifeIssues As DataSet
        Dim strTitle As String = title
        theSql = "SELECT ID, Title, Question, Commentary, Verse, Passage FROM LifeIssues WHERE title = '" & strTitle & "'"
        DA = New OleDb.OleDbDataAdapter(theSql, CN)
        dsLifeIssues = New DataSet
        DA.Fill(dsLifeIssues)

        lblTitle.Text = dsLifeIssues.Tables(0).Rows(0).Item("Title")
        lblQuestion.Text = dsLifeIssues.Tables(0).Rows(0).Item("Question")
        lblCommentary.Text = dsLifeIssues.Tables(0).Rows(0).Item("Commentary")
        lblVerse.Text = dsLifeIssues.Tables(0).Rows(0).Item("Verse")
        lblPassage.Text = dsLifeIssues.Tables(0).Rows(0).Item("Passage")

    End Sub







0
Comment
Question by:gogetsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 39

Expert Comment

by:appari
ID: 17912183
try these

 Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)
                If myCurrencyManager.Position = 0 Then
                    myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position - 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)

                myCurrencyManager.Position = myCurrencyManager.Position + 1
                If myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1 Then
                    myCurrencyManager.Position = 0
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position + 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
0
 

Author Comment

by:gogetsome
ID: 17912269
Thanks Appari for helping. I'm spoiled with asp.net.
The code you provided is looping through the combobox values and is throwing this error:

unable to cast object of type 'system.data.dataview' to type 'system.data.datatable'.

The value of the combobox is used to filter the select statement for dsLifeIssues dataset. The dsLifeIssues dataset is the one I want to loop through.
0
 
LVL 39

Expert Comment

by:appari
ID: 17912270
small mistake in

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Try
            With cboTitle
                Dim myCurrencyManager As CurrencyManager
                myCurrencyManager = CType(.BindingContext(.DataSource), CurrencyManager)

                If myCurrencyManager.Position = CType(.DataSource, DataTable).Rows.Count - 1 Then
                    myCurrencyManager.Position = 0
                Else
                    myCurrencyManager.Position = myCurrencyManager.Position + 1
                End If


            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:gogetsome
ID: 17912305
No that did not do anything. If I comment out the   MsgBox(ex.Message) the code works well to cycle through the combobox's values. BUT, the combobox value is only used to filter (see the WHERE condition above) the data of which populates the second dataset. The second dataset is the one I want to loop through.
For example: if the user selects the abortion value of the combobox that value is used for the dslifesiiues datset to bring back the rows where the title = abortion. It is those records that I want to loop through and not the combobox.
Am I missing something here?
0
 

Author Comment

by:gogetsome
ID: 17912331
One more though that might help.

If I change
 txtPassage.Text = dsLifeIssues.Tables(0).Rows(0).Item("Passage")

to

 txtPassage.Text = dsLifeIssues.Tables(0).Rows(1).Item("Passage")

where I change the row value to 1 the next abortion row is displayed.
0
 
LVL 39

Expert Comment

by:appari
ID: 17912362
try something like this


      

add a variable at form level

    private curRowIndex as integer = 0

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                If curRowIndex = 0 Then
                    curRowIndex = dsLifeIssues.Tables(0).Rows.Count - 1
                Else
                    curRowIndex  = curRowIndex  - 1
                End If

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

   Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        Try
            With cboTitle
                If curRowIndex =  dsLifeIssues.Tables(0).Rows.Count - 1 Then
                    curRowIndex = 0
                Else
                    curRowIndex  = curRowIndex  + 1
                End If

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub
0
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 17912367
for first and last records

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        Try
            With cboTitle

                curRowIndex = 0

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

   Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        Try
            With cboTitle

                curRowIndex =  dsLifeIssues.Tables(0).Rows.Count - 1

             lblTitle.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Title")
              lblQuestion.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Question")
              lblCommentary.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Commentary")
              lblVerse.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Verse")
              lblPassage.Text = dsLifeIssues.Tables(0).Rows(curRowIndex).Item("Passage")

            End With

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try
    End Sub

0
 
LVL 3

Expert Comment

by:vsvb
ID: 17913672
This is some different approch

1) Declare currentpos variable on your form base
2) suppose you have 4 button first,next,previous,last and call this procedure on each button
   

  Private Sub ButtonFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFirst.Click

        With Me.DataGridView1
            .AutoGenerateColumns = True
            .DataSource = GetDataPos(dt, 1, 10).DefaultView  'here dt = your dataset, (1=first,2=next,3=pre,4=last),    
                                                                                      10=  your row you want to display on each page)

            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
            .BorderStyle = BorderStyle.None
            .EditMode = DataGridViewEditMode.EditOnEnter
            .Refresh()
        End With
    End Sub

3) the procedure is simple its takes your dataset as parameter, your navigation flag, your differnece mostly this is
   static but i wrote it dynamic

Function GetDataPos(ByVal ds As DataSet, ByVal pos As Integer, ByVal diff As Integer) As DataTable
        'pos = 1 as first,2 as next,3 as previous, 4 as last
        Dim dt As DataTable
        dt = ds.Tables(0).Clone
        Dim i As Integer
        Dim startpoint As Integer, endpoint As Integer
        Select Case pos
            Case 1 'first
                startpoint = 0
                endpoint = startpoint + diff
            Case 4 'last
                startpoint = ds.Tables(0).Rows.Count - diff
                endpoint = ds.Tables(0).Rows.Count - 1
            Case 3 'next
                startpoint = currentpos
                endpoint = startpoint + diff
                If endpoint > ds.Tables(0).Rows.Count - 1 Then
                    endpoint = startpoint - ds.Tables(0).Rows.Count - 1
                End If
            Case 2 'previous
                startpoint = startpoint - diff
                endpoint = currentpos
                If startpoint < 0 Then
                    startpoint = 0
                    endpoint = diff
                End If
        End Select
        Dim dr As DataRow
        For i = startpoint To endpoint
            dr = dt.NewRow
            dr.ItemArray = ds.Tables(0).Rows(i).ItemArray
            dt.Rows.Add(dr)
        Next
        Return dt
    End Function

may be you need some testing but you have to test inside this procedure which returns selected rows
and that you directly bind with your datagridview

-best luck
vsvb
0
 

Author Comment

by:gogetsome
ID: 17916956
Thanks for your efforts Appari. It know works.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

647 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