Solved

cycle through dataset

Posted on 2006-11-09
9
578 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
  • 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

12 Experts available now in Live!

Get 1:1 Help Now