Generating Crystal Report In Web API

In this article I am going to explain how to generate Crystal Report in a Web API call. We know that in Web API we don't have any views or designer to show the generated report. So here I will generate the report and send the report to the client. Here are the simple steps to do it.

The first step here is to create a new Web API project. After creating the project add a new controller as "DetailsController".

After adding this click on the Project, Add New Item, then ADO.NET Entity DataModel as in the following:
 
 
 
Now choose "Generate From database" and click Next
 
 
 
Provide all the Connection string details and test your connection. 
 
 
 
Choose Yes to include this connection string in Web.config. 
 
 
 
Choose the table on which you want to work. 
 
 
 
Click Finish and check your table. 
 
Now add the Crystal Reports as follows.

 
 
Choose the report as  Standard,

 

Click on OLEDB (ADO) and provide the credentials.
 
 
 
Select the table you want to show data in Crystal Report.

 
 
Click Next to display all fields of that table.
 
 

Now just select the field you want to display in the report. 
 
 

After that Click "Finish" to complete the process and it will show the following report structure. 
 
 
 
I have designed the report by providing Header and Footer image as shown below.
 
 

Now Create a model  as "Users" like this in Model Folder.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace CrystalReportIn_Webapi.Models  
  7. {  
  8.     public class Users  
  9.     {  
  10.         public string FirstName { getset; }  
  11.         public string LastName { getset; }  
  12.         public string Email { getset; }  
  13.   
  14.     }  
  15. }   
Now go to the "DetailsController", create a method and write the following code. 
  1. namespace CrystalReportIn_Webapi.Controllers  
  2. {  
  3.     [RoutePrefix("api/Details")]  
  4.     public class DetailsController : ApiController  
  5.     {  
  6.         CodeXEntities cX = new CodeXEntities();  
  7.   
  8.         [AllowAnonymous]  
  9.         [Route("Report/SendReport")]  
  10.         [HttpPost]  
  11.         public HttpResponseMessage ExportReport(Users user)  
  12.         {  
  13.             string EmailTosend = WebUtility.UrlDecode(user.Email);  
  14.             List<Users> model = new List<Users>();  
  15.             var data = cX.tbl_Registration;   
  16.             var rd = new ReportDocument();  
  17.               
  18.             foreach (var details in data)  
  19.             {  
  20.                 Users obj = new Users();  
  21.                 obj.Email = details.Email;  
  22.                 obj.FirstName = details.FirstName;  
  23.                 obj.LastName = details.LastName;  
  24.                 model.Add(obj);  
  25.   
  26.             }  
  27.   
  28.             rd.Load(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Reports"), "UserRegistration.rpt"));  
  29.             ConnectionInfo connectInfo = new ConnectionInfo()  
  30.             {  
  31.                 ServerName = "Debendra",  
  32.                 DatabaseName = "CodeX",  
  33.                 UserID = "sa",  
  34.                 Password = "123"  
  35.             };  
  36.             rd.SetDatabaseLogon("sa""123");  
  37.             foreach (Table tbl in rd.Database.Tables)  
  38.             {  
  39.                 tbl.LogOnInfo.ConnectionInfo = connectInfo;  
  40.                 tbl.ApplyLogOnInfo(tbl.LogOnInfo);  
  41.             }  
  42.             rd.SetDataSource(model);  
  43.             using (var stream = rd.ExportToStream(ExportFormatType.PortableDocFormat))  
  44.             {  
  45.                 SmtpClient smtp = new SmtpClient  
  46.                 {  
  47.                     Port = 587,  
  48.                     UseDefaultCredentials = true,  
  49.                     Host = "smtp.gmail.com",  
  50.                     EnableSsl = true  
  51.                 };  
  52.   
  53.                 smtp.UseDefaultCredentials = false;  
  54.                 smtp.Credentials = new NetworkCredential("de****@gmail.com""**********");  
  55.                 var message = new System.Net.Mail.MailMessage("[email protected]", EmailTosend, "User Registration Details""Hi Please check your Mail  and find the attachement.");  
  56.                 message.Attachments.Add(new Attachment(stream, "UsersRegistration.pdf"));  
  57.   
  58.                 smtp.Send(message);  
  59.             }  
  60.   
  61.             var Message = string.Format("Report Created and sended to your Mail.");  
  62.             HttpResponseMessage response1 = Request.CreateResponse(HttpStatusCode.OK, Message);  
  63.             return response1;  
  64.         }  
  65.   
  66.         
  67.     }  
  68. }  
Code Explanation
  • Here I have created a Web API method as "ExportReport". I am allowing all users so I used AllowAnonymous attribute here. As it is a post method I have given post.This method needs a parameter as email which will come from my Users Model. 

  • I am getting all my data from my database using the following Query where cX is my entity object.
    1. var data = cX.tbl_Registration;    
  • After that I create a report document object and bind its datasource as my model which is nothing but the data mapped with my users model.

  • Now generate a report and send it to user as per the email entered.

  • Sometime if it will ask for an invalid database so I have set the connection string detail here also.         
 After that save all the things and put down as "Advance REST client" for Chrome to test the API call as follows.
 


Click ADD TO CHROME. It will added to the chrome. Now double click to open it.

 
 
 
After opening it provide the API url with the parameter as follows.
 
 
 
After that click on save. It will show you the response as 200 that means your request is successfuly executed. Now go to the mail and check for the email.

 
 
Now click the mail to open it.

 
 
Click Open to open the pdf report as follows.
 
 
 
So in this way we can generate Crystal Report using Web API call. Hope you understood the whole process. 
 
Read more articles on Crystal Reports:


Similar Articles