Connect Crystal Reports in Database - c#

I got problem in providing the User Id and Password in Crystal Report of Windows Form. I used Windows Authentication when connecting to SQL Server and sometimes use the sa account with sql server authentication. So I tried the Server name with a blank password but it failed. I also try the sa login of sql server authentication providing the same password I used when I'm connecting to sql server. Is User Id different from Login Name of sqlserver? How will I provide the User Id and password in Crystal Reports?
I also try to add dynamic logon parameter with UserID as sa and with it's password. But still failed.
I already know how to connect my application in database but I think it is different from crystal reports.

Hope you are using Report document and here is the C# code snippet to set the connection on the fly.
You may need to traverse through the sub reports as well to set the connections if any
for (int i = 0; i < reportDocument.DataSourceConnections.Count; i++)
{
reportDocument.DataSourceConnections[i].SetConnection(Server.Name, Database.Name, Server.User.Name,Server.User.Password);
reportDocument.DataSourceConnections[i].SetLogon(Server.User.Name, Server.User.Password);
}

Hi Kindly check out the code snippet below, that is how i set my database connection when using crystal reports.Hope it helps.
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;
cryRpt.Load(Application.StartupPath + "\\CrystalReport1.rpt");
crConnectionInfo.ServerName = "server Name";
crConnectionInfo.DatabaseName = "Database Name";
crConnectionInfo.UserID = "sa";
crConnectionInfo.Password = "****";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

Related

published reports don't work - Database logon failed Error

I am developing a simple web app that has 3 reports created on VS 2013, for some reason those reports run fine from developer mode, but when I publish the website they give me "Database logon failed" Error. What could be causing this? VS is installed on the same server that I am publishing the reports. I have created a local admin user with exactly the same info as the ODBC connection logon user for IIS authentication, but still no luck.
below is my code:
public partial class OpenWOsWebForm : System.Web.UI.Page
{
ConnectionInfo EPAKconnectionInfo = new ConnectionInfo();
protected void SetConnectionInfo()
{
EPAKconnectionInfo.ServerName = ConfigurationManager.AppSettings["EPAK_SERVER_NAME"];
EPAKconnectionInfo.UserID = ConfigurationManager.AppSettings["EPAK_USER_NAME"];
EPAKconnectionInfo.Password = ConfigurationManager.AppSettings["EPAK_PASSWORD"];
}
private void Page_Init(object sender, EventArgs e)
{
ReportDocument report = new ReportDocument();
report.Load("C:\\Delray Beach\\Delray Beach\\Reports\\OpenWOsReport.rpt");
SetConnectionInfo();
TableLogOnInfo crTableLogoninfo = new TableLogOnInfo();
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables)
{
crTableLogoninfo = CrTable.LogOnInfo;
crTableLogoninfo.ConnectionInfo = EPAKconnectionInfo;
CrTable.ApplyLogOnInfo(crTableLogoninfo);
}
foreach (ReportDocument subreport in report.Subreports)
{
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables)
{
crTableLogoninfo = CrTable.LogOnInfo;
crTableLogoninfo.ConnectionInfo = EPAKconnectionInfo;
CrTable.ApplyLogOnInfo(crTableLogoninfo);
}
}
OpenWOsViewer.ReportSource = report;
OpenWOsViewer.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
}
}
Thanks
Besides "Database logon failed", does it say "Unable to connect: incorrect log on parameters"?
If so, try this: https://stackoverflow.com/a/24578304/2248943
And try these:
kill the asp.net proccess (w3wp.exe or aspnet.exe) (it will
restart) .
if you are using a XSD file, check it's location path in
the RPT, try to "set location" again; if it does not work, try to remove the tables and include them ok (this will be hard-working).
I faced this problem many times and each case was solved by one of these actions.
In my case the problem was that on IIS Application Pools/ my web app/ Advanced Settings the 32 bit Applications was disable and I wasn't including on my project the folder aspnet_client where crystal run-times files are storage, so the time I was publishing the app, those files wren't transferred. Once I enabled the connection and included that folder the issue got resolved.

why crystal report is getting all records even with RecordSelectionFormula set?

