How do I … directly printing a project report.rdlc in VB.Net without previewing first

Also see next blog entry

Steps

  1. Imports System
    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports System.Windows.Forms
    Imports Microsoft.Reporting.WinForms
  2. Load data as usual
  3. Dim report as New LocalReport
    report.ReportPath = “<ReportName>.rdlc”
    or
    report.ReportPath = “Reports\<ReportName>.rdlc
    report.DataSources.Add(New ReportDataSource(“dtRouteCardWO”, CType(Me.DsRouteCard1.RouteCardWO, DataTable)))
    Export(report)
    Print()
  4.     Private Sub Export(ByVal report As LocalReport)
            ‘MsgBox(“Start of Export”)   ‘Reaches this line on EBC Rise TS
            ‘then before “End of Export” get “Error occurred during local report processing”        Dim deviceInfo = “<DeviceInfo>” & _
                “<OutputFormat>EMF</OutputFormat>” & _
                “<PageWidth>8.3in</PageWidth>” & _
                “<PageHeight>11.7in</PageHeight>” & _
                “<MarginTop>0.25in</MarginTop>” & _
                “<MarginLeft>0.25in</MarginLeft>” & _
                “<MarginRight>0.25in</MarginRight>” & _
                “<MarginBottom>0.25in</MarginBottom>” & _
                “</DeviceInfo>”
            Dim warnings As Warning() = Nothing
            m_streams = New List(Of Stream)()

            ‘See my blog. For this to work the Build Action must be changed from Embedded Resource to Content and a Copy policy chosen
            ‘For this to work from a different project. Add this file as a link in that project and then do above on linked file.
            report.Render(“Image”, deviceInfo, AddressOf CreateStream, warnings)
            For Each stream As Stream In m_streams
                stream.Position = 0
            Next

            ‘MsgBox(“End of Export”)
        End Sub

  5.     Private Function CreateStream(ByVal name As String, ByVal fileNameExtension As String, ByVal encoding As System.Text.Encoding, ByVal mimeType As String, ByVal willSeek As Boolean) As Stream
            Dim stream As Stream = New MemoryStream()
            m_streams.Add(stream)
            Return stream
        End Function
  6.     Private m_currentPageIndex As Integer
        Private m_streams As IList(Of Stream)
  7.     ‘ Handler for PrintPageEvents
        Private Sub PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            Dim pageImage As New Metafile(m_streams(m_currentPageIndex))        ‘ Adjust rectangular area with printer margins.
            Dim adjustedRect As New Rectangle(ev.PageBounds.Left – CInt(ev.PageSettings.HardMarginX), _
                                              ev.PageBounds.Top – CInt(ev.PageSettings.HardMarginY), _
                                              ev.PageBounds.Width, _
                                              ev.PageBounds.Height)

            ‘ Draw a white background for the report
            ev.Graphics.FillRectangle(Brushes.White, adjustedRect)

            ‘ Draw the report content
            ev.Graphics.DrawImage(pageImage, adjustedRect)

            ‘ Prepare for the next page. Make sure we haven’t hit the end.
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
            ev.HasMorePages = False ‘#1074 srs fudge. Routecard was on 2 pages, should always be 1
        End Sub

        Private Sub Print()
            ‘MsgBox(“Start of Print”)
            If m_streams Is Nothing OrElse m_streams.Count = 0 Then
                Throw New Exception(“Error: no stream to print.”)
            End If
            Dim printDoc As New PrintDocument()
            If Not printDoc.PrinterSettings.IsValid Then
                Throw New Exception(“Error: cannot find the default printer.”)
            Else
                AddHandler printDoc.PrintPage, AddressOf PrintPage
                m_currentPageIndex = 0
                printDoc.Print()
            End If
            ‘MsgBox(“End of Print”)
        End Sub

  8.     Public Shadows Sub Dispose() Implements IDisposable.Dispose
            If m_streams IsNot Nothing Then
                For Each stream As Stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub

Troubleshooting

  1. An error occurred during local report processing copy
    The report definition for report ‘..\some_path_to_report\<reportname>.rdlc’ has not been specified
    Could not find file ‘C:\inetpub\wwwroot\some_path_to_report\tse.rdlc’.
  2. Could not find file ‘C:\inetpub\wwwroot\some_path_to_report\tse.rdlc’.

Using SSIS to import data from SQL Server on premises to SQL Azure

This assumes prior knowledge of SSIS, but just hi-lights the difference

  1. SSIS
  2. Data Flow Task
  3. OLE DB Source
    1. As normal
  4. ADO Net Destination
      1. .Net Providers\SqlClient Data Provider
      2. Login

SQL Azure differences

Keyword or statement option ‘pad_index’ is not supported in this version of SQL Server. Also list below

  • pad_index
  • allow_row_locks
  • allow_page_locks

”Filegroup reference and partitioning scheme’ is not supported in this version of SQL Server.

Not yet used it, but PluralSight video pointed that an easier way to script this is to use the SQL Azure Migration Wizard http://sqlazuremw.codeplex.com/

VB.Net or SSRS code for Ordinal Numbers

Easily found on web. See example in SRS.ReportingServices.Functions and also in my invoice report.
With acknowledgement to:
http://www.jigsawboys.com/2010/03/04/ordinal-number-in-reporting-services/

Public Function FormatOrdinal(day as Integer) As String
  SELECT CASE (day Mod 100)
    CASE 11,12
    CASE 13
      Return day.ToString() + “th”
  END SELECT

  SELECT CASE day Mod 10
    Case 1
      Return day.ToString() + “st”
    Case 2
      Return day.ToString() + “nd”
    Case 3
      Return day.ToString() + “rd”
    Case Else
      Return day.ToString() + “th”
  END SELECT
End Function

Good luck!

SSRS Adding Code and also custom assemblies to reports

How to add Code to SSRS reports

  1. Right click on report > Code > Type code as Visual Basic function
  2. In expressions within a data region use Code.<functionname> e.g. =Code.OrdinalFunction(4)

Code for Ordinal Number readily avialable on web. Example use in SRS.ReportingServices.Functions and also in my invoice report

How to add custom assemblies to SSRS reports

  1. Create a class library project in Visual Studio VB.Net or C#
  2. Use a meaningful assembly name e.g. <initials>.ReportingServices.Functions
  3. Add a class and methods to this class. Make the methods shared
  4. Compile to create a dll
  5. Copy the dll to the equivalent location of:
    C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies
    You may not need the (x86) and you may be on a lower or higher version than 9.0
  6. In VS BIDS report right click > report properties > References > Add the reference
  7. In expressions in a report use full Namespace.ClassName.FunctionName(variables)

Next deploying the assembly to the report server

  1. Copy your DLL to the following location or equivalent:
    C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin

Good luck. Acknowledgments to:

http://www.devx.com/codemag/Article/33656/0/page/3

Some SQL Server 2005 services are not listed in SQL Server Configuration Manager – Also Red Cross shown in IIS – Server 2003

PROBLEM

  1. Some SQL Server 2005 services are not listed in SQL Server Configuration Manager
  2. Red Cross shown in IIS – Server 2003

SOLVED QUICK

BITS – Background Intelligent Transfer Service may not be registered and may have a startup type of Manual and be Stopped.

regsvr32.exe qmgr.dll
regsvr32.exe qmgrprxy.dll

Then reboot.

SOLVED DETAIL

by external support company. Asked them how they did it. Answer copied here:

It looks like a windows update was responsible for the failure, Steve correctly spotted the fundamental issue which was that the default web site was stopped in IIS.  By attempting to start IIS, this registered an event in the windows logs and I could diagnose the issue from there.

The earlier versions of SQL reporting prior to 2008 relied on IIS to operate and the failure of IIS to start resulted in the missing items in SQL.

Windows update installed a hot fix which altered a file responsible for BITS (the service that enables Windows Update to function), the update applied an updated version of a file for IIS and the file failed to register when it was installed.  IIS was effectively looking for a file which wasn’t present and stopped itself.

By re-registering the file and rebooting, IIS was able to recognise the updated file and the default web site started successfully.  As the default website hosts information referred to by SQL, this caused SQL reporting to re-appear.

An odd one, but then Microsoft updates are probably responsible for a lot of strange behaviour!

Incidentally, while I was on the server, I spotted a string of Windows Desktop Search errors in the log – Windows Desktop Search was given as an update by Microsoft and it is my personal view that the product is poorly designed and disables perfectly good search facilities included within Windows, it looks like it has been clogging your event log with errors for some time now and with both of these things in mind,  I have disabled the Windows Desktop Search.

Anyway, glad SQL & IIS are now working, please let me know if you have any further issues.

SSRS open url in a new window

With thanks to:

http://www.bidn.com/blogs/BrianKnight/ssis/972/ssrs-action-to-open-a-url-in-a-new-window

=”javascript:void(window.open(‘”+ Fields!ReferURL.Value + “‘,’_blank’))”

 

SSRS Data Cache in Development

After some effort I found that data in SSRS Development environment was being cached in a files called <ReportName>.rdl.data
So you may be expecting data to change and finding that it is not. To get around this you can do one of the following:

The following error may be due to this cache of data
[rsInvalidExpressionDataType] The Value expression used in textrun ‘InvoiceTitle.Paragraphs[0].TextRuns[0]’ returned a data type that is not valid.

Good luck!

SQL Server Compact Edition and Visual Studio

Motivation

To distribute applications to friends and family

Limitations

  1. No Schemas
  2. No Views
  3. No Stored Procedures
  4. Data types missing include:
    1. nvarchar(max)

Issues

  1. Difficult to switch a dataset from SQL to SQL compact at runtime. Considering having to projects with the datasets one SQL and one SQL Compact. Then having an IObjectData and implementations of both
  2. SQL Server Compact 4.0 is not supported by SSMS SQL Server Management Studio
  3. SQL Server Compact 4.0 is not supported by datasets

Because of limitations of 2 and 3, it may be best to stick with 3.5 SP2 for now. 4.0 seems more linked to Web projects for the minute.

If using 4.0 or in anycase it is possible to use Visual Studio > Views > Server Explorer or also Visual Studio > Data drop down menu item

Tools

Visual Studio Tools for SQL Server Compact 4.0. Unable to find microsoft site but link from here works.
Until this tooling is installed the pre-requisites offers only 3.5 SP2

http://erikej.blogspot.com/2010/12/visual-studio-tools-for-sql-server.html

Error Messages

“Format of the initialization string does not conform to specification starting at index 0” Means that the connection string format is not correct. I had missed out the “Data Source=”

VB.Net programming of SQL Server Replication

The article http://msdn.microsoft.com/en-us/library/ms146869.aspx suggests that Microsoft.SQLServer.Replication.dll is in the .Net references. For me it was not, it could however be found at:
C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies

Also needed:

  1. Microsoft.SQLServer.Rmo
  2. Microsoft.SQLServer.Replication
  3. Microsoft.SQLServer.Replication.BusinessLogicSupport
  4. Microsoft.SQLServer.ConnectionInfo