Multicolumn Pictures in Crystal Reports

Hello guys, its been a while since a posted something on this blog. I was working on this project where an item can have as many pictures as possible, as part of the requirement , a report was to be generated to show the item information and its pictures ( in a multicolumn way).

Below are the steps to show pictures in crystal reports (Note that the the data type used is image and hence using datasets as the data source for the reports, the picture can easily be dragged onto the report)

1. To start, the picture is dragged to the detail section of the report as shown in the picture below

image

2. Then right click on the “detail section” tab and choose the “section expert” as shown in the picture below

image

3. In the dialog box that appear, make sure the “details” section is selected and on the right hand side, on the “common” tab make sure “Format with Multiple columns “ is checked. The move to the “Layout” tab, make the required changes and make sure in the “printing direction” section you select “across” and down.

image

4. You are now done, with the reports, run you program and something as shown in the picture below should be shown.

image

CREATING PARAMETERIZED REPORTS WITH REPORTING SERVICES

In our previous post (https://gilbertadjin.wordpress.com/2010/07/05/reporting-with-reporting-services/) we went through how to create  reports with reporting services. In instances where you want to insert some records onto the reports which are not part of the data source, for example, inserting dates into your report title, you need to create parameters. It is assumed you can create reports with reporting services.

In this post we will be adding two parameters “name” and “title” to our reports. After creating the report, in the report data pane (if not shown, go to the menu bar, click on “View” then “Report Data” to show the pane

image

Below is the look of the report data pane

image

In the Report Data pane, right click on the “parameters” node and click on add parameter. A dialog box shown below is presented. Enter the name of your parameter and choose the data type.

image

On the design you are done. Go to you where the report data will be supplied in your code. Make sure to import (“Imports Microsoft.Reporting.WinForms“)

For example we have a procedure which accepts parameters including a collection of parameters

   1: Public Sub fillReport(ByVal dataset As String, ByVal reportname As String, ByVal loadData As DataTable, ByVal parameters As ReportParameterCollection)

   2:        Me.ReportViewer1.ProcessingMode = ProcessingMode.Local

   3:        Me.ReportViewer1.LocalReport.ReportPath = Application.StartupPath + "/" + reportname + ".rdlc"

   4:        Me.ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource(dataset, loadData))

   5:        Me.ReportViewer1.LocalReport.SetParameters(parameters)

   6:        Me.ReportViewer1.RefreshReport()

   7:    End Sub

we then give values to our parameters then we call our method above which will complete the job

   1: title = " Report on Sales made between " & Me.dtpStart.Value.ToShortDateString & " and " & Me.dtpEnd.Value.ToShortDateString

   2: Dim name As String = " Just trying"

   3: Dim parameters As New ReportParameterCollection()

   4: parameters.Add(New ReportParameter("title", title))

   5: parameters.Add(New ReportParameter("name", name))

   6: fillReport("DataSet1", "salesReports", Me.FarmershopeDataSet.SalesReports, parameters)

REPORTING WITH REPORTING SERVICES

Oh ok, so i don’t even need to worry downloading crystal reports for my Visual Studio 2010. Reporting services has been there for a while but did not bother to consider because I have been comfortable with crystal reports, but working on my first project with VS 2010 I had to, because was not seeing crystal reports and had to go through some processes to integrate. So i decided to give Reporting services a try and guys it is very cooooool. Are you a beginner in Reporting Services in Visual Studio, then this will offer you a great help.

Lets go through how to create a report using reporting services and dataset as the data source.

It is assumed you are working with a project with a dataset.

1. Right click to add a new item to your project. On the Reporting tab, choose “Report Wizard” and enter the name of your report in the “name” field provided at the bottom of the “add new item” dialog box. Click on the “Add’ button to proceed.

image

2. In this stage , you choose the data source of your report. You start by entering the name of your Dataset. Take note of the name you enter as you will use it in your code. Select the data source ( the dataset of your project) and then select the table you want to display in your report. Click next to go to the next stage.

image

3.Here, you choose the fields to display on your reports and how to group your records, either by rows or columns. Drag the fields you want to display in the “Values” area. In case you want group your records by rows or columns, drag that field to the “row groups” or “column groups “ respectively. Functions like sum,average, etc can be applied to  fields in the “values’ area. Click next to go the next tab

image 

4. In this stage you choose the layout of your report. Select your layout and click on the next button.

image

5. Select your report style and click on the finish button.

image

 

Your report is then displayed with the selected fields. You can then drag the table to a position of your choice on the report form. You can also insert text and image onto your report as it is done in crystal report.

image

After creating the reports you then have to integrate it into your forms. First you need to add a report viewer. You do this by creating a new form and dragging the report viewer onto it as shown below.

image

After this, you write some small codes to view your report.  First you have to make sure the data table is filled programmatically. Let say at form load event we want to display the report. We start by declaring a procedure that can that accepts the dataset name, report name and the datasource.

   1: public void fillReport(String dataset, String reportname, DataTable loadData)

   2:        {

   3:            this.reportViewer1.ProcessingMode = ProcessingMode.Local;

   4:            //choosing the report source

   5:            this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + "/" + reportname + ".rdlc";

   6:            //add the datasource to the report

   7:            this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(dataset, loadData));

   8:            this.reportViewer1.RefreshReport();

   9:  

  10:        }

