Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
every time ask me to enter login and then login failed?

private void button1_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables ;

            cryRpt.Load("F:\\final_library\\final_library\\allemp_rep.rpt");

            crConnectionInfo.ServerName = "192.168.1.3";
            crConnectionInfo.DatabaseName = "f_library";
            crConnectionInfo.UserID = "sa";
            crConnectionInfo.Password = "123456";

            CrTables = cryRpt.Database.Tables ;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();
        }
Posted

1 solution

C#
using CrystalDecisions.CrystalReports.Engine; 


using CrystalDecisions.Shared; 



 



public ReportDocument ConnectionInfo(ReportDocument rpt)
   {
       ReportDocument crSubreportDocument;
       string[] strConnection = ConfigurationManager.ConnectionStrings[("AppConn")].ConnectionString.Split(new char[] { ';' });

       Database oCRDb = rpt.Database;

       Tables oCRTables = oCRDb.Tables;

       CrystalDecisions.CrystalReports.Engine.Table oCRTable = default(CrystalDecisions.CrystalReports.Engine.Table);

       TableLogOnInfo oCRTableLogonInfo = default(CrystalDecisions.Shared.TableLogOnInfo);

       ConnectionInfo oCRConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();

       oCRConnectionInfo.ServerName = strConnection[0].Split(new char[] { '=' }).GetValue(1).ToString();
       oCRConnectionInfo.Password = strConnection[2].Split(new char[] { '=' }).GetValue(1).ToString();
       oCRConnectionInfo.UserID = strConnection[1].Split(new char[] { '=' }).GetValue(1).ToString();

       for (int i = 0; i < oCRTables.Count; i++)
       {
           oCRTable = oCRTables[i];
           oCRTableLogonInfo = oCRTable.LogOnInfo;
           oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo;
           oCRTable.ApplyLogOnInfo(oCRTableLogonInfo);
           if (oCRTable.TestConnectivity())
               //' If there is a "." in the location then remove the
               // ' beginning of the fully qualified location.
               //' Example "dbo.northwind.customers" would become
               //' "customers".
               oCRTable.Location = oCRTable.Location.Substring(oCRTable.Location.LastIndexOf(".") + 1);


       }

       for (int i = 0; i < rpt.Subreports.Count; i++)
       {

           {
               //  crSubreportObject = (SubreportObject);
               crSubreportDocument = rpt.OpenSubreport(rpt.Subreports[i].Name);
               oCRDb = crSubreportDocument.Database;
               oCRTables = oCRDb.Tables;
               foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in oCRTables)
               {
                   oCRTableLogonInfo = aTable.LogOnInfo;
                   oCRTableLogonInfo.ConnectionInfo = oCRConnectionInfo;
                   aTable.ApplyLogOnInfo(oCRTableLogonInfo);
                   if (aTable.TestConnectivity())
                       //' If there is a "." in the location then remove the
                       // ' beginning of the fully qualified location.
                       //' Example "dbo.northwind.customers" would become
                       //' "customers".
                      aTable.Location = aTable.Location.Substring(aTable.Location.LastIndexOf(".") + 1);

               }
           }
       }
       //  }

       rpt.Refresh();
       return rpt;
   }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900