How do I know if CopyToDataTable doesn't have rows so that blank pages don't appear in vb.net?
is there something wrong with my code?
Below is also the solution code but how can I implement it with my code. Thanks
'I found the code solution below but I didn't implement it in my code
If payslips.LastOrDefault() IsNot payslip Then
report.NewPage()
End If
Private Sub Generate()
Dim dt As New DataTable()
report.Clear()
Dim query = "Select EMPID,[NAME],MONTHLYSALARY From tblemployee"
Using adapter As New OleDbDataAdapter(query, Con)
adapter.Fill(dt)
End Using
For Each row As DataRow In dt.Rows
Dim table = {row}.CopyToDataTable()
report.AddDataTable(table)
report.NewPage()
Next row
End Sub
The point here is that, every time you call NewPage
, a new page is added. If there is one page by default and you call NewPage
for every row, you're going to end up with one more page than rows, which is exactly what you're seeing.
A solution is to call BEFORE adding the data to be printed on that page, if and only if a new page is needed. Ther first row will be printed on the default page, so you'll only need to call NewPage
for the second row and later. By using a For
loop instead of a For Each
loop, you have a row index that you can test to determine whether you're on the first row or not:
For i = 0 To dt.Rows.Count - 1
Dim row = dt.Rows(i)
If i > 0 Then
'We are not on the first row so we need a new page.
report.NewPage()
End If
'...
Next