so on form load we fill the data table and call the method above to fill the show the report.

“fillReport("DataSet1", "purchaseSearch", this.dataSet1.land”

Note that the dataset name should be the same as shown in the created report.

Enjoy…

HIDING CONTROLS WHEN PRINTING WITH WINDOW.PRINT()

Sometimes in web applications (ASP.NET) we prefer printing a window without some controls that appear on the interface. For example, printing a window by clicking on a print button without showing the print button on the printed document. In this example we are printing a student’s results without showing the Print and the close buttons.

To start, we place the two control in a DIV control.Drag the “DIV” control from the HTML controls section. Name the DIV control by giving a value to ID property as shown below. Place the print and close button in the DIV control.

clip_image002

Note that in creating ASP.NET applications, there are two types of source codes, the VB/C# source code and the HTML code. First we create a script to hide the DIV control as shown below. In the script below the “dv1” is the ID value of the DIV control

Add this script to the HTML source

   1: <script language="javascript" type="text/javascript">

   2:    function hidePrint(){

   3:     document.getElementById("dv1").style.display='none';

   4:     window.print();

   5:     window.close();

   6:             }

   7: </script>

Note that the script should be in between the <head></head> tag of the html source code

Select the print button and go to the html source code and add the onclick property . The value of the onclick property should be the created javascript function (named hidePrint()). The button’s html code should look like the one below after the addition of the onclick property

   1: <input type="button" id="Button1" runat="server" 

   2: value="Print" class="fltbutton"  style="width:60px" 

   3: onclick="javascript:hidePrint();" />

This is all you have to do and you are good to go.

Below is a window showing the student’s results and the Print and the Close Button

clip_image004

What is printed after clicking on the “print” button

clip_image006

Cheers!!!!

REPORTING WITH PRINTDOCUMENT IN .NET

Due to flexibility in creating reports with some tools in the .NET framework , using PrintDocument() class in creating reports is fading out. But sometimes you don’t have a choice but to use it. For instance creating reports without any predefined data source or generating reports where needed columns are determined at runtime.

In this post we will go through creating a report using the PrintDocument. Our datasoure will also come from a listview which contains a list of file types and their sizes from an indexed drive. When using the report document everything is drawn to the report interface, thus you draw a string, an image,a line. the difficulty in here also is, you yourself determine at runtime where your text, lines and images should be positioned

To start with, first drag a PrintPreviewDialog control on your form. The control is named “PrintPreview1”.

image

Then go to the code view and create  your procedure below.

Private Sub detaildisksreport_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Static count As Integer = 0
        Dim x, y, c, i As Integer
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim topMargin As Single = e.MarginBounds.Top

        ‘declaring fonts to use in your reports
        Dim backFont As New Font("Arial", 10, FontStyle.Italic)
        Dim HeaderFont As New Font("Arial", 20, FontStyle.Bold)
        Dim normfont As New Font("Times New Roman", 18, FontStyle.Regular)

        ‘ Calculate the number of lines per page.
        linesPerPage = e.MarginBounds.Height / normfont.GetHeight(e.Graphics)

        ‘x determines the horizontal positions whiles y determines the vertical position

        ‘ Drawing an image on top of the report
        Dim b As Rectangle
        Dim pic As New Rectangle(40, 5, pic1.Width + 40, pic1.Height + 40)
        e.Graphics.DrawImage(pic1.Image, pic)

        ‘drawing a line around the image
        b = New Rectangle(40, 5, pic1.Width + 40, pic1.Height + 40)
        e.Graphics.DrawRectangle(Pens.Black, b)

        linesPerPage = e.MarginBounds.Height / normfont.GetHeight(e.Graphics)
        linesPerPage = linesPerPage – 5

        ‘ Drawing our report titles
        x = 190
        y = pic1.Height + 70

        e.Graphics.DrawString("DETAIL DRIVE REPORT", HeaderFont, Brushes.Black, x, y)

        y += 40
        x = 25
        e.Graphics.DrawString(" FOLDERS ON DRIVE :  " & Me.cmbCat.SelectedItem.ToString.ToUpper, HeaderFont, Brushes.Black, x, y)
        y += 40
        x = 10
        e.Graphics.DrawString("REPORT GENERATED ON " & Now.ToLongDateString.ToUpper, HeaderFont, Brushes.Black, x, y)
        y += 10
        x = 5

        e.Graphics.DrawLine(Pens.Black, 5, y + 30, 820, y + 30)
        x = 10
        y += 60
        c = y

        e.Graphics.DrawString("FOLDER NAME", HeaderFont, Brushes.Blue, x, y)
        x += 260

        e.Graphics.DrawString("NUMBER OF FILES", HeaderFont, Brushes.Blue, x, y)
        x += 330

        e.Graphics.DrawString("FOLDER SIZE", HeaderFont, Brushes.Blue, x, y)

        e.Graphics.DrawLine(Pens.Black, 5, y + 30, 820, y + 30)

        x = 33
        y = c + 40

        i = 0
        ‘adding the content to the report
        For i = count To Me.LsvCDs.Items.Count – 1

            Dim len As Integer
            If LsvCDs.Items.Item(i).SubItems(0).Text.Length > 25 Then
                len = 25
            Else
                len = LsvCDs.Items.Item(i).SubItems(0).Text.Length
            End If

            e.Graphics.DrawImage(Me.SmallImages.Images(0), 15, y + 2)

            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(0).Text.Substring(0, len), normfont, Brushes.Blue, x, y)
            x += 400
            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(1).Text, normfont, Brushes.Blue, x, y)
            x += 220
            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(2).Text, normfont, Brushes.Blue, x, y)

            ‘setting the y value to point to the next line
            ‘reseting the x value to the margin
            y += 30
            x = 33

            ‘checking to see if the current page is full
            If y + normfont.Height > e.PageSettings.PaperSize.Height Then
                count = i + 1
                e.HasMorePages = True
                Return

            End If
        Next

        count = 0
        e.HasMorePages = False
    End Sub

Also add the code below to a button click event and you are good to go.

   1: Dim pd As New PrintDocument()

   2: 'adding the print method created to the printdocument printpage event

   3: AddHandler pd.PrintPage, AddressOf disksreport_PrintPage

   4:  

   5: 'passing the print doucument to the PrintPreviewDialog

   6: PrintPreview1.Document = pd

   7: PrintPreview1.ShowDialog()

Below are sample interfaces, using printDocument to generate reports

image

image

DEPLOYING ASP.NET APPS USING IIS 7

You have finished developing your ASP.net application using VS 2005 in Windows Vista environment and you need to deploy, just go ahead and deploy in the vista environment, you do not have to look for Windows XP before you can do this. Here are few steps to follow to deploy your application using IIS 7 .

  1. First make sure all the components of the IIS server is installed on your system. To do this, Go to Start > Control Panel >Programs and Features and the click on the "Turn Windows features on or off" on the left task pane of the window as shown below.

  2. On the windows feature dialog box that pops up, make sure the Internet Information Service node and all its child nodes are checked as shown below or else get your original Windows Vista CD, check all the nodes and install these features.

  3. To access the IIS Manager after the installation, Go to Start>Control Panel>Administrative Tools >Internet Information Services (IIS) Manager or go to run (Window Key + R) and enter "inetmgr" and you will get an interface like the one below.

  4. Now we are good to go on and deploy our ASP.Net Web Application. Navigate to the Default Web Site Node, then right click on it and choose "Add Application" from the context menu. A dialog box will pop up as shown below. The alias is the name of your website, for example if you enter Ghana, or accra , or kumasi, when accessing your website from your local computer, you will be typing "localhost/Ghana, or localhost/accra, or localhost/Kumasi"

    Click on the "Physical path:" button to browse for the website folder of your application and click on the "OK" button.

  5. A new node with the name you entered in the alias textbox will appear under the "Default Web Site" node. Select the node, and make sure your connection string, default documents are set to the required values. The picture below clarify these

  6. After making the required changes, you are done. Make sure your IIS server is running (you can check this by clicking on the "Default Web Site" node and on the right task pane, under manage web site, make sure the "star" link is disabled else click on it to start the server). Go to your browser and enter your URL, in this case "http://localhost/ghana&quot; or right click your application from IIS GUI and select browse.

