0

I'm creating an application with multiple .mdf User can navigate in different database. Changing connection in my application is working fine. But when i try to use my connection in Crystal Report, error comes out ReportError

EDIT: Is there a way without running the sp_attach_db

public static void Show(ReportDocument reportDocument, string reportFileName)
{
  try
  {
    ReportViewer reportViewer = new ReportViewer();
    CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;
    CrystalDecisions.CrystalReports.Engine.SubreportObject crSubreportObject;
    CrystalDecisions.CrystalReports.Engine.ReportDocument crSubreportDocument;
    CrystalDecisions.CrystalReports.Engine.Database crDatabase;
    CrystalDecisions.CrystalReports.Engine.Tables crTables;
    TableLogOnInfo crTableLogOnInfo;

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

    crConnectioninfo.ServerName = @".\SQLExpress";
    crConnectioninfo.DatabaseName = @"C:\Database\MyDatabase.mdf";
    crConnectioninfo.IntegratedSecurity = true;
    crDatabase = reportDocument.Database;
    crTables = crDatabase.Tables;

    foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
    {
      crTableLogOnInfo = crTable.LogOnInfo;
      crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
      crTable.ApplyLogOnInfo(crTableLogOnInfo);
    }

    CrystalDecisions.CrystalReports.Engine.Sections crSections = reportDocument.ReportDefinition.Sections;
    foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in crSections)
    {
      crReportObjects = crSection.ReportObjects;
      foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crReportObjects)
      {
        if (crReportObject.Kind == ReportObjectKind.SubreportObject)
        {
          crSubreportObject = (CrystalDecisions.CrystalReports.Engine.SubreportObject)crReportObject;
          crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
          crDatabase = crSubreportDocument.Database;
          crTables = crDatabase.Tables;

          foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
          {
            crConnectioninfo.ServerName = @".\SQLExpress";
            crConnectioninfo.DatabaseName = @"C:\Database\MyDatabase.mdf";
            crConnectioninfo.IntegratedSecurity = true;
            crTableLogOnInfo = crTable.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = crConnectioninfo;
            crTable.ApplyLogOnInfo(crTableLogOnInfo);
          }
        }
      }
    }

    reportViewer.RptDocument = reportDocument;
    reportViewer.RptDocument.ExportToDisk(ExportFormatType.PortableDocFormat, reportFileName);
    reportViewer.ReportFileName = reportFileName;
    reportViewer.Show();
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message, "Error in Loading Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}
jayvee
  • 160
  • 2
  • 17
  • 1
    No - a `.mdf` file is a **SQL Server** database file, and those need to be **attached** to a SQL Server instance to be accessible. You cannot just read directly from those `.mdf` files.... – marc_s Oct 11 '14 at 07:34
  • Thanks @marc_s now its clear to me. – jayvee Oct 11 '14 at 08:16

0 Answers0