简体   繁体   中英

Creating Crystal report with C# and Sql server

I'm using Crystal report to generate reports to my application, this is the code i used:

private void button5_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("C:\\Documents and Settings\\Administrateur\\Mes documents\\MyApplication\\MyApplication\\CrystalReport1.rpt");

        crConnectionInfo.ServerName = ".\\SQLEXPRESS";
        crConnectionInfo.DatabaseName = "database";
        crConnectionInfo.UserID = "";
        crConnectionInfo.Password = "";
        crConnectionInfo.IntegratedSecurity = true;
        CrTables = cryRpt.Database.Tables;

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

        cryRpt.SetDatabaseLogon("", "", ".\\SQLEXPRESS", "database");
        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh();
    }

But when i run the application, a login screen appeared and demands the connection Id and password, i tried to enter null values but the connection is failed.

Where is the problem??

You have to initialize an instance of the DataSet class, and fill it with information from

your DataSet because Crystal report Datasource is based on a dataset. This is a basic code of

using Crystal report with Dataset:

SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=YOUR PATH\database.mdf;Integrated Security=True;User Instance=True";
        con.Open();
        string sql = "SELECT * FROM tablename";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        dscmd.Fill(ds, "tablename");
        con.Close();

        CrystalReport1 objRpt = new CrystalReport1();
        objRpt.SetDataSource(ds.Tables["tablename"]);
        crystalReportViewer1.ReportSource = objRpt;
        crystalReportViewer1.Refresh();

while we are creating the crystal report using windows explorer we have to provide the location of database ex: "C:\\data" then once after that it works perfect but if we change the location of database file or the location of application it asks for login. The refresh of the datasource may also resolve your problem

consider this exapmle and try to resolve your problems here

First goto database expert and check right side whether there is a single connection.if one or more connections there delete unwanted connection like access connections then copy and paste the below code it works perfectly

 ReportDocument cryRpt = new ReportDocument();

            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            string path = "" + Application.StartupPath + "\\Mgc\\Invoice.rpt";
            cryRpt.Load(path);

            cryRpt.SetParameterValue("billno", Program.billno);

            crConnectionInfo.UserID = "userid";
            crConnectionInfo.Password = "Password";
            crConnectionInfo.ServerName = "servernme";

            crConnectionInfo.DatabaseName = "database;


            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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM