nemi007
May 12th, 2006, 03:52 AM
Hallo all,
I have a problem with dynamic connection to Crystal Report. I need dynamically change server name, dbname, userid and password. I use this code to change this parameter. This code is ok only on server where is the SQL server. When i run report on client station open the form for connection params? The user must set the servername, dbname, userid, password. But it doesnt help. Its return login failed. Why? How to make connection dynamically? Thanks for your help.
public CrystalViewer(TEST.SBO.Connection oConn, TESToAddOn oAddOn, string DocNum, string cisloradku) {
cConn = oConn;
cAddOn = oAddOn;
CisloFaktury = DocNum;
InitializeComponent();
crConnectioninfo = new ConnectionInfo();
crReportDocument.Load(cConn.getResourcesPath() + @"\ServisniHlaseni.rpt");
ApplyLogon(crReportDocument,crConnectioninfo);
Logon(crReportDocument,cConn.Company.Server,cConn.Company.CompanyDB,"TEST","******");
crystalReportViewer1.ReportSource = crReportDocument;
crystalReportViewer1.SelectionFormula = "{OSCL.callId} = "+CisloFaktury+" AND {_AXM_OSERVISZPR.U_rowID} = "+cisloradku+" and {_AXM_OSERVISZPR.U_callID} = "+CisloFaktury+"";
}
/// <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.Dock = System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.SelectionFormula = "";
this.crystalReportViewer1.Size = new System.Drawing.Size(680, 373);
this.crystalReportViewer1.TabIndex = 0;
//
// CrystalViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 373);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "CrystalViewer";
this.Text = "CrystalViewer";
this.Load += new System.EventHandler(this.CrystalViewer_Load);
this.ResumeLayout(false);
}
#endregion
private void CrystalViewer_Load(object sender, System.EventArgs e) {}
bool ApplyLogon(ReportDocument cr, ConnectionInfo ci) {
TableLogOnInfo li;
// for each table apply connection info
foreach (Table tbl in cr.Database.Tables) {
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful
// if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity()) {
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0) {
string table = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
tbl.Location = cConn.Company.CompanyDB + ".dbo." + table;
}
else {
tbl.Location = tbl.Location;
}
}
else {
return(false);
}
}
return(true);
}
// The Logon method iterates through all tables
bool Logon(ReportDocument cr, string server,string db, string id, string pass) {
ConnectionInfo ci = new ConnectionInfo();
SubreportObject subObj;
ci.ServerName = server;
ci.DatabaseName = db;
ci.UserID = id;
ci.Password = pass;
if (!ApplyLogon(cr, ci)){
return (false);
}
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects) {
if (obj.Kind == ReportObjectKind.SubreportObject) {
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci)){
return(false);
}
}
}
return (true);
}
I have a problem with dynamic connection to Crystal Report. I need dynamically change server name, dbname, userid and password. I use this code to change this parameter. This code is ok only on server where is the SQL server. When i run report on client station open the form for connection params? The user must set the servername, dbname, userid, password. But it doesnt help. Its return login failed. Why? How to make connection dynamically? Thanks for your help.
public CrystalViewer(TEST.SBO.Connection oConn, TESToAddOn oAddOn, string DocNum, string cisloradku) {
cConn = oConn;
cAddOn = oAddOn;
CisloFaktury = DocNum;
InitializeComponent();
crConnectioninfo = new ConnectionInfo();
crReportDocument.Load(cConn.getResourcesPath() + @"\ServisniHlaseni.rpt");
ApplyLogon(crReportDocument,crConnectioninfo);
Logon(crReportDocument,cConn.Company.Server,cConn.Company.CompanyDB,"TEST","******");
crystalReportViewer1.ReportSource = crReportDocument;
crystalReportViewer1.SelectionFormula = "{OSCL.callId} = "+CisloFaktury+" AND {_AXM_OSERVISZPR.U_rowID} = "+cisloradku+" and {_AXM_OSERVISZPR.U_callID} = "+CisloFaktury+"";
}
/// <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.Dock = System.Windows.Forms.DockStyle.Fill;
this.crystalReportViewer1.Location = new System.Drawing.Point(0, 0);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.SelectionFormula = "";
this.crystalReportViewer1.Size = new System.Drawing.Size(680, 373);
this.crystalReportViewer1.TabIndex = 0;
//
// CrystalViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 373);
this.Controls.Add(this.crystalReportViewer1);
this.Name = "CrystalViewer";
this.Text = "CrystalViewer";
this.Load += new System.EventHandler(this.CrystalViewer_Load);
this.ResumeLayout(false);
}
#endregion
private void CrystalViewer_Load(object sender, System.EventArgs e) {}
bool ApplyLogon(ReportDocument cr, ConnectionInfo ci) {
TableLogOnInfo li;
// for each table apply connection info
foreach (Table tbl in cr.Database.Tables) {
li = tbl.LogOnInfo;
li.ConnectionInfo = ci;
tbl.ApplyLogOnInfo(li);
// check if logon was successful
// if TestConnectivity returns false, check
// logon credentials
if (tbl.TestConnectivity()) {
// drop fully qualified table location
if (tbl.Location.IndexOf(".") > 0) {
string table = tbl.Location.Substring(tbl.Location.LastIndexOf(".") + 1);
tbl.Location = cConn.Company.CompanyDB + ".dbo." + table;
}
else {
tbl.Location = tbl.Location;
}
}
else {
return(false);
}
}
return(true);
}
// The Logon method iterates through all tables
bool Logon(ReportDocument cr, string server,string db, string id, string pass) {
ConnectionInfo ci = new ConnectionInfo();
SubreportObject subObj;
ci.ServerName = server;
ci.DatabaseName = db;
ci.UserID = id;
ci.Password = pass;
if (!ApplyLogon(cr, ci)){
return (false);
}
foreach (ReportObject obj in cr.ReportDefinition.ReportObjects) {
if (obj.Kind == ReportObjectKind.SubreportObject) {
subObj = (SubreportObject)obj;
if (!ApplyLogon(cr.OpenSubreport(subObj.SubreportName), ci)){
return(false);
}
}
}
return (true);
}