How to show only a single record with crystal report in ASP.NET MVC


I want to show only a single record from an employeeList by ID with crystal report in asp.net MVC. But I don't know how to do it. I can show a whole list. But I can't do it for a single ID. Please help.


Asked by:- SalmanZahir
1
: 5814 At:- 10/19/2017 5:11:01 AM
ASP.NET MVC Crystal Report







2 Answers
profileImage Answered by:- pika

I was able to generate .pdf file of one value with ID using CrystalReport, let me show you my code, database and other details.

Note: before you proceed for answer, I believe you have VS2017 and Crystal report in Visual Studio, if you have latest VS 2017, you can download from here

In this project I am using Entity framework and MVC, so here is my code for it :

 BlogDbEntities blogDbEntities = new BlogDbEntities();
        public ActionResult Index()
        {
            List<Blog> OneBlog = new List<Blog>();

           //Taking only One row using ID
            OneBlog = blogDbEntities.Blogs.Where(a => a.BlogId == 1).ToList();

           

            ReportDocument rd = new ReportDocument();
            //location of your crystal report in this it is /ProjectFolder/Model/CrystalReport1.rpt
            rd.Load(Path.Combine(Server.MapPath("~/Models"), "CrystalReport1.rpt"));

            rd.SetDataSource(OneBlog);

            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();


            Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);
            return File(stream, "application/pdf", "BlogList.pdf");
            
        }

Here is my database table image:

BlogDB-database-crsytal-reports-in-MVC

and here is the Output

BlogList.png

If you are new to Crstal report you can generate it using Following steps :

  1. Create a new Folder in your project and name it as "Reports"
  2. Now "Add" a crystal report

    adding-crystal-report-in-MVC
  3. Now Select "As a blank report", Click "OK"
  4. Right Click "Database Fields", Select "Database Expert"
  5. Now select "Create New Connection"

    Report-in-MVC.png
  6. Click "Next"
  7. Select your server, Enter user ID & password, select database, Click "Next", Click "Finish"
  8. Now expand your db table, select table  for which you want to show data

    Select-database-crystal-report-MVC
  9. Now Expand "Database Fields", Expand "Table(Blog here)", Drag and drop your column, which you want to render on crystal report

    Crystal-report-Drag-drop-column-MVC
  10. That's it, you are done.

Note: Your table for which you want to show doesn't have any Nullable column, otherwise while generating reports you may get error "Dataset does not support system.nullable"

Please upvote or mark it as answer, if it works,  or comment your queries thanks

2
At:- 10/19/2017 10:58:11 AM


profileImage Answered by:- SalmanZahir

SampleIts almost the answer, but what I wanted is, suppose I have created an actionlink on the index page "Print", here when I will be clicking it, it will print the details of that particular entry. How can I do that? I have Attached an image. I want to print the amount_received_slip for that particular entry

0
At:- 10/24/2017 10:09:08 AM Updated at:- 10/24/2017 10:11:12 AM
ok then above Step by Step procedure would work as when you click on "Print", you can call the method of Crystal Report, fetch the single detail, as shown above, it will send you the pdf file with amount_received_slip on it only, try it, it will work for sure, you just need to do some changes according to your table 0
By : pika - at :- 10/25/2017 7:19:06 AM
remember you can also Show pdf file generated in new_tab, instead of downloading it 0
By : pika - at :- 10/25/2017 7:19:58 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use