How to use LINQ to SQL for binding data in CrystalReports 2008

clock September 10, 2008 01:54 by author Mohammad Mahdi Ramezanpour

Today I just decided to create some reports for one of our customers in Visual Studio 2008 and .NET Framework 3.5. Because I'm developing this application using LINQ, I wanted to use LINQ to SQL in CrystalReports. In this post I want to show you how you can use LINQ in order to bind data in CrystalReports.

Add CrystalReportViewer

At first, all sections are regular. You need to create a new application (I used Windows Application) and then add a Crystal Report Viewer to you specified form. When you add this control, you will see that Visual Studio added five new references to your application. These references are:

  • CrystalDecisions.CrystalReports.Engine
  • CrystalDecisions.Enterprise.Framework
  • CrystalDecisions.Enterprise.InfoStore
  • CrystalDecisions.ReportSource

You will need these libraries in order to let your application work with CrystalReports.

You made a place to show your reports in. Now you have to get started and create some reports. I want to add a new report by right clicking on my project, point to Add and then New Item. In the list of new items you need to point to Reporting section on the left side of "Add New Item" form and select Reporting. You items on templates side will be filtering to just 3 items:

  • Crystal Report
  • Report
  • Report Wizard

We want to work with CrystalReport here but for now I want to tell you other 2 items will work with Microsoft SQL Server Reporting Service that enables you to create flexible reports like CrystalReports but because CrystalReports is more common, most of developers prefer to use CrystalReports.

We just select Crystal Report and then select Add.

Because we want to work with LINQ classes, I need to create a LINQ to SQL class in order to add my tables to it and then use it in my CrystalReport file (For information about how you can create a LINQ to SQL class, Just check out my previous post: How to use SQL "IN" keyword in LINQ). Here is my tables structure:

LINQtoSQLClass

All I want is to show records from my Articles table. So I should implement this in my report. As you know when you're working with LINQ to SQL, tables become classes and fields become properties of those classes. When you want to add a new data source to your CrystalReport file (Data Fields section), there are some possibilities. You can use ADO.NET Datasets that enables you to use datasets you implemented in your application; and also you can use .NET Objects. It means that you can use classes in your application to be a data source for your report file. Because LINQ is based on classes, You must use .NET Objects in order to use LINQ to SQL as your Report's data source.

When you expand .NET Objects, you can see all classes in your application and also you can see the classes that made by LINQ class designer. Because I want to use my articles table I have to select Article class in the list as shown below:

Selecting Article Class

If you select this class as your data source you can see all fields available in the Articles table in database field section of our crystal report file. It means you can add each field you want to your report. So I just want to add Title field to my report (Details section):

Add Title To My Report

OK. That's all we need in our report file. Just one section left and that's let your report file know what data to show. You need to assign a database query to your file.

Let's go back to our report viewer file and go to code-behind section. In order to set your report file's properties, you have to create a new instance of your report file as following:

CrystalReport1 rpt = new CrystalReport1();

Now it's time to specify a query to your report file. A CrystalReport report file has a method named SetDataSource() that takes a datasource as the following types:

  • DataSet
  • DataTable
  • IDataReader
  • IEnumerable

LINQ to SQL, enables you to convert your data to a List<entityType>, Array and more. As you know List inherits IEnumerable, so All you need is to create a LINQ object and add a ToList() method to it and finally set the "SetDataSource()" method to our LINQ object as you can see in the code below:

ReportSampleDBDataContext db = 
    new ReportSampleDBDataContext();
var data = (from records in db.Articles 
            select records).ToList();
rpt.SetDataSource(data);


There is a property in our CrystalReportViewer control name ReportSource that enable you to specify which report file you want show in your CrystalReportViewer control. So you must set it up:

crystalReportViewer1.ReportSource = rpt;


It's working fine, isn't?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Google Chrome, A Web-Browser from Google

clock September 8, 2008 01:08 by author Mohammad Mahdi Ramezanpour

Google Chrome

Google Chrome is a new web-browser released by Google that I think it's one of my best web-browsers I've ever seen. It's very similar to Safari and it have some other features based on Google Search and more. The best part is that it have a really good interface specially on Windows Vista because I just like this User Interface:

Chrome

There is a lot of features included in this web-browser that is available in Google Chrome official website: http://www.google.com/chrome. You can also download it from from that page.

 

By the way for those who are living in Iran or other restricted countries, you can download it from my friend, Omid Mafakher's website.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Want Windows Online? Try Live Mesh

clock September 5, 2008 16:21 by author Mohammad Mahdi Ramezanpour

Live Mesh

it was about two months ago and I wanted to store my files on the web. I knew Windows Live SkyDrive, but I wanted to store my music files in a place that I can play it online without downloading it. Finally, I found out that Microsoft introduced a new service name Live Mesh that enable users to have several Windows features online. You can check it out by visiting it's site: http://www.mesh.com

But before get started let's see what can Live Mesh do for you:

All your devices working together

No more e-mailing attachments to yourself. Instead, synchronize the information you need across all your devices. The most up-to-date versions will be at hand when you need them—at home, at the office, and on the go.
Just install the Live Mesh software on each device. Then add folders to your mesh. Folders are automatically synchronized, always available.

Access from anywhere

Anything you add to Live Mesh is available from anywhere, including the web from your Live Desktop. Your Live Desktop comes with 5 GB of free storage, and can be used from most web browsers.
Need a program that's only on your home PC? With Live Mesh, access to all your devices—and any programs on those devices—is at your fingertips, no matter where you are.

Simple to share

Easily share files and photos with friends, family, and colleagues—invite them to a folder. Everyone is kept up to date because files can be synchronized automatically with all your devices and all their devices.
Update documents, post comments, or send instant messages, all right from the folder. The Live Mesh bar helps you connect instantly with other folder members.

Stay informed

Keep track of all the activities in your mesh. See the online status of friends and colleagues, find out who has updated which files or folders, post and read comments, and check the status of your devices.
News about your mesh is easy to access. You can view news items in the notifier, from the mesh bar, and on the Live Mesh website—available whenever, wherever you are.

Protection you know

Your mesh is password-protected with your Windows Live ID, so only you have access to it. When you share a folder with family and friends, they sign in with their own Windows Live ID to access it.
All file transfers are protected using Secure Socket Layers (SSL), the same technology your online bank uses.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


I love this cookie

clock September 4, 2008 21:01 by author Mohammad Mahdi Ramezanpour

Zoolbia

I love this cookie a lot and I don't why it's not available always. In Iran, there is a special month, named Ramadan that people are trying not to eat a lot and most of cookie makers, cooking this kind of cookies. But unfortunately it's not available always and I don't know how come?

Anyway, I love this and I want to suggest you to try it one time and you'll be one of it's proponent if you haven't.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Management is the most important section in a company

clock September 3, 2008 01:19 by author Mohammad Mahdi Ramezanpour

BossEnforcing

These days I'm not good at work. My boss doesn't behave good and he is always sad. He is trying to enforce us to develop as much as we can.

I asked him about it and he told me: Everything is going to be OK. "I'm normal and there is nothing to worry about", he said.

I think, he doesn't know how a manager can effect on his/her employees with these kinds of behaviors. When a manager is sad, all employees think that there is something wrong with the company for example, one of the company's projects has been failed.

In my opinion, a good manager is a person who always behave well at work. because he/she is a person that can make the company happy. When a manager is good, so the company is good too.

Because of these changes in our company, I decided to change it. So a manager may lost some of his/her employees.

I got an advise for you:

If you want to start working in a company, try to know the manger(s) as well as you can.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Welcome


Mohammad Mahdi Ramezanpour
Welcome to my website. Hope you can get something useful here. For more information about me and ways you can contact, please use site menu.

Main Menu

Sign in