Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,
I'm facing a problem says "There is no row at position 0." ... I got this code from this site Pagination
Really it's so helpful and with some few modifications , i got rid of some problems like 'There is no row at position 0.' but it appears randomly ....
I will have to display all my code to facilitate the sight
Public Function GetData() As DataView
        
        Dim SelectQry = "SELECT BillInfo.InvoiceNo as [رقم الايصال],
                                    ProductSold.SoldDate as [تاريخ البيع],                                    
                                    BillInfo.CustomerName as [اسم العميل],
                                    ProductSold.ProductCode as [كود الصنف], 
                                    ProductSold.ProductName as [اسم الصنف],
                                    ProductSold.Price as [سعر التكلفة],
                                    ProductSold.Category as [اسم الباكية], 
                                    ProductSold.AVP as [الكمية المتاحة عند البيع], 
                                    ProductSold.SellPrice as [سعر البيع],
                                    ProductSold.SoldPackets as [الكمية المباعة], 
                                    ProductSold.TotalAmount as [الاجمالى المبيعات من الصنف],
                                    ProductSold.NewAVP as [الكمية المتاحة الجديدة]
                                    FROM BillInfo INNER JOIN ProductSold ON BillInfo.InvoiceNo = ProductSold.InvoiceNo 
                                    WHERE SoldDate >= @D1 AND SoldDate <= @D2
                                    ORDER BY SoldDate DESC"
        Dim ds As New DataSet
        Dim dv As DataView
        Try
            conn = New OleDbConnection(cs)
            Dim cmd As New OleDbCommand()
            Dim da = New OleDbDataAdapter()
            cmd.CommandText = SelectQry
            cmd.Connection = conn
            cmd.Parameters.Add("@D1", OleDbType.DBDate).Value = dtpInvoiceDateFrom.Value
            cmd.Parameters.Add("@D2", OleDbType.DBDate).Value = dtpInvoiceDateTo.Value

            da.SelectCommand = cmd
            da.Fill(ds)
            dv = ds.Tables(0).DefaultView
        Catch ex As Exception
            Throw ex
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return dv
    End Function

    Private Sub loadDGV(Statement As String, TableName As String)

        Using conn As New OleDbConnection(cs)
            conn.Open()
            Using cmd As New OleDbCommand(Statement, conn)
                'cmd.Parameters.Add("@ID", SqlDbType.Int).Value = CInt(Val(txt))

                'Set the DataAdapter's query.
                da = New OleDbDataAdapter(cmd)
                ds = New DataSet()

                ' Fill the DataSet.
                da.Fill(ds, TableName)
                'da.Fill(ds, TableName1)

                ' Set the source table.
                dtSource = ds.Tables(TableName)
                'dtSource = ds.Tables(TableName1)
                'dt = ds1.Tables(TableName1)
            End Using
        End Using
    End Sub

    Private Sub LoadPage()
        Dim i As Integer
        Dim startRec As Integer
        Dim endRec As Integer
        Dim dtTemp As DataTable
       
       
        Select Case True
            Case DataGridView1.RowCount = Nothing
                Exit Sub
        End Select
        'Duplicate or clone the source table to create the temporary table.
        dtTemp = dtSource.Clone
       

        If currentPage = PageCount Then
            endRec = maxRec
        Else
            endRec = pageSize * currentPage
        End If

        startRec = recNo

        'Copy the rows from the source table to fill the temporary table.
        For i = startRec To endRec - 1
            dtTemp.ImportRow(dtSource.Rows(i))
            recNo = recNo + 1
        Next

        DataGridView1.DataSource = dtTemp
        
        DisplayPageInfo()

    End Sub

    Private Function CheckFillButton() As Boolean

        'Check if the user clicks the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("Set the Page Size, and then click the button!", "Hint")
            CheckFillButton = False
        Else
            CheckFillButton = True
        End If
    End Function

    Private Sub DisplayPageInfo()
        txtDisplayPageNo.Text = "Page " & currentPage.ToString & "/ " & PageCount.ToString
    End Sub

    Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
        If Not CheckFillButton() Then Return

        btnLast.Enabled = False
        btnPrevious.Enabled = True
        btnNext.Enabled = False
        btnFirst.Enabled = True

        ' Check if you are already at the last page.
        If recNo = maxRec Then
            MessageBox.Show("You are at the Last Page!", "Hint")
            
            Return
        End If

        currentPage = PageCount

        recNo = pageSize * (currentPage - 1)

        LoadPage()
    End Sub

    Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
        If Not CheckFillButton() Then Return
       
        btnNext.Enabled = True
        btnLast.Enabled = True

        If currentPage = PageCount Then
            recNo = pageSize * (currentPage - 2)
        End If

        currentPage = currentPage - 1

        'Check if you are already at the first page.
        If currentPage < 1 Then
            MessageBox.Show("You are at the First Page!")
            btnPrevious.Enabled = False
            
            btnFirst.Enabled = False
          
            currentPage = 1
            Return
        Else
            recNo = pageSize * (currentPage - 1)
        End If

        LoadPage()
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        'If the user did not click the "Fill Grid" button then Return
        If Not CheckFillButton() Then Return

        btnPrevious.Enabled = True
        btnFirst.Enabled = True
        
        'Check if the user clicked the "Fill Grid" button.
        If pageSize = 0 Then
            MessageBox.Show("Set the Page Size, and then click the button!")
            Return
        End If

        currentPage = currentPage + 1

        If currentPage > PageCount Then
            currentPage = PageCount

            'Check if you are already at the last page.
            If recNo = maxRec Then
                MessageBox.Show("You are at the Last Page!")
                btnNext.Enabled = False
              
                btnLast.Enabled = False
                Return
            End If
        End If

        LoadPage()
    End Sub

    Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
        If Not CheckFillButton() Then Return
        
        btnFirst.Enabled = False
        btnPrevious.Enabled = False
        btnNext.Enabled = True
        btnLast.Enabled = True

        ' Check if you are already at the first page.
        If currentPage = 1 Then
            MessageBox.Show("You are at the First Page!")
         
            Return
        End If

        currentPage = 1
        recNo = 0

        LoadPage()
    End Sub

    Private Sub btnFill_Click(sender As Object, e As EventArgs) Handles btnFill.Click

        Using conn As New OleDbConnection(cs)
            conn.Open()
            Using cmd As New OleDbCommand("SELECT BillInfo.InvoiceNo as [رقم الايصال],
                                    ProductSold.SoldDate as [تاريخ البيع],                                    
                                    BillInfo.CustomerName as [اسم العميل],
                                    ProductSold.ProductCode as [كود الصنف], 
                                    ProductSold.ProductName as [اسم الصنف],
                                    ProductSold.Price as [سعر التكلفة],
                                    ProductSold.Category as [اسم الباكية], 
                                    ProductSold.AVP as [الكمية المتاحة عند البيع], 
                                    ProductSold.SellPrice as [سعر البيع],
                                    ProductSold.SoldPackets as [الكمية المباعة], 
                                    ProductSold.TotalAmount as [الاجمالى المبيعات من الصنف],
                                    ProductSold.NewAVP as [الكمية المتاحة الجديدة]
                                    FROM BillInfo INNER JOIN ProductSold ON BillInfo.InvoiceNo = ProductSold.InvoiceNo 
                                    WHERE SoldDate >= @D1 AND SoldDate <= @D2
                                    ORDER BY SoldDate DESC", conn)
                cmd.Parameters.Add("@D1", OleDbType.DBDate).Value = dtpInvoiceDateFrom.Value
                cmd.Parameters.Add("@D2", OleDbType.DBDate).Value = dtpInvoiceDateTo.Value

                'Set the DataAdapter's query.
                da = New OleDbDataAdapter(cmd)
                ds = New DataSet()

                ' Fill the DataSet.
                da.Fill(ds, "BillInfo")
                

                ' Set the source table.
                dtSource = ds.Tables("BillInfo")

            End Using
        End Using

        'Set the start and max records. 
        pageSize = CInt(Val(txtPageSize.Text))
        maxRec = dtSource.Rows.Count

        PageCount = maxRec \ pageSize

        ' Adjust the page number if the last page contains a partial page.
        If (maxRec Mod pageSize) > 0 Then
            PageCount = PageCount + 1
        End If

        'Initial seeings
        currentPage = 1
        recNo = 0

        ' Display the content of the current page.
        LoadPage()
    End Sub

    Private Sub txtPageSize_TextChanged(sender As Object, e As EventArgs) Handles txtPageSize.TextChanged
        If Val(txtPageSize.Text) > 100 Then
            Exit Sub
        End If
        Select Case True
            Case Val(txtPageSize.Text) = 0 Or txtPageSize.Text = ""
                
                Exit Sub
        End Select

        btnFill_Click(sender, e)
        btnFirst.Enabled = False
        btnPrevious.Enabled = False
        btnNext.Enabled = True
        btnLast.Enabled = True
    End Sub


Any suggestions .
Thanks in advance .......................
Regards from Amr Aly

What I have tried:

I tried to avoid the exception by doing this

btnFirst.Enabled = False
      btnPrevious.Enabled = False
      btnNext.Enabled = True
      btnLast.Enabled = True

As the above code says but the exception appears again ..... And now during writing my question an idea came in my mind to avoid the exception but useless
by doing a select case in every button like this
Select Case True
        Case DataGridView1.RowCount = Nothing
            Exit Sub
        Case recNo = maxRec
            MessageBox.Show("You are at the Last Page!", "Hint")
            Exit Sub
    End Select
    LoadPage()


Because the error occurred in the LoadPage sub
This is an image of my Exception .. With 'first' button the error is 'There is no row at position 0.'
and with 'next' button the error is 'There is no row at position -50.'

Sorry I forgot to say that the error always occurs in this line
dtTemp.ImportRow(dtSource.Rows(i))
Posted
Updated 27-Dec-18 19:06pm
v8
Comments
Bryian Tan 27-Dec-18 17:21pm    
Maybe the query not returning any results?
amr aly 28-Dec-18 1:50am    
the query returns with results ... error occurs when the result of the query is less than 2 pages(one page)... and if i click on the button when the gridview is empty with no data
MadMyche 27-Dec-18 21:17pm    
Can you fix the link to the site you got this code from? Right now it points to this post
amr aly 28-Dec-18 1:00am    
I fixed it ......sorry for this problem

1 solution

0. You need to understand the error. As far as I can tell, you are trying different things in an effort to avoid the error, rather than fixing why the error is occurring.
1. The error means that you are trying to access row 0 but there is no row 0 because there is no data.
2. When asking for help, point out which line of code is causing the error.

But the main issue here is that you need to understand what your code does and not randomly try different things in trying to fix an error you do not understand.
 
Share this answer
 
Comments
amr aly 28-Dec-18 1:11am    
Thanks for replying and Sorry for this, The error always occurs in this line
 'Copy the rows from the source table to fill the temporary table          dtTemp.ImportRow(dtSource.Rows(i))
ZurdoDev 28-Dec-18 9:36am    
Use your debugging tools and figure out why dtSource does not have any data.

That code is in your LoadPage event but dtSource gets its data in the loadDGV which is not called yet.
amr aly 3-Jan-19 11:47am    
I solved my issue.Thanks ZurdoDev your comments guided me to the right way ..... But I have an inquiry, why my application in Access database 2013 display this error(that i have fixed it) and the same application(Copy of the application in access, i have made it in SQL server Express) did not has this error and i used my above code only....
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900