EXPORTING THE CONTENT OF A DATASOURCE INTO EXCEL

Hi fellows, this post is about exporting the content of your data source (dataset, data table, data grid) into MS-Excel using VB.NET. All you need to is to read the data from your data source into a 2-dimensional array and then send the content straight to excel. With this you don’t have to be adding rows to excel one after the other.

Let’s start, we have a data table called “dsstudents” with 5 columns “IndexNo, Name, Year Group, Program, Gender”. We then declare a variable Private Students(,) As String as our 2-dimensional array. We are then ready to go (code). Note that we are using late binding to the excel objects here, thus we are not referencing the excel object at the design time but it’s done in runtime.

Below are two methods that will get the job done. Cheers!!!!

Private Sub readContent()

If Me.dsStudent.Rows.Count > 0 Then
ReDim Me.courses(Me.dsStudent.Rows.Count + 2, 7)

For i As Integer = 0 To
Me.dsStudent.Rows.Count – 1

‘setting the column headers for each column

students(0, 0) = "Index No"

students(0, 1) = "Students’ Name"

students(0, 5) = "Year group"

students(0, 6) = "Program Name"

students(0, 11) = "Student Gender"

‘getting the actual content from our datasource

students(i + 2, 0) = Me.dsStudent.Rows(i)(0).ToString

students(i + 2, 1) = Me.dsStudent.Rows(i)(1).ToString

students(i + 2, 3) = Me.dsStudent.Rows(i)(2).ToString

students(i + 2, 4) = Me.dsStudent.Rows(i)(3).ToString

students(i + 2, 5) = Me.dsStudent.Rows(i)(4).ToString

Next

createExcel()

End If

End Sub

The implementation of the createExcel() method

Private Sub createExcel()

Dim oExcel As Object

Dim oBook As Object

Dim oSheet As Object

‘Start a new workbook in Excel.

oExcel = CreateObject("Excel.Application")

oBook = oExcel.Workbooks.Add

‘Add headers to the worksheet on row 1.

oSheet = oBook.Worksheets(1)

oSheet.Range("c1").Value = "EXPORTING DATASOURCE CONTENT INTO EXCEL"

‘Sending the data to Excel by specifying where to start from (cell) and the rows and ‘columns need.

oSheet.Range("B8").Resize(Me.dsStudent.Rows.Count + 2, 7).Value = Me.setudent

oExcel.visible = True

oSheet = Nothing

oBook = Nothing

oExcel.quit()

oExcel = Nothing

GC.Collect()

End Sub