I am using the following code for loading crystal reports.
ConnectionInfo crconnectioninfo = new ConnectionInfo();
ReportDocument cryrpt = new ReportDocument();
TableLogOnInfos crtablelogoninfos = new TableLogOnInfos();
TableLogOnInfo crtablelogoninfo = new TableLogOnInfo();
Tables CrTables;
crconnectioninfo.ServerName = "localhost";
crconnectioninfo.DatabaseName = "dbclients";
crconnectioninfo.UserID = "ssssssss";
crconnectioninfo.Password = "xxxxxxx";
cryrpt.Load(Application.StartupPath + "\\rpts\\" + dealerInfo.ResourceName);
CrTables = cryrpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtablelogoninfo = CrTable.LogOnInfo;
crtablelogoninfo.ConnectionInfo = crconnectioninfo;
CrTable.ApplyLogOnInfo(crtablelogoninfo);
}
cryrpt.RecordSelectionFormula = getCustInfoRptSelection();
cryrpt.Refresh();
allReportViewer.ReportSource = cryrpt;
getCustInfoRptSelection() is getting a specific client
but the report shows all the clients at first time and when i close the report and open it again it shows the correct record.
so basicly i have to open the report twice to get the correct data though the getCustInfoRptSelection() results is not changing.
and in this case the RecordSelectionFormula =" {dealer.dealer_type_id}=2 and {dealer.DEALER_NAME} like 'Mark stall X'"
I had the exact same problem, and I found this answer on a SAP website that worked
Expand Form1.vb|cs to show Form1.Designer.vb|cs and double-click it to show its code
Find where the initial crystalReportViewer1 properties are set in the the InitializeComponent method
Comment out or delete the following two lines:
this.crystalReportViewer1.SelectionFormula = ""
this.crystalReportViewer1.ViewTimeSelectionFormula = ""
Apparently this was something that was added in one of their newer service packs, as this problem didn't start happening until after I upgraded the Crystal Reports framework for .Net
This one nailed it.
Locate your Windows Form that contains the CrystalReportViewer Control.
Normally, your Windows Form has Resx file and designer file
Example:
Open the Designer file:
Delete the 2 lines below:
This I think is a bug.
I am using VS2015 VB.NET.
It worked!

C# Crystal Reports Login fails on Windows XP

I have written this code with the help of other programmers. This code works perfectly when running on Windows 7 but when I install the program on Windows XP the log-on fails. I have installed .Net Framework 4.0 and CRforVS_13_0_3 of Crystal Reports on the Windows XP machine.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
label2.Text = Convert.ToString(BAssistencia.nroo);
ReportDocument segredo = new ReportDocument();
segredo.Load(#"C:/Relatorios/CrystalReport3.rpt");
ParameterField pf1 = new ParameterField();
ParameterFields pf1s = new ParameterFields();
ParameterDiscreteValue pdv = new ParameterDiscreteValue();
TableLogOnInfo tabla = new TableLogOnInfo();
TableLogOnInfos tablas = new TableLogOnInfos();
ConnectionInfo infocon = new ConnectionInfo();
Tables crtables;
infocon.ServerName = "server";
infocon.DatabaseName = "database";
infocon.UserID = "user";
infocon.Password = "password";
infocon.IntegratedSecurity = false;
crtables = segredo.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crtable in crtables)
{
tabla = crtable.LogOnInfo;
tabla.ConnectionInfo = infocon;
crtable.ApplyLogOnInfo(tabla);
}
pf1.Name = "#pedido";
pdv.Value = label2.Text;
pf1.CurrentValues.Add(pdv);
pf1s.Add(pf1);
crystalReportViewer1.ParameterFieldInfo = pf1s;
crystalReportViewer1.ReportSource = segredo;
What am I doing wrong???
A few links, the first one are extension methods that I've written that I use with Crystal (on XP, Vista and 7 along with Server 2003, 2008 and they work there).
Crystal Reports extension methods for .Net:
http://www.blakepell.com/Main/BlogEntry.aspx?EntryID=e2fcdcf9-312e-4911-8572-20178c94660b
The second one here has a different issue but in the end (before my rant) I have some info about gotchas swapping out connections at runtime.
http://www.blakepell.com/Blog/?p=15
The code provided above has worked for me in all of my environments. Now, in your case, you'll also want to make sure that the XP box has access to the database server outside of Crystal also (no firewalls blocking, the server isn't denying it access, no ACL's in place that are stopping network routing, etc). Over the years I have found swapping connections out with Crystal to be very particular.

Crystal Reports not chaging the server programatically

