troubleshooting Question

VB.net to C# conversion CrystalReports DB login failed

Avatar of triphen
triphen asked on
C#Crystal Reports
3 Comments1 Solution178 ViewsLast Modified:
Hello Experts,

I am changing some code from VB.net to C#. The original code would load a Crystal report and Export it to PDF. The new code wont connect to the database, but none of the DB names or login info has changed. I get this error:

CrystalDecisions.CrystalReports.Engine.LogOnException: Database logon failed. ---> System.Runtime.InteropServices.COMException: Database logon failed.

   at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext)

   at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)

   --- End of inner exception stack trace ---

   at CrystalDecisions.ReportAppServer.ConvertDotNetToErom.ThrowDotNetException(Exception e)

   at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)

   at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)

   at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)

   at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export()

   at CrystalDecisions.CrystalReports.Engine.ReportDocument.Export()

   at Member.frmTransactionHistory.btnEmailTransactionHistory_Click(Object sender, EventArgs e) in C:\frmTransactionHistory.cs:line 236


VB.net Code
        'Load the PDF
        Dim cryRpt As New ReportDocument
        Dim crParameterFieldDefinitions As ParameterFieldDefinitions
        Dim crParameterFieldDefinition As ParameterFieldDefinition
        Dim crParameterValues As New ParameterValues
        Dim crParameterDiscreteValue As New ParameterDiscreteValue

        cryRpt.Load(Application.StartupPath & "\Reports\StdRecpt.rpt")

        crParameterDiscreteValue.Value = (txt_TransNum.Text)
        crParameterFieldDefinitions = _
        cryRpt.DataDefinition.ParameterFields
        crParameterFieldDefinition = _
        crParameterFieldDefinitions.Item("Transact")
        crParameterValues = crParameterFieldDefinition.CurrentValues
        crParameterValues.Clear()
        crParameterValues.Add(crParameterDiscreteValue)
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

      cryRpt.SetDatabaseLogon("user", "password", "sqlbase", "")

        'Export the PDF
        Try
            Dim CrExportOptions As ExportOptions
            Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
            Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions
            CrDiskFileDestinationOptions.DiskFileName = (Application.StartupPath & "\Exports\PDF\Trans# " & txt_TransNum.Text & ".pdf")
            CrExportOptions = cryRpt.ExportOptions
            With CrExportOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
                .DestinationOptions = CrDiskFileDestinationOptions
                .FormatOptions = CrFormatTypeOptions
            End With
            cryRpt.Export()


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error: Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        End Try



C# Code
 try
                {
                    ReportDocument cryRpt;
                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues;
                    ParameterDiscreteValue crParameterDiscreteValue;
                    cryRpt = new ReportDocument();
                    
                    crParameterValues = new ParameterValues();
                    crParameterDiscreteValue  = new ParameterDiscreteValue();

                    cryRpt.Load(Application.StartupPath + @"\Reports\POSMemberTransactionHistory.rpt");

                    crParameterDiscreteValue.Value = Convert.ToInt32(txtMemcode.Text);
                    crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;
                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                   
                  cryRpt.SetDatabaseLogon("user", "password", "sqlbase", "");

                    ExportOptions CrExportOptions;
                    DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                    PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                    CrDiskFileDestinationOptions.DiskFileName = @"C:\Test.pdf";
                    CrExportOptions = cryRpt.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }

                    cryRpt.Export();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }


My datasource is ODBC. With a System DSN name of "sqlbase".

Help?
ASKER CERTIFIED SOLUTION
triphen

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros