Crystal Reports In ASP.NET

This example shows how to Create Crystal Reports In ASP.NET 2.0,3.5,4.0 Using C# And VB.NET. I am generating Crystal report by fetching data from two tables and grouping them based on Project Name. Database tables are just for demo purpose you can create your own tables with whatever schema you want

Two tables are as shown below.

Crystal reports in ASP.NET

Create a new website and right click on solution explorer > add new Item > Select Crystal Report
In the dialog box choose blank report.

 
Now click on CrystalReports Menu in VS and select DataBase Expert 
  
In database expert dialog box expend create new connection > OLEDB(ADO) section
 
Now select SQL Native client and enter you SQL server address , username , password and pick database name from the dropdown. 
 
  
In next screen Expend your database objects in left pane and add the tables you want to use in right pane 
 
Link your tables based on Primary keys (If any)

Click ok to finish the wizard.
Right click on Field Explorer and select Group Name Fields  > Insert Group

In next box select the field you to report to be grouped (in my case it's ProjectsName)

Click on OK to finish
Now design the report , drag and fields from Database fields in field explorer and which you want to show in report and drop them in Section3(Details), and preview the report, it should look like show below.


Go to default.aspx page and drag and drop CrystalReportViewer from the toolbox, click on smart tag and choose new report source.
Choose you report from the dropdown menu and click ok to finish.
Now when you build and run the sample , it asks for the database password everytime.

 
To fix this we need to load the report programmatically and provide username and password from code behind .
Now run the report , it should look like this 


Html markup of default.aspx look like
<form id="form1" runat="server">
<div>
  <CR:CrystalReportViewer ID="CrystalReportViewer1" 
                          runat="server" AutoDataBind="True"
                          Height="1039px" 
                          ReportSourceID="CrystalReportSource1" 
                          Width="901px" />
  <CR:CrystalReportSource ID="CrystalReportSource1" 
                          runat="server">
            <Report FileName="CrystalReport.rpt">
            </Report>
   </CR:CrystalReportSource>
    
    </div>
    </form>

C# code behind

Write this code in the event you find appropriate , i m writing it in Page_Load , you can write this code in click event of button or in pagePreRender event
The code to provide password programmatically.
protected void Page_Load(object sender, EventArgs e)
    {
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
        crystalReport.SetDatabaseLogon
            ("amit", "password", @"AMIT\SQLEXPRESS", "TestDB");
        CrystalReportViewer1.ReportSource = crystalReport;
    }

VB.NET code behind
Protected Sub Page_Load
(ByVal sender As Object, ByVal e As EventArgs)

Dim crystalReport As New ReportDocument()

crystalReport.Load(Server.MapPath("CrystalReport.rpt"))

crystalReport.SetDatabaseLogon
("amit", "password", "AMIT\SQLEXPRESS", "TestDB")

CrystalReportViewer1.ReportSource = crystalReport

End Sub

Hope this helps


related posts :
Crystal Reports in Winforms Windows Forms with Parameters C#.NET VB.NET

SubReports in Crystal Reports in ASP.NET

78 comments:

  1. WoW! U did the superb Job.

    Please provide your post full feed.

    I need your help..can u help me please

    1. how can I add more links above the comment box?

    2. how can I add these links & images below my post like you added in your post "digg it, tweet it, mixx it, Email subscription, etc etc

    please mail me your answer

    ReplyDelete
  2. i got the problem in code behind in this line

    ReportDocument crystalReport = new ReportDocument();

    error is : Error 1 The type or namespace name 'ReportDocument' could not be found (are you missing a using directive or an assembly reference?). is there any namespace i

    hv to add in code behind

    my report is not well design all fields are coming 2 times in report.when i draged the fields in section 3. they automatically coming in section 2.

    ReplyDelete
  3. @Himanshu:

    You need to add namespace in code behind


    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;


    When you drag field from database fields in section3 than same fields name comes in section 2 for heading , you can change fields name in section 2 to whatever you want to display

    ReplyDelete
  4. @काशिफ़ आरिफ़/Kashif Arif:

    It requires editing html code of template, if you can do that ,let me know.
    btw do u belongs to Agra ?

    ReplyDelete
  5. hey well i think this is a slower way
    i think there is another way for making crystal report using data set but i don't know how
    can any one help me ???

    ReplyDelete
  6. Dude thank u.... anyways for beginners just to create a basic report this is good...

    ReplyDelete
  7. i got an error when i try to load the report. It says failed to load repor...

    ReplyDelete
  8. The reportdocument load method is not getting the path of your .rpt file .Please provide complete path of your .rpt file.
    I am sure it will work.

    ReplyDelete
  9. Good Way of representation ,A beginner can generate report by reading this article
    but focus on properties of report
    thank you

    ReplyDelete
  10. if u need more exaples

    VB.NET

    http://vb.net-informations.com/crystal-report/vb.net_crystal_reports_tutorials.htm

    C#

    http://csharp.net-informations.com/crystal-reports/csharp-crystal-reports-tutorial.htm

    tks.

    ReplyDelete
  11. Thanks Dear for your Ernest effort to make it understandable.I am a beginner and it help me a lot.

    Thanks again.

    Pankaj Gupta

    ReplyDelete
  12. Logon failed. Details: crdb_adoplus : Object reference not set to an instance of an object. Error in File C:\DOCUME~1\SCHOWD~1\LOCALS~1\Temp\CrystalReport1

    Hi I am getting this.

    ReplyDelete
  13. thanks for crystal report procedure

    ReplyDelete
  14. hi i am asp.net beginner i need to generate report when the user click the submit button
    thanks in advance

    ReplyDelete
  15. making blank crystal report from dataset using asp.net with c# .already i worked but i get the error like Report has no tables
    string str = "select * from tempa";
    SqlConnection cn = new SqlConnection("server= PENNANTSRV01;database=Practice;user id=sa;password=zxcvbn1@");
    SqlDataAdapter da = new SqlDataAdapter(str, cn);
    cn.Open();
    DataSet dt = new DataSet();
    da.Fill(dt,"dfg");
    if (dt.Tables[0].Rows.Count > 0)
    {
    CrystalDecisions.CrystalReports.Engine.ReportDocument crystalReport2;
    crystalReport2 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    crystalReport2.Load(Server.MapPath("CrystalReport.rpt"));

    //crystalReport2.Database.Tables[0].SetDataSource(dt.Tables[0]);

    crystalReport2.SetDataSource(dt.Tables["dfg"]);// Error occured Here

    CrystalReportViewer1.ReportSource = crystalReport2;
    CrystalReportViewer1.DataBind();
    }
    cn.Close();


    but i don't know how
    can any one help me ???

    If anyone have solution please mail(skpraju.bh@gmail.com) to me.

    ReplyDelete
  16. thanks for posting

    ReplyDelete
  17. its a nice article ..thanku

    ReplyDelete
  18. Hi i m umesh chandurkar and the .net beginer So thanks a lot to share with me

    ReplyDelete
  19. hey after writing the code in the pageload method there still it asking for username and password...

    ReplyDelete
  20. DataSet dt = new DataSet();
    da.Fill(dt,"dfg");
    if (dt.Tables[0].Rows.Count > 0)
    {
    CrystalDecisions.CrystalReports.Engine.ReportDocument crystalReport2;
    crystalReport2 = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    crystalReport2.Load(Server.MapPath("CrystalReport.rpt"));

    //crystalReport2.Database.Tables[0].SetDataSource(dt.Tables[0]);

    crystalReport2.SetDataSource(dt.Tables["dfg"]);// Error occured Here

    CrystalReportViewer1.ReportSource = crystalReport2;
    CrystalReportViewer1.DataBind();
    }
    cn.Close();

    In that change
    da.Fill(dt,"dfg"); to da.fill(dt);
    crystalReport2.SetDataSource(dt.Tables["dfg"]);
    to
    crystalReport2.SetDataSource(dt);

    It should work

    ReplyDelete
  21. Hi..nice article thanks for sharing...

    ReplyDelete
  22. Thank you very much!!!!!!!!!!
    It helped me a lot.Appreciate your way of presentation.helps beginers also...

    ReplyDelete
  23. Am getting crystal report Version error in web.config

    ReplyDelete
  24. Good enough for beginners...thanx... :)

    ReplyDelete
  25. hi skpraju,
    do u got solution for ur problem ?
    if yes please post answer or mail me the code to movnabbs@gmail.com

    thank you

    ReplyDelete
  26. Dear All
    i, need procedure for calling crystal reports 2008 in asp.net3.5
    i, am using crystal reports 2008, which is seperately installed, i, have query for that report, but, i, don't know, how to call the CR2008 in asp.net , which contains input fields has to display in report.
    example: 2 dates, like fromdate and todate,
    one dropdown list box, based on these selection, i, had a query also,, pls send me code for code behind for vb.net , how to call cr2008 and how to pass the input parameters to be display in the report.
    if anyone knows kind mail to nalla_gangadhar@yahoo.co.in or nalla.gangadhar@gmail.com as soon as possible.

    Thanks in advance

    Warm Regards..

    ReplyDelete
  27. Sir,
    I want to use crystal report in asp.net for bill generation. So I want to arrange fields vertically. How can I do this? Help me?
    Thanks
    Gopal Sharma,Jaipur

    ReplyDelete
  28. hello amit..
    i want ask u that can it is possible to find database link from Crystal report even u r not admin..if yes then how..

    i m explaining again my problem:-
    "That is any way to extract data from crystal report.i have different-2 link of reports i want to extract data from it but i m not a admin so What i am doing that going to all link and copy data.
    But it is very difficult task so please suggest me any easy solution/way...."

    thank you very much

    ReplyDelete
  29. This is really great info abt crystal reports, it helps me lot to create crystal reports

    ReplyDelete
  30. Nice article.Thanks for sharing

    ReplyDelete
  31. Thanks for sharing....
    If you are looking for better reporting software, please take a look at Windward Reports. With Windward you design reports in Microsoft Word, Excel, or PowerPoint so report design is a lot faster and easier - and non-programmers can design reports.

    ReplyDelete
  32. hi,
    how can i set report data source programttically in your example...

    ReplyDelete
  33. thanks friend for your blog. can u let how to pass multiple parameter.
    and with where condition....

    ReplyDelete
  34. first we will go for new website and we open a new website.....then

    through the solution explorer we do through the ADD NEW ITEM and we seclect SQL SERVER DATABASE and we add that....whose name is like "INVETORY.mdf"...then

    we open a server explorer and we add the table in INVETORY.mdf database.....here table name is "LOGIN"....who has a two field......ID and PASSWORD.....then

    now we open table LOGIN and SHOW TABLE DATA and we input some data in the table LOGIN....then

    agin we go to solution explorer and we add CRYSTAL REPORT through ADD NEW ITEM....

    now what i do for i want to create a crystal report for table LOGIN and its DATA mince ID and PASSWORD.....

    i hope now you understand.....

    please kindly consider my QUESTION and giving me answer....

    i m waiting for your answer.....

    ReplyDelete
  35. first we will go for new website and we open a new website.....then

    through the solution explorer we do through the ADD NEW ITEM and we seclect SQL SERVER DATABASE and we add that....whose name is like "INVETORY.mdf"...then

    we open a server explorer and we add the table in INVETORY.mdf database.....here table name is "LOGIN"....who has a two field......ID and PASSWORD.....then

    now we open table LOGIN and SHOW TABLE DATA and we input some data in the table LOGIN....then

    agin we go to solution explorer and we add CRYSTAL REPORT through ADD NEW ITEM....

    now what i do for i want to create a crystal report for table LOGIN and its DATA mince ID and PASSWORD.....

    i hope now you understand.....

    please kindly consider my QUESTION and giving me answer....

    i m waiting for your answer.....

    ReplyDelete
  36. Hiiiii. i m creating a website. For that i have to create a crystal report. Bt i dn't know about it. this is a new topic for me. Plz help me out.

    ReplyDelete
  37. using CrystalDecisions.CrystalReports.Engine;

    //must be add this namespace

    ReplyDelete
  38. i do this same but when i added page_load code
    the the grouped fields did not worked properly.

    so, please provide me solution immediately.............

    ReplyDelete
  39. I follow all the steps and my page load succceeded but while running the page it need password while i copy the code behing code on page_load event and when i entered the password it remains on same page n the password textbox become empty after loading.

    ReplyDelete
  40. @Monik:

    Hi monik, you doesn't have too provide password through textbox, just write code i mentioned above in Page_Load event and it will autometically take the password

    ReplyDelete
  41. Hi,

    good article, can u describe that how to take a print out of data of a web form like... I am updating some data online, at last I want to take a print out of that in a report form including my updation....
    thankx in advance...

    ReplyDelete
  42. thanks a lot man...
    i was looking for it...

    ReplyDelete
  43. can u plz tell me how to set the page size while creating the crystal report......

    ReplyDelete
  44. @Priya: you can set the page size by setting the horizontal and vertical rulers bar

    ReplyDelete
  45. thanks alot for such a nice post. its greet job.

    ReplyDelete
  46. very good for beginners

    ReplyDelete
  47. i want to show 58 columns in the crystal report
    in asp.net using c# 2008
    anyone can help me????????????Pls give me solution as soon as possible i really need it.

    ReplyDelete
  48. Thanks great programming

    ReplyDelete
  49. hai!frnds...
    it's really cool....i have one problem in my table i have two column..bt when i add the table one column only DISPLAY IN INSERT GROUP MESSAGE BOX the crystal report...any one help me?

    ReplyDelete
  50. @Above: plz reconfigure your report and check whether all the fields are displayed in field explorer ?

    ReplyDelete
  51. sir,
    please help me , getting error of logon failed..
    what to do???

    ReplyDelete
  52. @Above: Logon failed means you have entered wrong username or password of sql server login in code behind , plz try logging in in sql server management studio with the user name and password u have written in code

    ReplyDelete
  53. Excellent!!! But when I use one of the buttons in the CrystalReportViewer toolbar on runtime, like Searchbutton, refreshbutton or ToggleGroupTreeButton the page goes blank (All white). Any ideas why?
    Thank you...

    ReplyDelete
  54. Thanks,
    i got error on ReportDocument but by adding "using CrystalDecisions.CrystalReports.Engine;"
    as namespace in code behind it worked .

    ReplyDelete
  55. This comment has been removed by a blog administrator.

    ReplyDelete
  56. Can u please tell me how to generate the reports dynamically (taking inputs from user at run time )in asp.net and coding tutorials in vb.net

    Thank you in advance,

    ReplyDelete
  57. getting this message evry time please help
    Object reference not set to an instance of an object.

    ReplyDelete
  58. how can i use search in this?
    i want to search ID in crystal report from text box how can i do this,??
    and when i use parameter for search ID it is showing
    Object reference not set to an instance of an object
    please help

    ReplyDelete
  59. @asif: Please refer Pass Crystal Report Parameters Programmatically To know how to search id and show report

    ReplyDelete
  60. I have problem that print button ids not working..

    ReplyDelete
  61. thanx this code is very useful 4 me.........thnxzzz

    ReplyDelete
  62. he have un problem to print this is an erro message:
    Error in file {ETAT_BON_DE_COMMANDE 2E8AACC2-77BC-4616-8111-03EE6174224D} rpt.:
    Access denied to file report. There may be in use by another program.

    ReplyDelete
  63. this is me problem :
    Error in file {ETAT_BON_DE_COMMANDE 2E8AACC2-77BC-4616-8111-03EE6174224D} rpt.:
    Access denied to file report. There may be in use by another program.

    ReplyDelete