creating crystal reports using c (1)

Upload: olivier-garcia

Post on 06-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Creating Crystal Reports Using C (1)

    1/6

    1

    Creating Crystal Reports using C# with Datasets

    Introduction

    This article is about creating Crystal Reports using C#.NET. About two weeks back,

    one of my friends asked me this kind of example, also he searched through the Internet

    and he told that he couldnt find a sample. When I was doing this kind of developmentsIm also looked for samples, but I couldnt find one. Thats why I did this article toCodeProject without creating reports separately using Crystal Reports other than using

    .NET IDE.

    Create a Dataset

    First you should create a Dataset to get the data from the DB. By clicking the Add

    New Item in the Project menu you can add a Dataset. A picture of an added Datasetis looks like the following window and can add elements to the Dataset by dragging and

    dropping elements from the toolbox. This Dataset is used to fill the data, which isgetting from the DB by using a query. Because of that the names of the elements in this

    Dataset should be equal to the names of the elements in the DB. For an example thisDataset can fill using the following query.

    String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data

    Source=..\\..\\myDB.mdb;User ID=Admin;Password=";

    OleDbConnection conn = new OleDbConnection(connString);

    conn.Open();

  • 8/3/2019 Creating Crystal Reports Using C (1)

    2/6

    2

    string query = "SELECT studentID, firstName, lastName, birthDate,

    address, contactNo FROM studentInfo";OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);

    Create a Report using Crystal Reports

    Now you have a created Datset, then we can use it to fill the report with data, which will

    be getting from the DB. As I said before you can add a Crystal Report to the project by

    clicking Add New Item in the Project menu. Then the following window will

    appear, and you can select your choices and click OK.

  • 8/3/2019 Creating Crystal Reports Using C (1)

    3/6

    3

    Next window is like the following and you have to select your created Dataset underProject Data, and click Insert Table, then click next.

  • 8/3/2019 Creating Crystal Reports Using C (1)

    4/6

    4

    Then you have to add the field, which you want to display in the report through the

    following window and click next.

    Also you can go to other tabs of this window and select/ deselect your choices. Use thelast tab Style to select the format of the report. Also you can type a Report Title here

    and click finish.

  • 8/3/2019 Creating Crystal Reports Using C (1)

    5/6

    5

    Then your report creation is done by the .NET IDE. If you want to do any changes to

    the report you can do it by using the .NET IDE.

    Set the created Report to display in the Form

    Then you have to set a crystalReportViewer in your form to load the report that you

    created earlier. And also you need to set the report source of this crystalReportViewercomponent, which is falling in the properties panel or you can set the report source by

    using the code like the following.

    // code to get data from the DBDBConnection DBConn = new DBConnection();OleDbDataAdapter myDataAdapter = DBConn.getDataFromDB();

    // use the created Dataset to and fill it with data getting

    // from the DB by using the DataAdapterDataSet dataReport = new DataSet();myDataAdapter.Fill(dataReport,"myPersonalInfoTable");

    // create a new report from the created CrystalReportmyDataReport myDataReport = new myDataReport();// set the data source of the reportmyDataReport.SetDataSource(dataReport);

  • 8/3/2019 Creating Crystal Reports Using C (1)

    6/6

    6

    // set the report source of the created crystalReportViewer

    // component to the created reportcrystalReportViewer1.ReportSource = myDataReport;

    Additional InformationAlso you can create reports using Crystal Reports separately without using the .NET

    IDE. For that you have to install Crystal Reports as well, and you should save those

    reports in a directory. Then you have to set the report source of the

    crystalReportViewer component to the particular report under your report Directory.

    Ex:-

    crystalReportViewer1.ReportSource = @..\Reports\salesReport.rpt;

    But the better way is the previous one, because we can get the data from the DBaccording to the inputs, which are doing by the user of the Applications.