Dynamically login in Crystal Reports

Here I am going to show you how to dynamically login in Crystal Reports. Sometimes developer faces the issue that he has to provide server name and etc this code will help you to remove those exceptions in applications.

  1. TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();                   
  2. TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();  
  3. Tables CrTables;  
  4.   
  5. ConnectionInfo Cr = new ConnectionInfo();  
  6. Cr.ServerName = "";//here you should provide your Server Name  
  7. Cr.DatabaseName = "";//here you should provide your Database Name  
  8. Cr.UserID = "";//here you should provide your User Name  
  9. Cr.Password = "";//here you should provide your Password  
  10. CrTables = Ip.Database.Tables;  
  11.   
  12. foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)  
  13. {  
  14.     crtableLogoninfo = CrTable.LogOnInfo;  
  15.     crtableLogoninfo.ConnectionInfo = Cr;  
  16.     CrTable.ApplyLogOnInfo(crtableLogoninfo);  
  17. }  
  18. crystalReportViewer1.ReportSource = Ip;  

In this code I have created a crystal report and created its object IP and used that in the report source.