.

Running Crystal reports off the config file

———————————————————————
Running Crystal reports off the web.config / app.config file:
———————————————————————

 

protected void Page_Load(object sender, EventArgs e)
{
    Reports.Report1 myReportDocument;
    myReportDocument = new Reports.Report1();
    myReportDocument.Load(Server.MapPath("/Reports/Report1.rpt"));

    System.Data.SqlClient.SqlConnectionStringBuilder SB = GetConnectionDetailsFromConfigFile();
    ConnectionInfo myConnectionInfo = GetConnectionInfoFromConStringBuilder(SB);
          
    CrystalReportViewer1.ReportSource = myReportDocument;
    foreach (TableLogOnInfo myTableLogOnInfo in CrystalReportViewer1.LogOnInfo)
    {
        myTableLogOnInfo.ConnectionInfo = myConnectionInfo;
    }

    foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in myReportDocument.Database.Tables)
    {
        TableLogOnInfo li = tbl.LogOnInfo; li.ConnectionInfo = myConnectionInfo; tbl.ApplyLogOnInfo(li);
    }
    myReportDocument.SetParameterValue("title", "TITLE FROM PARAM");

    CrystalReportViewer1.DataBind();
           
}

 

 

/*   NEED THESE FUNCTIONS   */

private static System.Data.SqlClient.SqlConnectionStringBuilder GetConnectionDetailsFromConfigFile()
{
    System.Configuration.ConfigurationElement c =
            System.Configuration.ConfigurationManager.ConnectionStrings["CliniWeb2ConnectionString"];
    string constr = c.ToString();
    System.Data.SqlClient.SqlConnectionStringBuilder s = new System.Data.SqlClient.SqlConnectionStringBuilder(constr);
    return s;
}


    private static ConnectionInfo GetConnectionInfoFromConStringBuilder(System.Data.SqlClient.SqlConnectionStringBuilder SB)
{
    ConnectionInfo myConnectionInfo = new ConnectionInfo();
    myConnectionInfo.ServerName = SB.DataSource;
    myConnectionInfo.DatabaseName = SB.InitialCatalog;
    myConnectionInfo.UserID = SB.UserID;
    myConnectionInfo.Password = SB.Password;
    return myConnectionInfo;
}