Is there a way to check whether a specified report / page is already open?
If a report is created, then called again from the same page. The open window is activated but the data is not updated.
Any suggestions?
Cheers
Andrea
Dear Andrea,
Thanks for your consideration.
Could you send me your sample code and/or designer file? So I can investigate your question.
Code is as follows:
Imports System.Data.SqlClient
Imports Aspose.Excel
Public Class rptLocations
Inherits System.Web.UI.Page
Protected WithEvents selEntity As System.Web.UI.WebControls.DropDownList
Protected WithEvents lblNoData As System.Web.UI.WebControls.Label
Protected WithEvents cmdProduceReport As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
#Region " Page Initialisation"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then PopulateEntity()
End Sub
Private Sub PopulateEntity()
'Populates Entity Combo with selected value from Initial Combo
Dim cn As New SqlConnection(GlobalConstants.App_ConnectionString)
Dim ds As New DataSet()
Dim com As New SqlCommand(“ENT_EntityForLocationType_sp”, cn)
Try
cn.Open()
ds = CUtilityBelt.ExecuteStoredProcedureDataset(com)
cn = Nothing
CUtilityBelt.BindList(selEntity, ds)
CUtilityBelt.selDefaultValue(selEntity, “Select an Entity”)
selEntity.DataBind()
Catch ex As SqlException
CErrorHandler.RaiseError(ex, “PopulateEntity”)
Catch ex As Exception
CErrorHandler.RaiseError(ex, “PopulateEntity”)
Finally
ds = Nothing
cn = Nothing
com.Dispose()
End Try
End Sub
#End Region
#Region " Control Events"
Private Sub cmdProduceReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProduceReport.Click
ProduceReport()
End Sub
#End Region
#Region " Report Production"
Private Sub ProduceReport()
Dim cn As New SqlConnection(GlobalConstants.App_ConnectionString)
Dim com As New SqlCommand(“rpt_Relationships_sp”, cn)
Dim xls As New Excel(GlobalConstants.APP_AsposeLicenceFile, Me) 'Creates an Excel Report
Dim ds As New DataSet()
Dim sFile As String
Dim xlsTemplate As String
Dim rptHeading As String
Dim sheet As Worksheet
Try
'SQL sp / ds population
com.Parameters.Add("@nEntID", SqlDbType.Int).Value = CInt(selEntity.SelectedItem.Value)
ds = CUtilityBelt.ExecuteStoredProcedureDataset(com)
If ds.Tables(0).Rows.Count = 0 Then
xls = Nothing
lblNoData.Visible = True
Exit Sub
Else
'Specify location of excel file which report will be based on
sFile = “Locations.xls”
xlsTemplate = Server.MapPath(HttpContext.Current.Request.ApplicationPath & “/Reports/” & sFile)
'Report Header String to appear in first cell(“A1”)
rptHeading = "Relationships Report for Entity: " & selEntity.SelectedItem.Text
'Open specified excel file
xls.Open(xlsTemplate)
sheet = xls.Worksheets(0)
'Set value of A1
sheet.Cells(0, 0).PutValue(rptHeading)
'Set value of A2
sheet.Cells(1, 0).PutValue(CUtilityBelt.CreatedBy)
'Set value of A3
sheet.Cells(2, 0).PutValue(CUtilityBelt.DateCreated)
'Transfers ds datatable starting from A6 (zero-index row 5, zero-index column 0)
sheet.Cells.ImportDataTable(ds.Tables(0), False, 5, 0)
'Name of the worksheet
sheet.Name = “Locations”
Dim openFName As String
'Opens report in browser
xls.Save(sFile, SaveType.OpenInBrowser, FileFormatType.Default, Me.Response)
End If
Catch ex As SqlException
CErrorHandler.RaiseError(ex, “ProduceReport”)
Catch ex As Exception
CErrorHandler.RaiseError(ex, “ProduceReport”)
Finally
ds = Nothing
cn = Nothing
com.Dispose()
sheet = Nothing
xls = Nothing
End Try
End Sub
#End Region
End Class
Dear Andrea,
Your code is perfect. This problem is caused by IE’s routine.
My suggestion is:
If you want to open the result excel file in browser, do not create it in event other than Page_Load event.
You can chage your code to:
1. Create a new aspx page named ShowReport.aspx
2. Change the code to:
#Region " Control Events"
Private Sub cmdProduceReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProduceReport.Click
Me.Response.Redirect(“ShowReport.aspx”)
End Sub
#End Region
3. Move ProduceReport() method to the Page_Load event of ShowReport.aspx
If you want to show report in current page:rptLocations.aspx, you can do the following:
#Region " Page Initialisation"
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then PopulateEntity()
Dim param as String = Me.Request.Params(“Flag”)
If param = “showreport” Then ProduceReport()
End Sub
#Region " Control Events"
Private Sub cmdProduceReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProduceReport.Click
Me.Response.Redirect(“rptLocation.aspx?Flag=showreport”)
End Sub
#End Region
Thanks for the suggestion – I will try it and get back to you if it doesn’t work.