0
votes

I use Crystal reports for reporting my application. The problem is that i didn't know how

to set the connection infos for Crystal reports. this is the code i tried:

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;

        crConnectionInfo.ServerName = ".\\SQLEXPRESS";
        crConnectionInfo.DatabaseName = "GestStock.mdf";
        crConnectionInfo.UserID = "";
        crConnectionInfo.Password = "";
        CrTables = cryRpt.Database.Tables;

        cryRpt.Load("C:\\Documents and Settings\\Administrateur\\Mes documents\\GestionStock\\GestionStock\\CrystalReport1.rpt");

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

        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh();
    }

But it doesn't work i got this error message:

Chemin d'accès au fichier de rapport non valide (Invalid report file path)

PS: this the connectionstring of my sql server database:

"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DOCUMENTS AND SETTINGS\ADMINISTRATEUR\MES DOCUMENTS\GESTIONSTOCK\GESTIONSTOCK\GestStock.mdf;Integrated Security=True;User Instance=True";

So how to set it in a correct way?

2
Can you convert the error message to English?HaveNoDisplayName
Invalid report file pathuser4374239

2 Answers

2
votes

Crystal Reports data source is based on a DataSet, so you have to first initialize an instance of the DataSet class, and fill it with information from your DataSet, then set its parameters based on your needs.

Nonetheless, your error message:

Chemin d'accès au fichier de rapport non valide (Invalid report file path)

states that the file path to your report is invalid. So this has nothing to do with an unassigned or a misassigned connection string.

Here's an interesting answer which shows how to use a DataSet to fill in a Crystal report:

Make sure your report gets loaded from the expected file path, and that it can access it (perhaps a concurrent access would be the cause of the problem)?

Once your report gets loaded as expected, then I guess you should try to use a data set to fill the report and set its data source with it as illustrated in the linked answer.

This way, your connection string gets assigned to the SqlConnection used to instantiate the SqlCommand, then your command is passed to SqlDataAdapter.SelectCommand property. This way, you can use default credentials by specifying Integrated Security=true in your connection string and assigning it to the SqlConnection instance prior to open it.

1
votes

The basic code would be like this:-

        SqlConnection cnn ;
        string connectionString = null;
        string sql = null;

        connectionString = "data source=SERVERNAME;initial catalog=DATABASENAME;user  
                            id=USERNAME;password=PASSWORD;";
        cnn = new SqlConnection(connectionString);
        cnn.Open();
        sql = "SELECT columnName FROM tableName";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn);
        DataSet1 ds = new DataSet1();
        dscmd.Fill(ds, "Product");
        MessageBox.Show (ds.Tables[1].Rows.Count.ToString());
        cnn.Close();

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