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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace CrystalReportIn_Webapi.Models
- {
- public class Users
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
-
- }
- }
Now go to the "DetailsController", create a method and write the following code.
- namespace CrystalReportIn_Webapi.Controllers
- {
- [RoutePrefix("api/Details")]
- public class DetailsController : ApiController
- {
- CodeXEntities cX = new CodeXEntities();
-
- [AllowAnonymous]
- [Route("Report/SendReport")]
- [HttpPost]
- public HttpResponseMessage ExportReport(Users user)
- {
- string EmailTosend = WebUtility.UrlDecode(user.Email);
- List<Users> model = new List<Users>();
- var data = cX.tbl_Registration;
- var rd = new ReportDocument();
-
- foreach (var details in data)
- {
- Users obj = new Users();
- obj.Email = details.Email;
- obj.FirstName = details.FirstName;
- obj.LastName = details.LastName;
- model.Add(obj);
-
- }
-
- rd.Load(Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/Reports"), "UserRegistration.rpt"));
- ConnectionInfo connectInfo = new ConnectionInfo()
- {
- ServerName = "Debendra",
- DatabaseName = "CodeX",
- UserID = "sa",
- Password = "123"
- };
- rd.SetDatabaseLogon("sa", "123");
- foreach (Table tbl in rd.Database.Tables)
- {
- tbl.LogOnInfo.ConnectionInfo = connectInfo;
- tbl.ApplyLogOnInfo(tbl.LogOnInfo);
- }
- rd.SetDataSource(model);
- using (var stream = rd.ExportToStream(ExportFormatType.PortableDocFormat))
- {
- SmtpClient smtp = new SmtpClient
- {
- Port = 587,
- UseDefaultCredentials = true,
- Host = "smtp.gmail.com",
- EnableSsl = true
- };
-
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = new NetworkCredential("de****@gmail.com", "**********");
- var message = new System.Net.Mail.MailMessage("[email protected]", EmailTosend, "User Registration Details", "Hi Please check your Mail and find the attachement.");
- message.Attachments.Add(new Attachment(stream, "UsersRegistration.pdf"));
-
- smtp.Send(message);
- }
-
- var Message = string.Format("Report Created and sended to your Mail.");
- HttpResponseMessage response1 = Request.CreateResponse(HttpStatusCode.OK, Message);
- return response1;
- }
-
-
- }
- }
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.
- 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: