Search code examples
c#.netwinformscrystal-reports

How to prevent database login window in crystal report?


I am developing a windows application and I use Crystal Report in it (I am new to Crystal reports) the problem I am facing is when I test the report at first load it works fine but when I try to refresh the report it gives me the database login window, is there a way to prevent this window? and how to set the connection string for the report using code?

Notes: 1-I tried

private void crystalReportViewer1_ReportRefresh(object source, CrystalDecisions.Windows.Forms.ViewerEventArgs e)
    {
        Myreport.SetDatabaseLogon("username", "password", "server", "dbname", false);
    }

but I still get the database login window.

2-I use crystal report drag and drop to create my report.

3-this is a windows application and sql server 2008 database C# is the programming language.

4-server in the application may or may not be in the same pc.


Solution

  • I had a similar problem. The SetDatabaseLogon function wasn't working for me so I had to manually assign connection details to each table in the report. I thought the function was geared for SQL Server (I'm using Sybase ASE), but you may be having the same issue as I was.

    ConnectionInfo connInfo = new ConnectionInfo();
    connInfo.ServerName = "Driver={Adaptive Server Enterprise};Server=x.x.x.x;Port=x;";
    connInfo.DatabaseName = "dbname";
    connInfo.UserID = "username";
    connInfo.Password = "password";
    
    TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
    tableLogOnInfo.ConnectionInfo = connInfo;
    
    foreach(Table table in reportDoc.Database.Tables)
    {
      table.ApplyLogOnInfo(tableLogOnInfo);
      table.LogOnInfo.ConnectionInfo.ServerName = connInfo.ServerName;
      table.LogOnInfo.ConnectionInfo.DatabaseName = connInfo.DatabaseName;
      table.LogOnInfo.ConnectionInfo.UserID = connInfo.UserID;
      table.LogOnInfo.ConnectionInfo.Password = connInfo.Password;
    
      // Apply the schema name to the table's location
      table.Location = "dbo." + table.Location;
    }
    

    Clearly your connInfo.ServerName will differ, but I've included the pattern I used for anyone else stuck with this same problem but on ASE.

    Hope this helps.