user3035133 user3035133 - 8 days ago 6
C# Question

Saving logon information when loading Crystal Report in C#

I created a Crystal Report that can run fine but it needs enter logon information . I googled and find this code but it did not work for me. Any suggestion with code will be highly appreciated.

private void frmPrintWIPReport_Load(object sender, EventArgs e)
{
var cryRpt = new ReportDocument();
var crtableLogoninfo = new TableLogOnInfo();
var crConnectionInfo = new ConnectionInfo();
Tables CrTables;

cryRpt.Load("rptWIP.rpt");

crConnectionInfo.ServerName = "192.168.40.253";
crConnectionInfo.DatabaseName = "TNET";
crConnectionInfo.IntegratedSecurity = true;
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "******";

CrTables = cryRpt.Database.Tables;
foreach (Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

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

Answer

You can try this. I hope it is same as your need.

private void frmPrintWIPReport_Load(object sender, EventArgs e)
    {
        var cryRpt = new ReportDocument();
        var crtableLogoninfo = new TableLogOnInfo();
        var crConnectionInfo = new ConnectionInfo();
        Tables CrTables;

        cryRpt.Load("rptWIP.rpt");

        ConnectionInfo crConnectionInfo = new ConnectionInfo()
            {
                ServerName = @"192.168.40.253",
                DatabaseName = @"TNET",
                UserID=@"sa",
                Password=@"******"                
               //IntegratedSecurity = true
            };

        CrTables = cryRpt.Database.Tables;
        foreach (Table CrTable in CrTables)
        {
            crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }

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