I'm developing a local app with friends and we're using svn, but we have crystal reports, but it saves the last server used by one of my friends when doing the commit. I tried changing the server programatically using this piece of code, but it didn't work :S
*UPDATE: it seems that the .rpt keeps a history of the server names, and somehow doesn't seem to clear the list, so my friends computer "\sqlexpress" is still there, and I can't seem to clear it :S"
string nombre = WindowsIdentity.GetCurrent().Name.ToString().Split('\\')[1];
ReportDocument cryRpt = new ReportDocument();
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables ;
//cryRpt.SetDatabaseLogon(string.Empty,string.Empty, nombre + "\\sqlexpress","trupp");
cryRpt.Load(FinalPath);
crConnectionInfo.ServerName = nombre + "\\sqlexpress";
crConnectionInfo.IntegratedSecurity = true;
crConnectionInfo.UserID = string.Empty;
crConnectionInfo.Password = string.Empty;
crConnectionInfo.DatabaseName = "trupp";
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crtableLogoninfo = CrTable.LogOnInfo;
crtableLogoninfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crtableLogoninfo);
}
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
You need to make a call to clear the existing connections from the report before setting the new ones.
cryRpt.Load(FinalPath);
// After loading report clear all datasourceconnections
cryRpt.DataSourceConnections.Clear();
// Now you can set new datasourceconnections
crConnectionInfo.ServerName = nombre + "\\sqlexpress";
Sometimes Crystal Reports can be a pain so if for some reason the above doesn't work you can always just set the server name on the existing connection embedded in the report.
cryRpt.DataSourceConnections[0].SetConnection(nombre + "\\sqlexpress", "trupp", true);
It's a pain in the neck, eh?
If you and your friends are using the same make and model of database table server, you might try putting an entry in each developer machine hosts file to create a local hostname alias, or if your development data bases are all on your own machines, use localhost.
If you use ODBC, you can all create your own ODBC entries with the same name and use those.

database login prompt with crystal reports

I'm trying to display some report with some subreports inside it, but every it shows the report it throws some dialog box asking for database connection. I'm using this code:
private void frmReporte_Load(object sender, System.EventArgs e)
{
Clave = ConfigurationSettings.AppSettings["Password"].ToString();
NombreBD = ConfigurationSettings.AppSettings["CatalogBD"].ToString();
NombreServidor = ConfigurationSettings.AppSettings["Servidor"].ToString(); ;
UsuarioBD = ConfigurationSettings.AppSettings["UserID"].ToString();
this.crtReportes.ReportSource = this.prepareReport();
}
public void imprimirReporte()
{
ReportDocument rpt = new ReportDocument();
rpt.Load(mvarRutaReporte);
rpt.SetDataSource(clsReportes.dsReporte);
rpt.PrintToPrinter(1, false, 1, 1);
}
private ReportDocument prepareReport()
{
Sections crSections;
ReportDocument crReportDocument, crSubreportDocument;
SubreportObject crSubreportObject;
ReportObjects crReportObjects;
ConnectionInfo crConnectionInfo;
Database crDatabase;
Tables crTables;
TableLogOnInfo crTableLogOnInfo;
crReportDocument = new ReportDocument();
crReportDocument.Load(RutaReporte);
crReportDocument.SetDataSource(clsReportes.dsReporte.Tables[0]);
crDatabase = crReportDocument.Database;
crTables = crDatabase.Tables;
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = NombreServidor ;
crConnectionInfo.DatabaseName = NombreBD;
crConnectionInfo.UserID = UsuarioBD;
crConnectionInfo.Password = Clave;
foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
// Para los reportes que poseen subreportes
// pongo el objeto seccion del la seccion actual del reporte
crSections = crReportDocument.ReportDefinition.Sections;
// busco en todas las secciones el objeto reporte
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//busco en todos los reportes por subreportes
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
crSubreportObject = (SubreportObject)crReportObject;
//abro el subreporte y me logeo con los datos del reporte general
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
crDatabase = crSubreportDocument.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
{
crTableLogOnInfo = aTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
aTable.ApplyLogOnInfo(crTableLogOnInfo);
}
}
}
}
return crReportDocument;
}
I had a similar problem and it is solved now, so I'm adding this reply in case it might help someone else in my situation.
When setting the SQL Server login info for the report, make sure you include the service name. So, for example, make sure you're giving Crystal "myserver\myservice" instead of just "myserver".
My program is able to access data from SQL Server using just "myserver", but Crystal needs to be given "myserver\myservice".
In crystal you can specify whether to prompt for db login # the time of report generation in Crystal Management Console (CMC) # each report level. Login to CMC, open your report > Process > Database. At the bottom of the page you can specify "Prompt the user for database logon", "Use SSO context for database logon" or "Use same database logon as when report is run". Select the third option for using the credentials stored in the server.
You can just write
report.SetDatabaseLogon("username", "pwd", #"server", "database");
give the login information then the database logon box will not appear when you will run the code.
Some older Crystal Report files saved with prior versions of the SQL Native Client (e.g. Provider: SQLNCLI) will no longer be able to login if either the client or server disables the respective TLS protocol version. For example, if a report was saved with SQL Server 2005 (version 9.00.xx), then TLS 1.0 is needed. If this protocol is disabled, there's no intuitive error, simply a "Database logon failed" is seen programmatically. Client software including rendering in a web browser will be prompted to enter a password, even though the account may have proper SQL permissions.
If rendering reports in IIS applications, see a tool called IISCrypto from Nartac Software to easily turn on and off various protocols, ciphers, hashes, and key exchanges.
Manually editing the report file and using Database - Set Datasource Location when connecting with a more modern native client is one way to fix this so that obsolete and insecure protocols don't need to remain enabled.

Categories