Link to home
Start Free TrialLog in
Avatar of dcass
dcassFlag for United States of America

asked on

.NET, C#, Windows App, Crystal Reports, SQL - asks for password - can't use - examples don't work

I am using .NET, C#, Windows App, Crystal Reports, SQL which is easy to create and runs but it asks the USER for the SQL password.  I can't use it because I can't give the users the SQL password and from what I read there is no way to specify the password in Crystal, you have to do it in the C# program.  I've tried about 10 examples I found on the internet, but none of them would compile, including the Microsoft example with ConnectionInfo.   If you know what I'm talking about, it uses REPORT which doesn't exist and it doesn't tell where it came from (maybe I need a using statement, but I've tried every one that has anything to do with Crystal and it still didn't work).  I'm a C# novice, so if you would rather send a VB example, feel free - I'll take anything that will get rid of the password inquiry.   But I need the entire code, not just pieces like Microsoft does, so a full example would be great.
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dcass

ASKER

It's still asking for the password, but when I type it in, it works.
I know I probably put the code in the wrong place, so here it is:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


namespace BlountReports
{
      public class Form1 : System.Windows.Forms.Form
      {
            private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
            private System.ComponentModel.Container components = null;
            public Form1()
            {
                  InitializeComponent();
            }
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code

            private void InitializeComponent()
            {
                  this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
                  this.SuspendLayout();
                  this.crystalReportViewer1.ActiveViewIndex = -1;
                  this.crystalReportViewer1.Location = new System.Drawing.Point(24, 24);
                  this.crystalReportViewer1.Name = "crystalReportViewer1";
                  this.crystalReportViewer1.ReportSource = "C:\\Visual Studio Projects\\BlountReports\\BFLabels.rpt";
                  this.crystalReportViewer1.Size = new System.Drawing.Size(600, 272);
                  this.crystalReportViewer1.TabIndex = 0;
                  this.crystalReportViewer1.Load += new System.EventHandler(this.crystalReportViewer1_Load);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(680, 334);
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {                                             this.crystalReportViewer1});
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);
            }
            #endregion

            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void crystalReportViewer1_Load(object sender, System.EventArgs e)
            {
            }
            
            private void FixDatabase (ReportDocument report, String un, String pass, String dbName, String serverName)
            {
                        foreach (Table table in report.Database.Tables)
                        {
                              TableLogOnInfo logOnInfo = table.LogOnInfo;
                              if (logOnInfo != null)
                              {
                                    logOnInfo.ConnectionInfo.UserID = "sa";
                                    logOnInfo.ConnectionInfo.Password = "XXXXXXXXX";
                                    logOnInfo.ConnectionInfo.DatabaseName = "Database1";
                                    logOnInfo.ConnectionInfo.ServerName = "Server1";
                                    table.ApplyLogOnInfo (logOnInfo);
                              }
                        }

                        foreach (ReportObject reportObject in report.ReportDefinition.ReportObjects)
                              if (reportObject.Kind == ReportObjectKind.SubreportObject)
                                    FixDatabase (report.OpenSubreport (((SubreportObject)reportObject).SubreportName), un, pass, dbName, serverName);
            }
            
      }
}
Avatar of dcass

ASKER

I found a way to make it work so I'm withdrawing the question:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace BlountReports
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
            //CrystalReport1 crReportDocument;
            TableLogOnInfos crTableLogonInfos;
            TableLogOnInfo crTableLogonInfo;
            ConnectionInfo crConnectionInfo;
                   
            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();
                  
                  //crReportDocument = new CrystalReport1();
                  crTableLogonInfos = new TableLogOnInfos();
                  crTableLogonInfo = new TableLogOnInfo();
                  crConnectionInfo = new ConnectionInfo();
                  crConnectionInfo.ServerName = "ServerName";
                  crConnectionInfo.DatabaseName = "DBName";
                  crConnectionInfo.UserID = "sa";
                  crConnectionInfo.Password = "XXXX"
                  crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                  crTableLogonInfo.TableName = "table name";
                  crTableLogonInfos.Add(crTableLogonInfo);
                  crystalReportViewer1.LogOnInfo = crTableLogonInfos;
                  //crystalReportViewer1.ReportSource = crReportDocument;

            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();

                  this.SuspendLayout();
                  //
                  // crystalReportViewer1
                  //
                  this.crystalReportViewer1.ActiveViewIndex = -1;
                  this.crystalReportViewer1.Location = new System.Drawing.Point(24, 24);
                  this.crystalReportViewer1.Name = "crystalReportViewer1";
                  this.crystalReportViewer1.ReportSource = "C:\\Visual Studio Projects\\Reports\\Labels.rpt";
                  this.crystalReportViewer1.Size = new System.Drawing.Size(600, 272);
                  this.crystalReportViewer1.TabIndex = 0;
                  this.crystalReportViewer1.Load += new System.EventHandler(this.crystalReportViewer1_Load);

                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(680, 334);
                  this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                              this.crystalReportViewer1});
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

                                    
            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {

                  Application.Run(new Form1());
            }

            private void crystalReportViewer1_Load(object sender, System.EventArgs e)
            {  
      
            }
            

      
      }
}
Avatar of dcass

ASKER

I solved it myself but would like to give TheAvenger 150 points for responding.