Creating a Crystal report with C # and Sql servers

I am using Crystal report to generate reports in 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 ran the application, the login screen appeared and required the connection id and password, I tried to enter null values, but the connection failed.

Where is the problem?

+3


source to share


3 answers


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

your DataSet as Crystal Report Datasource is dataset based. This is the basic code



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

      

+2


source


while we generate the crystals report using Windows Explorer, we have to provide the database location ex: "C: \ data" and then after that it works fine, but if we change the location of the database file or app location it asks login. Updating the data source might also fix your problem.



consider this exapmle and try to solve your problems here

0


source


First goto database expert and check the right side if there is one connection. If one or more connections remove unwanted connection like access connections then copy and paste the code below it works great.

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

      

0


source







All Articles