Skip to main content

Passing SQL to Crystal Reports 10 using C# express and SQL Server 2005

Tools used in this example:
1. C# Express 2008
2. MS SQL Server Express 2005
3. Crystal Reports 10

I. Create the Report File
1. Click on File->New and select "As blank report" (The Database Expert wizard shows)
2. Expand the "Create New Connection" folder, and expand OLE DB (ADO). A window will appear.

3. On the Provider section, look for SQL Native Client. Select it and click Next.
4. On the Data Source combo box, type in the name of your server. (Mine is RIXX\SQLExpress)
5. Select the Integrated Security checkbox so that it uses your windows account for authentication.
6. Choose your database from the Database combo box and click finish. I have a database with the following attributes:
Name: TestDB1
Tables:
TestTable1
Columns: Id, Description
Data:
Id Description
1 aaa
2 bbb
3 ccc
4 ddd
5 eee

7. Add your tables to the Selected Tables section and click Ok.
8. Drag the fields to the Details section of the report. Save your report. I used Report1.rpt.


II. Making the C# Project:
1. Create a new Windows application project.
2. Right click on the Toolbox and choose "Choose Items..."
3. Select the COM components tab.
4. Select the "Crystal ActiveX Report Viewer Control 10.0"
5. Click Ok. (The Crystal Report Viewer Control icon should now be visible on the toolbox.



6. Add a Crystal Report Viewer to the form.
7. Right Click on the Solutions Explorer and click Add Reference.
8. Click the Browse tab.
9. select the following location (or depending on where you installed your crystal report): "c:\Program Files\Common Files\Crystal Decisions\2.5\bin\" and select "CRAXDRT.dll"
10. Click Ok
11. Use the following lines of code when opening a report:

CRAXDRT.Report rpt = new CRAXDRT.Report();
CRAXDRT.Application app = new CRAXDRT.Application();
rpt = app.OpenReport("", null);
axCrystalActiveXReportViewer1.ReportSource = rpt;
axCrystalActiveXReportViewer1.ViewReport();

12. the report viewer should then show the report you have created.

III. Passing SQL statements to the report.
1. Modify the code above into:
CRAXDRT.Report rpt = new CRAXDRT.Report();
CRAXDRT.Application app = new CRAXDRT.Application();
app.LogOnServerEx("crdb_ado.dll",
@"RIXX\SQLExpress", "TestDB1", "", "", null, @"Provider=SQLNCLI;Server=localhost;Database=TestDB1;Trusted_Connection=yes;"); //This is used for logging in to your SQL Server
rpt = app.OpenReport("d:\\report1.rpt", null);
rpt.SQLQueryString = "select * from TestTable1 where Id=4"; //instead of displaying all 5 records, I chose to display only the one with Id # 4.
axCrystalActiveXReportViewer1.ReportSource = rpt;
axCrystalActiveXReportViewer1.ViewReport();

Comments

Anonymous said…
Hi Sir,

I just would like to ask if you are interested to have own domain/web hosting.. you can check my website for details. I have already many clients and one of them is http://exanderbarrios.info/.

Thanks
I am stryggling from One week to make DataType Independent in Crystal report. This Post is helpfull to me. Thanks for nice post.
I am Eric.E. said…
I'm glad I can help. You are welcome. :D

Popular posts from this blog

How to register a business name

Attending business summits and conferences is a big help to those who belong to the quite "techy" (technological or technical) industry. Being a graduate of one, I had less knowledge in the field of entrepreneurship. Enrolling myself in business administration gave me quite the knowledge to be a part of the business world and thus improve my entrepreneurial skills. I now would like to share this information that I got familiar with (and I managed to get a copy of the entire process from the 6th Mindanao ICT Congress): How to Register a Business Name (in the Philippines) ----------------------------------------------------------------------------------------------- SINGLE PROPRIETORSHIP Applicant must secure 2 copies of registration form and pay Php 300.00 (rate may change) for single proprietorship registration processing fee. The registration shall be valid for five (5) years. A surcharge of Php 100.00 is imposed if renewal is filed beyond the three (3) month grace period, c

Adding a Footer to the DataGridView component

I have been searching for sites and forums that would give me a any hint on having a footer on the .net DataGridView control. It was frustrating. I found some, but not what I was looking for. I use windows forms. It would have been easier if I was into web. I decided to create one for myself. It's not complete, but it works with me. It needs improvement and I hope that some programmers who might pass through this blog will help me with it :D. Limitations: Cannot set Footer values during design time. Can sometimes hide a row when scrolled to the last item in the grid. What I did was just create a user control that inherits the DataGridView control and add a StatusStrip to act as the footer. public partial class MyDataGridView : DataGridView { public StatusStrip Footer { get { return (StatusStrip)this.Controls["Footer"]; } } private bool _footerVisible; [Browsable(false)] /// /// Sets or Gets the va

Using Firebird with ASP.net

In my previous post , I was able to connect a firebird 2.0 database to asp.net using controls. The next goal was to connect to a firebird database using the firebird.net engine and using the repeater control. I however, was able to use the firebird.net engine to connect to the firebird database but not to the repeater control. I used the html table tag. I've created a simple asp.net demo displaying the contents of a firebird database on page load. Here are the steps: 1. Things Needed: Firebird 2.0 Server Firebird 2.0 Client Visual Web Developer 2. In Visual Web Developer, create a new ASP.net Website (using C# as the programming language). The project will have a default page named Default.aspx. Make sure that the code is separate from the page (ex. Default.aspx.cs is separate from Default.aspx) 3. On the Website menu, click on Add Reference. 4. Select the Firebird - ADO.net 2.0 Data Provider and click Ok. 5. Add using FirebirdSql.Data.FirebirdClient; to the "using" sec