Click here to Skip to main content
Click here to Skip to main content

Dynamic Reports UsingSQL Queries

By , 23 Nov 2005
 

Sample Image

Sample Image- Multilines

Introduction

Microsoft Visual Studio .NET provides Crystal Reports for report printing and throws the DataReport component (Visual Studio 6) away from .NET. Although, Crystal Reports is a good report creator, sometimes when we want to create dynamic reports, maybe for listing something or for data summation, it is not so suitable. Crystal Reports can be used for dynamic reports but it has a fixed set of column types and the number of maximum columns.

So I use the PrintDocument component, in the System.Drawing.Printing namespace of the .NET base library. PrintDocument is a component like a plain paper. You can draw or print anything on it. Normally, you have to set the object coordination and other properties just like when you use a pen to write on the paper. But for this code, you just use a SQL query or a dataset only!

Using the code

Add a form to a project and a PrintDocument component to it. And use a DataSet, and specify the column of the first table (Table(0)) by strict format (1 set per 1 report column):

  • Column 1 -> Column name.
  • Column 2 -> Start position on paper, I set that all width has range between 0-99.
  • Column 3 -> End position on paper, I set that all width has range between 0-99.
  • Column 4 -> Justify (L-Left, R-Right, C-Center).
  • Column 5 -> Has summarize in this column (Y/N).
  • Column 6 -> Display format (such as #,##0.00).
  • Column 7 -> Rest in line? Begin with 1.
  • Column 8 -> Data column.

Please look at the example, it's easy to understand.

We can set the properties of PageSetDlg and prnSetDlg objects for the print options. I use the Form component and I put the PrintDocument, PrintPreviewDialog, PageSetupDialog, and PrintDialog components on it. Then, we can use these for direct printing or use a print dialog too. We can set the properties of those objects for a specific print option such as setting the page orientation to landscape, etc.

'-------- Example use, If you haved data in
'           dataset already. --------------

'--- We can set properties of PageSetDlg
    ' and prnSetDlg object for print option 
    'frmDynamicPrint.PageSetDlg.PageSettings.Landscape = True

    '---If it has sum column is this report,
    '   we have to set hasSum flag to true. 
    frmDynamicPrint.hasSum = True

'---Initialization
    frmDynamicPrint.FontName = "Tahoma"
    frmDynamicPrint.FontSize = 12
    frmDynamicPrint.FontSizeHead = 16
    frmDynamicPrint.FontSizeHead2 = 14
    frmDynamicPrint.LineHeight = 1
    frmDynamicPrint.SkipLinePerRecord = 1

    '---Call InitPrint for inital some properties,
    ' you can modify it.
    frmDynamicPrint.InitPrint()

    frmDynamicPrint.HeadFirstString = _
      "Report for Product Order Value Summary"
    frmDynamicPrint.HeadSecondString = "Test Report"
    frmDynamicPrint.DateString = Now.ToString
    frmDynamicPrint.PrintPreview()
    frmDynamicPrint.Dispose()

Points of Interest

I will develop an HTML report later or will add a feature to export a report to another format.

History

  • November 27, 2005 - Fixed a little bug in the example in setting the page number.
  • November 24, 2005 - Fixed a big bug in the query loop and the summation method. I'm very sorry for my late bug fixing.
  • October 05, 2005 - Added the wrap text feature.
  • September 13, 2005 - Add feature for multiple lines and setting height of each line. It can be used like the wrap text feature too.
  • September 12, 2005 - Changed the column position addressing on paper. Use start-end positions instead of one position.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Pakorn Indhatep
Web Developer
Thailand Thailand
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionC#???memberrobertdlm18 Jul '12 - 17:57 
can you post in C#??
QuestionDisplaying the values of fieldmemberElie Ghanoum19 May '08 - 11:48 
after I got the values of certain field. it always display them in vertical way. How can I display them in horizontal way
GeneralError on printing - Infinity page print!!!memberMember 179106330 Mar '08 - 22:49 
Hi,
 
When I tried to print first time, i got an error saying "there is no date at row ##" then i insert the fallowing lines into prnDoc_PrintPage function
 

PageNumber = 0
SumList.Clear()
dbCursor = 0L
 

There's the actual problem comes up, it try to print infinity number of pages, last page number I saw was 986 which is kinda infinity already for 2 page report I guess.I should also say that all pages are the first page, non of these 986 pages contains any data from the second page! Frown | :(
 
PS: i didn't actually print them on paper, I just saved the output as PDF file. Smile | :)
GeneralNew comer Need Help...PleasememberSamRST22 Sep '07 - 20:01 
Hi friends,
i'm new in windows application. can u please give me an idea about the crystal report generation. i searched lot but in confusion..different articles in different ways.. help me please
cheers..
GeneralDbnull Values and Columns Not found after Countmemberjcviera6 Feb '07 - 7:37 
I tried using this with a Stored Procedure and Filling the dbset with an adapter I used from my object and that all worked but It gave me the error that it could not find Column 46 but there is no Column 46 so what do I do now?
 
Error was: out of Range Exception
 
thanks,
 
Jason
GeneralSource code Bugsmemberdanmarin9 Dec '06 - 7:39 
There is a swarm of bugs in this code, unfortunately it is a nice piece of code , but it should be fixed so that others can take a chance at it.
First of all:
1) The document does not print, i guess it goes beyond the end of the dataset as the message is "there is no row at position 72"
2) If you try the "fixes" proposed here it will just loop infinitely
3) It is a huge chunk of code , it should be broken down in small units in order to read it properly.
4) if you try to comment any of the try-catch structures the document crashes "method not supported" this means that with the try -catch mechanism suppresses some other error.
 
