This post has reference to part one of this Article.
Create a new Class with the name "MyLinq". We are going to use the DataContextFactory Class which we created in Part One. Also assume in our database we have Table with name "User" and with the following Definition.
First of all, in your Project or Website, add a new item from the "LINQ to SQL" template. This will create a file with extention ".dbml" and code behind files. Now from Server Explorer, drag and drop the required Tables from the DB. At this point, we will be able to save the Server Connection String to connect to the Database automatically. If its a Web application, it will be saved in web.config and for Windows Project, it will be saved in Settings.settings file.
First of all you need to create a New Data Set object from the Add new Item. Inside the Data set you need to create a new Table Adapter which will Fill data to a Data Table.
Then you need to create a new Crystal Report File from Add new Item. In the Crystal report, from the Field Explorer -> Database Fields, now you will able to see the created Data Table with all the Column Elements selected.
Select the Column Elements needed, and add them to your Crystal Report and arrange them to your specific need.Using Crystal Reports->Design->Printer Setup, you can adjust the size of the Crystal Report according to your need. Also to make it a template for Excel, you need to Suppress the Page Footer from the Section Expert.
Then the below given Block of code is used for Excel Reporting.
try {
TempDataSetTableAdapters.TempTableAdapter temp = new MyProject.TempDataSetTableAdapters.TempTableAdapter();
TempDataSet.TempDataTable tempTable = new TempDataSet.TempDataTable();
DataTable DT = tempTable;
temp.Fill(tempTable);
ReportDocument rpt = new ReportDocument();
rpt.Load(Server.MapPath("~") + "\\TempReport.rpt");
rpt.SetDataSource(DT);