views:

437

answers:

1

In my (Win32) application, I am displaying Crystal Reports.

I set the login information at runtime. However, Crystal kindly decided to refer to the database name by several different names, depending on how the report connects. For instance, if the report connects through an ODBC connection, then it is called "Data Source", but if it connects directly, then it is called "Server".

Of course we don't know until runtime which report is going to be called.

Currently, I work around the issue by swallowing an exception and trying an alternate method, like this:

procedure TCrystalReporter11.SetLoginInfo(const username, password,
  server : string);
var
  i : integer;
begin
  //set user name and password
  //crystal only accepts these values if they are CONST params
  for i := 1 to FRpt.Database.Tables.Count do begin
    FRpt.Database.Tables[i].ConnectionProperties.Item['User ID'] := username;
    FRpt.Database.Tables[i].ConnectionProperties.Item['Password'] := password;
    try
      {
      Some reports use direct connections, and others use an ODBC Data Source.
      Crystal XI uses a different label to refer to the database name in each
      method.
      I don't know how to determine in advance which method is being used, so:
          First, we try the direct connection.
          If that fails, we try the "data source" method.

      Reference: "Crystal Reports XI Technical Reference", pages 41 thru 46;
                 "Common ConnectionProperties"
      }
      FRpt.Database.Tables[i].ConnectionProperties.Item['Server'] := server;
    except on E: Exception do
      FRpt.Database.Tables[i].ConnectionProperties.Item['Data Source'] := server;
    end;
  end;
end;

Ideally, I'd like to say something like:

case  FRpt.Database.Tables[i].ConnectionProperties.ConnectMethod of
  crymethod_ODBC : sIdx := 'Data Source';
  crymethod_Direct : sIdx := 'Server';
  ...other methods...
end;  //case
FRpt.Database.Tables[i].ConnectionProperties.Item[sIdx] := server;

So my question is:

How can I determine the connection method of a Crystal XI report at runtime, before logging in?

Background info:

  • I'm using Delphi 2007
  • I'm displaying the report using the ActiveX library, which is cumbersome and difficult and stupid and unavoidable (see this post).
  • Reports are in Crystal XI, SP4
  • For the sake of discussion, let's assume reports are all against an Oracle 10g database
  • My dev machine is using Windows Vista, most users are on XP.

Many thanks for any help that someone can offer.

+1  A: 

Look for a DSN item in Tables[i].ConnectionProperties. Non-ODBC won't have it, ODBC always should, AFAIK.

Craig Stuntz