CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Posts
    87

    how to dynamically change connection string in crystal reports at runtime?

    plz. tell me

    how to dynamically change connection string in crystal reports at runtime?

    Like i can get the connection string from an xml file at runtime, and modify its value anytime.

    thanks

  2. #2
    Join Date
    Mar 2000
    Location
    Germany, Franken
    Posts
    257
    I normally do it this way:

    Code:
    private bool ConnectDB( )
      {
        Database                                  crDatabase;
        Tables                                    crTables;
        TableLogOnInfo                            crTableLogOnInfo;
        ConnectionInfo                            crConnectionInfo;
    
        try
          {
          crConnectionInfo = new ConnectionInfo();
          crConnectionInfo.ServerName   = (string)Application["DataSource"];
    
          // the tables for the report are always at the global database
          crConnectionInfo.DatabaseName = "mydatabase";
          crConnectionInfo.UserID       = (string)Application["User"];
          crConnectionInfo.Password     = (string)Application["Password"];
    
          //Get the tables collection from the report object
          crDatabase = m_oRpt.Database;
          crTables = crDatabase.Tables;
    
          //Apply the logon information to each table in the collection
          foreach( CrystalDecisions.CrystalReports.Engine.Table crTable in crTables )
            {
            crTableLogOnInfo = crTable.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
            crTable.ApplyLogOnInfo( crTableLogOnInfo );
    
            // test the database connection
            bool bTest =  crTable.TestConnectivity();
            if( !bTest )
              {
              Response.Write( "Failed to Logon Table: " + crTable.Name + "<br>" );
              }
            }
          }
        catch( CrystalDecisions.CrystalReports.Engine.LogOnException ex )
          {
            Response.Write( "Error while Database Logon. Please check Logon inforamtion. <br>" );
            Response.Write( "Username: " + (string)Application["User"] +
                            "<br>Database:mydatabasel<br>DataSource: " +
                            (string)Application["DataSource"] + "<br>" );
            Response.Write( ex.Message + "<br>" + ex.Source + "<br>" + ex.StackTrace + "<br>"  + ex.TargetSite.Name );
            return( false );
          }
        catch( Exception /*ex*/ )
          {
          return( false );
          }
        return( true );
      }
    Hope this helps

  3. #3
    Join Date
    Sep 2002
    Posts
    87
    i did, but still its loading from the database, which was specified at design time.

    Kindly tell me how to do it.

    Thanks again
    Last edited by Farhad; April 5th, 2004 at 11:31 PM.

  4. #4
    Join Date
    Mar 2000
    Location
    Germany, Franken
    Posts
    257
    mhm, strange thing. When you save your report do you set the option "Save report with data" to true or to false?

  5. #5
    Join Date
    Sep 2002
    Posts
    87
    it never asked me to save report with data?

    how to set that?

  6. #6
    Join Date
    Apr 2005
    Location
    Mainz, Germany
    Posts
    3

    Re: how to dynamically change connection string in crystal reports at runtime?

    I've had some weired behavior with the Database Connection on installations. Things got better with later versions of the Merge Moduls.
    It's worth a try!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured