Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass DB logon info to Crystal Report Control? 1

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
OK one more question about using the Crystal Report Viewer control:

I am currently giving my viewer control a report file name using the .ReportSource property of the control. What's happening is that I'm being prompted for Database Logon info (DB Name, UserID, Pwd). I would like to pass these parameters to the report from the control. What is the property I need to address in order to pass this information?

Thanks


CraigHartz
 
...If it helps at all, using the old crystal OCX control the property was the Database --

crxReport.Database.Tables(1).SetLogOnInfo "Database", "", "UserID", "Pwd"

Hope someone can help! I've been looking through the properties and methods of this control and can't identify how to pass the DB logon info.


CraigHartz
 
This is how you would do that:

Code:
Dim rpt As New crReport
rpt.SetDatabaseLogon("username", "password")
 
OK, so I need to create a report object first, then set the SetDatabaseLogon parameters there? Do I then pass the report object to the crystal report viewer?

I'll give it a try.


CraigHartz
 
Hmmm. I can't define an object as crReport. I've added all the Crystal Report references and can't find a Crystal Report object in thr oolbox, so I'm at a loss here.

Can you be more specific? I'm not getting the reference.


CraigHartz
 
crReport is the name of the report included in the project - in my case it was crReport.

Yes you need to add it to the viewer.

Code:
crView.ReportSource = rpt

Where crView is the Crystal Report Viewer.
 
OK Wait -- so you have the report included in the solution, then? I don't have that, and I'm not sure it is an option for me. I've previously been able to just set up the viewer, give it the report filename and database logon parameters, and invoke it to display the report. I'm talking about 25 separate reports for this project and it would be a bit of an issue to add them all into the solution.

There's no way to do this without including the report(s) in the solution?


CraigHartz
 
No - there is a way. I've just had at max 2 reports for the projects I've worked on - and I created them within Visual Studio.

I'm not sure how to add them from a file - I'm sure someone knows that.
 
.NET 1.1:
Code:
Dim cr As New CrystalDecisions.CrystalReports.Engine.ReportDocument

cr.Load(filename)

For Each tbl As CrystalDecisions.CrystalReports.Engine.Table In cr.Database.Tables

    Dim ci As New CrystalDecisions.Shared.ConnectionInfo

    ci.ServerName = "(local)"

    ci.DatabaseName = "Northwind"

    ci.UserID = "sa" 'Leave as empty string for SSPI

    ci.Password = "password" 'Leave as empty string for SSPI

    Dim tlo As New CrystalDecisions.Shared.TableLogOnInfo

    tlo.ConnectionInfo = ci

    tbl.ApplyLogOnInfo(tlo)

Next

crystalReportViewer1.ReportSource = cr
.NET 2.0:
Code:
Dim cr As New CrystalDecisions.CrystalReports.Engine.ReportDocument

cr.Load(filename)

For Each connection As CrystalDecisions.Shared.IConnectionInfo In report.DataSourceConnections
				
For i As Int32 = 0 To report.DataSourceConnections.Count
			
    report.DataSourceConnections(i).SetConnection(servername, databasename, username, password)
	
Next

crystalReportViewer1.ReportSource = cr
 
Sorry. couple of typos on the second (converted from C# in my head!):
Code:
Dim cr As New CrystalDecisions.CrystalReports.Engine.ReportDocument

cr.Load(filename)

For Each connection As CrystalDecisions.Shared.IConnectionInfo In report.DataSourceConnections
                 
    For i As Int32 = 0 To report.DataSourceConnections.Count - 1
            
        report.DataSourceConnections(i).SetConnection(servername, databasename, username, password)
    
    Next

Next

crystalReportViewer1.ReportSource = cr
 
Shelton,

OK I think I'm nearly there -- only thing is where you use the keyword "Report" I get a syntax error stating "Report" is undefined. I tried substituting cr for it but that did not work.

If I use Report what should I define it as?

Thanks


CraigHartz
 
Sorry Craig, I've been having a Friday from hell!!!

Yes, report should have been cr (I've tidied it up a bit for VB):
Code:
Dim cr As New CrystalDecisions.CrystalReports.Engine.ReportDocument

cr.Load(filename)

Dim conn As CrystalDecisions.Shared.ConnectionInfo

For Each conn In cr.DataSourceConnections

    For i As Int32 = 0 To cr.DataSourceConnections.Count - 1

        cr.DataSourceConnections(i).SetConnection(servername, databasename, username, password)

    Next

Next
 
Thanks again, but I'm still getting the following error:

C:\Projects\YardiFasReports\frmReport.vb(90): 'DataSourceConnections' is not a member of 'CrystalDecisions.CrystalReports.Engine.ReportDocument'.


Is it possible I'm missing a reference, imports or inherits statement?




CraigHartz
 
Sorry for the dumb question, but are you using .NET 2.0 (i.e. Visual Studio 2005) ? DataSourceConnections is not implemented in .NET 1.1 (VS2003).
 
Here is my code for setting up logon info for tables in VS2k2 w/ CR XI (I believe it worked with v9 also):
Code:
Dim x As Integer
For x = 0 To cr.Database.Tables.Count - 1
  If cr.Database.Tables.Item(x).LogOnInfo.ConnectionInfo.DatabaseName.ToUpper = "DatabaseName" Then
    Dim tloi As New CrystalDecisions.Shared.TableLogOnInfo()
    tloi.ConnectionInfo = cr.Database.Tables.Item(x).LogOnInfo.ConnectionInfo
    tloi.ConnectionInfo.UserID = "UserName"
    tloi.ConnectionInfo.Password = "Password"
    cr.Database.Tables.Item(x).ApplyLogOnInfo(tloi)
  End If
Next

I check the database name first as we have some reports that pull info from XML files and a database, and you can't really log into an XML file ;)

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Shelton,

I'm running (apparently) VB.NET 2002! I thought I was running 2003 up until a week ago, but apparently that isn't so. I guess that means I'm using framefork 1.1. I guess it isn't possible to have 2002/2003 use 2.0?

I do have a copy of 2005 available to upgrade to but I have not done it yet. Looks like maybe I had better go ahead and do that.

I'm trying to determine for sure which framework version I'm running now, but can't find any reference to it.



CraigHartz
 
Rick,

What is cr defined as? I am guessing it might be:

CrystalDecisions.CrystalReports.Engine.ReportClass

But I really don't know. I know it isn't the Report Viewer control because I tried that.

Sorry for all the hand-holding, I wish I had a good manual or FAQ to refer to but I'm flying blind here. I know my way around Crystal 8 and the old OCX control but all this .NET stuff is very new to me. Thanks.


CraigHartz
 
Hi Craig

VB.NET 2002 uses version 1.0 of the framework. I don't think you can use version 1.1 and you definitely can't use version 2.0 without upgrading to VB.NET 2003 and VB.NET 2005 respectively.

You may find that the .NET 1.1 code I submitted earlier (or indeed Rick's reply) will work with version 1.0, but the version 2.0 code will most definitely not work with your version of VB.

I hope this helps, and sorry if the version 2.0 code I posted was misleading. The problem is that you never know what version the original poster is using!!!
 
Hmmm. OK, I guess that tears it, I have to upgrade. I will get started on that and then revisit this later today (I hope) and see if that fixes it all.

Thanks for all of your help. I'll get back to you assuming everything goes well with the upgrade.


CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top