Hope this feedback helps and also i hope you still want to edit this post as it is a promising piece of code, nice idea as you tried to approach it from a different angle.
 
Hope for a fast feedback.
 
Dan
.NET DEVELOPER
 
"The universe and the developers were always in a war. Developers trying to create idiot-proof programs and the universe creating idiots. The universe seems to win....D'Oh! | :doh: Frown | :( img src="/script/Forums/Images/smiley_doh.gif" align="top" alt="D'Oh! | :doh:" />
GeneralPage NumbersmemberRichard Elias11 Sep '06 - 22:02 
I noticed that the pagenumber is not increased while having multiple pages.
This could be fixed in the same area that resets dbCursor
I solved it in the following manner and that works fine, including printing and reprinting.
If hasSum Then
If Not (dbCursor = dbSet.Tables(0).Rows.Count) And (Not printSumComplete) Then
e.HasMorePages = True
PageNumber += 1
Else
e.HasMorePages = False
dbCursor = 0
PageNumber = 1
End If
Else
If Not (dbCursor = dbSet.Tables(0).Rows.Count) Then
e.HasMorePages = True
PageNumber += 1
Else
e.HasMorePages = False
dbCursor = 0
PageNumber = 1
End If
End If
 
Richard
GeneralPrint problemmemberRichard Elias11 Sep '06 - 3:02 
Dear Pakorn,
 
If you want to print the preview, an error occurs on table boudaries.
This is caused by the dbCursor that follows the datatable.
The problem can be overcome by resetting dbCursor fter the last page is printed.
If hasSum Then
If Not (dbCursor = dbSet.Tables(0).Rows.Count) And (Not printSumComplete) Then
e.HasMorePages = True
Else
e.HasMorePages = False
dbCursor = 0
End If
Else
If Not (dbCursor = dbSet.Tables(0).Rows.Count) Then
e.HasMorePages = True
Else
e.HasMorePages = False
dbCursor = 0
End If
End If
 
If the dbCursor = 0 line is inserted in both scenarios, I am now able to reprint the report as often as I want.
 
Hope this is of help.
 
With kind regsrd,
 

 
Richard Elias

QuestionPage header & footer?memberpoetyip24 Aug '06 - 5:40 
I am wondering if this dynamic report can have something like pageheader or pagefooter? It is because I have 2 db want to print. The header would like to put in pageheader, and detail in your normal details section.
Questioncan't print multiple page problemmemberTony567887657 Jul '06 - 6:46 
When I click the print button, if the report page more than 1,
there is a message box "Generating Preview" show "page xxx of document" never stop, I think it's a recursive loop.
I don't know how to fix this problem.
Thanks a lot!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 Nov 2005
Article Copyright 2005 by Pakorn Indhatep
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid