cancel
Showing results for 
Search instead for 
Did you mean: 

ServerName Property in Code Being Ignored

Former Member
0 Kudos

It seems that the connectionInfo.ServerName = "XXX"; property in my code is being ignored. No matter what I type in there, it keeps trying to use the ServerName that was used for the report creation. Unfortunately, that was my development server and now I need it to use my clients production server. It applies the user and pass correctly as I tried changing those values in the code and it reflected in the login prompt that eventually appears.

I'm fairly certain my code is right as I relied on the VS2005 walkthrough for a lot of it but I am also new to Crystal Reports. Any help would be appreciated.


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

namespace Val_Report
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            
                //Set up database and report path
                ConfigureCrystalReports();

                //Set Report Dates Initially at first of month to current date

                //Get first day of month
                DateTime firstDay = DateTime.Now;
                DateTime FirstDayInMonth = new DateTime(firstDay.Year, firstDay.Month, 1);
                string strStartDate = FirstDayInMonth.ToShortDateString();
                //Set the orderStartDate TimePicker to value
                orderStartDate.Value = FirstDayInMonth;

                //Get current date
                string strEndDate = DateTime.Now.ToShortDateString();

                //Set the orderEndDate TimePicker to value
                orderEndDate.Value = DateTime.Now;

                //Set up my report parameters (date, etc)
                setReportParameters(strStartDate, strEndDate);
            
            
        }

        private void ConfigureCrystalReports()
        {
            
            
                //Set up sql connection
                ConnectionInfo connectionInfo = new ConnectionInfo();
                connectionInfo.ServerName = "Database";
                connectionInfo.DatabaseName = "db_name";
                connectionInfo.UserID = "test";
                connectionInfo.Password = "test";
                connectionInfo.IntegratedSecurity = false;


                //Set report path
                string reportPath = Application.StartupPath + "\\" + "ValidationView.rpt";
                crystalReportViewer.ReportSource = reportPath;

                // Loop through main report and set connection info
                SetDBLogonForReport(connectionInfo);
            
            

        }

        private void SetDBLogonForReport(ConnectionInfo connectionInfo)
        {
            TableLogOnInfos tableLogOnInfos = crystalReportViewer.LogOnInfo;
            foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
            {
                tableLogOnInfo.ConnectionInfo = connectionInfo;

            }

        }


        private void crystalReportViewer_Load(object sender, EventArgs e)
        {

        }


        private void setReportParameters(string strStartDate, string strEndDate)
        {
           
            

                // all the parameter fields will be added to this collection 
                ParameterFields paramFields = new ParameterFields();

                // the parameter fields to be sent to the report 
                ParameterField pfStartDate = new ParameterField();
                ParameterField pfEndDate = new ParameterField();

                // setting the name of parameter fields with wich they will be recieved in report 

                pfStartDate.ParameterFieldName = "StartDate";
                pfEndDate.ParameterFieldName = "EndDate";

                // the above declared parameter fields accept values as discrete objects 
                // so declaring discrete objects 
                ParameterDiscreteValue dcStartDate = new ParameterDiscreteValue();
                ParameterDiscreteValue dcEndDate = new ParameterDiscreteValue();

                // setting the values of discrete objects 

                dcStartDate.Value = DateTime.Parse(strStartDate);
                dcEndDate.Value = DateTime.Parse(strEndDate);

                // now adding these discrete values to parameters  

                pfStartDate.CurrentValues.Add(dcStartDate);
                pfEndDate.CurrentValues.Add(dcEndDate);

                // now adding all these parameter fields to the parameter collection 

                paramFields.Add(pfStartDate);
                paramFields.Add(pfEndDate);

                /* Set the modified parameters collection back to the viewer so that
                 the new parameter information can be used for the report. */
                crystalReportViewer.ParameterFieldInfo = paramFields;
            
           
        }

        private void redisplay_Click_1(object sender, EventArgs e)
        {

           // ConfigureCrystalReports();
            
            string strStartDate = orderStartDate.Text;
            string strEndDate = orderEndDate.Text;

            string reportPath = Application.StartupPath + "\\" + "ValidationView.rpt";
            crystalReportViewer.ReportSource = reportPath;

            setReportParameters(strStartDate, strEndDate);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form about = new Validation_Report.About();
            about.ShowDialog();

        }

       
    }
}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

What happens, if you use the ReportDocument instance instead of the viewer to apply the logon infos like this:

public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			ConfigureCrystalReports();
			
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		private void ConfigureCrystalReports()
		{
			reportDoc = new ReportDocument();
            reportDoc.Load(@"C:\Documents and Settings\Administrator\Desktop\Report1.rpt");
			// the report has been generated using your sql script you send to me

			ConnectionInfo connectionInfo = new ConnectionInfo();
			connectionInfo.ServerName = "servername";
			connectionInfo.DatabaseName = "databasename";
			connectionInfo.UserID = "userid";
			connectionInfo.Password = "password";

			SetDBLogonForReport(connectionInfo, reportDoc);

			crystalReportViewer1.ReportSource = reportDoc;
		}

		private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
		{
			Tables tables = reportDocument.Database.Tables;
			foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
			{
				TableLogOnInfo tableLogonInfo = table.LogOnInfo;
				tableLogonInfo.ConnectionInfo = connectionInfo;
				table.ApplyLogOnInfo(tableLogonInfo);
			}
//			TableLogOnInfo tInfo = new TableLogOnInfo();
//			tInfo.ConnectionInfo = connectionInfo;
//			reportDocument.Database.Tables[0].ApplyLogOnInfo(tInfo);
//			reportDocument.Database.Tables[0].Location = "TEST_TABLE";
		}

Cheers

Former Member
0 Kudos

I will give it a try and let you know.

Answers (2)

Answers (2)

former_member208657
Active Contributor
0 Kudos

Alphonse has the correct answer here. There is a limitation in the viewer SDK when changing database information. You can't change servers. If you want to change server's and databases you should use the ReportDocument SDK.

Former Member
0 Kudos

Hi,

More information required-

CR Version?

Service pack applied?

Windows or web app?

Connection type?

Regards,

Amit

Former Member
0 Kudos

CR Version? One provided with VS2005

Service pack applied? All updates have been applied

Windows or web app? Windows

Connection type? OLE DB SQL

Former Member
0 Kudos

Hi,

I am using CR2008 v12.0.0.683 with VS2008 v9.0.21022.8 RTM.

Crystal Reports 2008 for Visual Studio.

I am developing a Web application that uses a PervasiveSQL v10 database.

I have used exactly the same code used in the sample provided for Visual Studio:

CS_Web_RDObjMod_DBLogon

Unfortunately, right after the execution of table.ApplyLogOnInfo(tableLogOnInfo), the values of the TableLogOnInfo go back to the values stored on the original report, and worst of all, I got the error:

"Failed to open the connection"

The report I am currently using is a very simple one I have just created to simplify the problem. It lists just one table without any subreports or parameters. The table listed exists in both databases I want to test, the original set on the report and the one I want the report to collect the information from.

Any idea about why the method ApplyLogOnInfo is not working?

protected void Page_Load(object sender, EventArgs e)

{

string reportName = Request.QueryString["reportName"];

string reportPath = ConfigurationManager.AppSettings.Get("CrystalReportsDefaultFolder") + reportName;

ReportDocument crDocument = new ReportDocument();

crDocument.Load(reportPath);

string dbuser = "dbuser";

string dbpass = "dbPass";

string dbserver = "dbserver";

string dbname = "dbname";

setConnectionDatabaseString(crDocument, dbserver, dbname, dbuser, dbpass);

crystalReportViewer.ReportSource = crDocument;

}

public void setConnectionDatabaseString(ReportDocument document, string server, string database, string user, string password)

{

ConnectionInfo connectionInfo = new ConnectionInfo();

connectionInfo.ServerName = server;

connectionInfo.DatabaseName = database;

connectionInfo.UserID = user;

connectionInfo.Password = password;

Tables tables = document.Database.Tables;

foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)

{

TableLogOnInfo tableLogOnInfo = table.LogOnInfo;

tableLogOnInfo.ConnectionInfo = connectionInfo;

table.ApplyLogOnInfo(tableLogOnInfo);

}

former_member183750
Active Contributor
0 Kudos

Manuel, please post this into a new thread. Way is using CR 10.2 you are using CR 2008. That in it's self may be enough of a difference to troubleshoot the issue differently.

Thank you for your understanding,

Ludek