Search code examples
c#sqldatabasevisual-studio-2010crystal-reports

Crystal Reports C#


I have a multitable c# report based on a database I have created underneath using SQL. I am using the following code to try and set the datasource and have tried many different variations but cannot get the information from my database to show up properly in my report. Any help is greatly appreciated. The dataset is populating. Additionally the single table reports properly fill with data only the multitable reports aren't working.

UPDATE: The report now shows data from one table using report.Database.Tables["Invoices"].setDataSource(posDataSet.Tables["Invoices"]); However using the same logic on the table that generates the detail lines does not show any data.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

namespace SureSalePos
{
    public partial class Reporter : Form
    {

        private PosDatabaseDataSet posDataSet;
        public Reporter(PosDatabaseDataSet dataToReportOn)
        {
            InitializeComponent();
            posDataSet = dataToReportOn;
           //InvoiceReport ir = new InvoiceReport();
           // this.reportViewer.LocalReport.ReportPath = ir.FileName;

        }

        private void Reporter_Load(object sender, EventArgs e)
        {
            ConnectionInfo myConnectionInfo = new ConnectionInfo();

            myConnectionInfo.ServerName = "";// "PosDatabaseDataSet";
            myConnectionInfo.DatabaseName = "";//"PosDatabase.mdf";
            myConnectionInfo.UserID = string.Empty;
            myConnectionInfo.Password = string.Empty;
            setDBLOGONforREPORT(myConnectionInfo);
            //this.reportViewer.RefreshReport();
        }

        private void setDBLOGONforREPORT(ConnectionInfo myconnectioninfo)
        {
            InvoiceReport report = new InvoiceReport();
            report.DataSourceConnections.Clear();

            // For each table set the source from your dataset
            report.Database.Tables["Invoices"].SetDataSource(posDataSet.Tables["Invoices"]);
            report.Database.Tables["InvoiceItems"].SetDataSource(posDataSet.Tables["InvoiceItems"]);
            //report.Database.Tables["People"].SetDataSource(posDataSet.Tables["People"]);
            report.Database.Tables["Preferences"].SetDataSource(posDataSet.Tables["Preferences"]);
            //report.Database.Tables["Skus"].SetDataSource(posDataSet.Tables["Skus"]);

            crystalReportViewer1.ReportSource = report;
            crystalReportViewer1.RefreshReport();

        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {
        }
    }
}

EDIT: Updated Code above September 22


Solution

  • You shouldn't need to set the login since you are supplying the report data. Try the following:

    report.DataSourceConnections.Clear();
    
    // For each table set the source from your dataset
    report.Database.Tables["TableName"].SetDataSource(posDataSet.Tables["TableName"]);
    
    crystalReportViewer1.ReportSource = report;
    crystalReportViewer1.RefreshReport();