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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CR.net and VB.net 2005 1

Status
Not open for further replies.

bburnell

Programmer
Sep 15, 2000
560
US
Hi all,

Anyone know where I can find good sample code for:

A) Showing a Crystal Report in asp.net via vb.net

B) Passing a parameter to said Crystal Report (programatically)

C) Exporting said Crystal Report to pdf programatically.

All of the examples I have or have seen do not seem to work in 2005 or have obsolete methods in 2005.

Any help would be greatly appreciated!
~Brett
 
Ok. I got it. B is done with the following:
crReportDocument.SetParameterValue(<index#>, <value>)

~Brett
 
If you don't mind the code in VB.NET, then I can help.

It's not so bad, but there are a few things you have to watch out for.

Throw a CR Viewer on a page.
Code:
      <CR:CrystalReportViewer ID="CReportViewer" runat="server" AutoDataBind="true"
        Style="z-index: 18; position: absolute; left: 0px; top: 0px" DisplayGroupTree="False" />


This class behind takes care of logging in, setting the params and displaying the report.

Code:
Partial Class ViewReport
  Inherits System.Web.UI.Page


  'Crystal report configuration code needs to be called earlier, during the Page.Init event.
  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    ConfigureCrystalReports()
  End Sub

  'The ConfigureCrystalReports() method enables users to interact with the report at runtime. 
  ' It also controls programmatic interaction with the report. 
  Private Sub ConfigureCrystalReports()
    Dim CRReport As ReportDocument

    CRReport = New ReportDocument
    Dim reportPath As String = Server.MapPath("Reports/ProductNutritional.rpt")

    'Loads a new report. If a report is already loaded, then it is closed and a new one is opened. 
    CRReport.Load(reportPath)

    'Don't allow the user to enter the login information
    CReportViewer.EnableDatabaseLogonPrompt = False

    'Set up an object to use the connecton info
    Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()

    Dim sName As String = System.Configuration.ConfigurationManager.ConnectionStrings("ItemMasterConnectionString").Name

    'Set the DatabaseName, UserID, and Password properties of the ConnectionInfo instance.
    myConnectionInfo.ServerName = "myserver"
    myConnectionInfo.DatabaseName = "ItemMaster"
    myConnectionInfo.UserID = "me"
    myConnectionInfo.Password = "mypassword"

    'Enter a call to the SetDBLogonForReport() method, by passing in the ConnectionInfo instance and the NorthwindCustomers report.
    SetDBLogonForReport(myConnectionInfo, CRReport)

    CRReport.SetParameterValue("parmItemNumber", "72012345")

    CReportViewer.ReportSource = CRReport

  End Sub



  Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
    '**************************************************************************
    'Copied from: [URL unfurl="true"]http://msdn2.microsoft.com/en-us/library/ms227783(VS.80).aspx[/URL]
    '**************************************************************************

    'Tables is an indexed class that contains instances of the Table class.
    Dim myTables As Tables = myReportDocument.Database.Tables

    'Create a foreach loop that loops through each Table instance in the Tables indexed class instance.
    For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables

      'Within the foreach loop, retrieve the TableLogonInfo instance from the LogOnInfo property of the Table instance.
      Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo

      'Within the foreach loop, set the ConnectionInfo property of TableLogonInfo to the ConnectionInfo parameter.
      myTableLogonInfo.ConnectionInfo = myConnectionInfo

      'Within the foreach loop, pass the TableLogonInfo instance as a parameter to the ApplyLogonInfo method of the Table instance.
      myTable.ApplyLogOnInfo(myTableLogonInfo)

    Next

  End Sub

End Class

If you need more help, I just learned all this the last few weeks so it's fresh. Please ask.



If anyone can tell me how to get the myConnectionInfo info out of my config file then I'll be grateful.
I don't mean as separate ones, I mean using the connectionStrings one that has one long connection string.
 
If anyone can tell me how to get the myConnectionInfo info out of my config file then I'll be grateful.

Do you mean like this?
ServerName = configurationAppSettings.GetValue("ServerName")
UserID = configurationAppSettings.GetValue("UserID")
Password = configurationAppSettings.GetValue("Password")

For a connection string, you just put the whole DSN in a variable name in your web.config file and call it like any other variable name.

SavedDSN = configurationAppSettings.GetValue("YourDSN")

~Brett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top