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

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:


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();

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?

Google Chrome, A Web-Browser from Google

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:


There is a lot of features included in this web-browser that is available in Google Chrome official website: 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.

Want Windows Online? Try Live Mesh

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:

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.

I love this cookie


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.

Management is the most important section in a company


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.

How to create multi-language websites using ASP.NET

Using Resources can help you create flexible multi-language websites in ASP.NET. In this post I want to show you how you can do it.

As you know, ASP.NET has some specific folders like: App_Data, App_Code, … . One of these folders is App_GlobalResources that holds resources. you can add this folder by right click on your website in Solution Explorer and from Add New ASP.NET Folder, select App_GlobalResources as following:

Add App_GlobalResources Folder

When you created this folder, then right click on App_GlobalResources folder and then select Add New item:

Add A Resource File to Global Resource Folder

As you can see, you can add a limited types of items to this folder. From the list, select Resource File and then select a name for your new file as always. I selected WebRes.resx as my file name. Resource files are XML based files that hold data in your application and will compile with your application. In order to create multi-language websites, you need to create a file for each language you want to implement in your website. So because I want to have Persian language too, I should create another resource file. The important section of creating another resource file is in it’s name. You must follow some rules in your naming in order to let ASP.NET know which languages you have in your application. The rules are listed below:

  • You must start your resource file name with the name you specified to your first resource (In this post WebRes).
  • The only difference between your first file name and new language file name is in the last section of your file name. For example if you have a first resource file named WebRes.resx, and you want to add Persian language to your application, you must name your Persian language resource file WebRes.fa.resx (fa is short for Farsi and it’s global); After that .NET Framework automatically understands that you have a Persian language resource file.

Now how to use resources in my application?

In order to use resources, you must make a use of embedded tags (<% %>) in your application. For example imagine that I have a label in my application and I want to use one of my resource keys in as the text property of my label. In order to do this, you must do something like this:

<asp:Label runat="server" ID="Label1" Text='<%$ Resources: WebRes, ResLabel %>'></asp:Label>

How it works?

The first part is Resources that tells ASP.NET that it’s going to be a Global Resource.

WebRes is the name of our Resource file (Without any suffix). Note that you don’t need to write any suffix in this section.

ResLabel is the name of our key we want to use in. I just added a key named ResLabel as shown below:

Added Resources

After you specified where want to show your resource , now you must let your application know which of those languages (In this sample, English (Unites States) and Persian) you want to use.

Your have two general options:

  1. Let your Web Browser decide.
  2. You enforce your application to use your specific language.

Let your Web Browser decide:

In order to set your application to get default language from your web browser, all you need is to set Culture and UICulture attributes to auto as shown below:

<%@ Page 
Inherits="_Default" %>

Your Web Browser automatically fill your culture information from user’s Operating System.

Enforce your application to use your specific language:

Now it’s time to write some code. in order to enforce your application to use a specific language, you need to dynamically change the default Culture and UICulture of your application. ASP.NET holding culture information in the application main thread. So you have to change that as following:

    protected void ChangeLanguage()
        string langName;
        //Language short name for English (United States).
        langName = "en-US";
        System.Threading.Thread.CurrentThread.CurrentCulture =
        System.Threading.Thread.CurrentThread.CurrentUICulture = 
        //Now the language is English (United States).
        //Language short name for Persian.
        langName = "fa-IR";
        System.Threading.Thread.CurrentThread.CurrentCulture =
        System.Threading.Thread.CurrentThread.CurrentUICulture =
        //Now the language is Persian.

As you saw, .NET Framework will automatically understands which culture you want to use and then tells your application which resource file it must use.