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’.

TFS Build: Referenced assembly ‘…dll’ targets a different processor than the application

 

For both the application and the referenced library check that the following match

Project > Properties > Compile > Advanced Compile Options > Target CPU ( possibly also the Target Framework )

If difficult to find use the Build explorer > View Log > Next Warning to work out the application project

TFS Build: Could not resolve this reference. Could not locate the assembly ”. Check to make sure the assembly exists on disk.

A TFS Build fails probably after a new reference has been added. Possibly a .exe reference

ERROR
TFS Build: Could not resolve this reference. Could not locate the assembly ‘<name>’. Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors

SOLUTION
In Team Explorer > Builds > Build Explorer > Select broken build and double-click to work out which project did not build
Visual Studio > Projects > Project Dependencies > Select project that is not building > Add the required dependency > Confirm that the Build Order tab now shows this project higher than the project failing to build

Still did not work. Removed and added the reference again and it then worked.
The change in Source Control for the file was that the version no was in the broken version. not in the fixed version. Before here:

After here:

 

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/

Visual Studio Macros

DTE – meaning got lost in time. Application object – Development Tools Environment.

Use record macro, inspect and learn

ProjectName = DTE.ActiveDocument.ProjectItem.ContainingProject.Name
DTE.ActiveDocument.Selection.Text = “Hello World”

Visual Studio Macros “Cannot load the recording macro…”

Error
“Cannot load the recording macro project ” for the following reason:
‘C:\Users\<username>\Documents\Visual Studio 2010\Projects\VSMacros80\MyMacros\MyMacros.vsmacros’ is an invalid or inaccessible macro project file
Would you like to recreate it?”
Yes or No

Solved Answer No.
The problem may well be that you are not using C, maybe using D or another drive.

If looking in the wrong place then set the macro recording project to the other project then unload the current project. If necessary move any new created macro project to where you want it then open it. Repeat for samples.

DHCP reservation on Server 2003

 

Start > Administrative Tools > DHCP

If the server is not connected then DHCP > Right-click > Add Server

Server name > Scope > Reservations > Add need MAC address from PC that you want to fix IP. To get this do an ipconfig /all on that pc

 

Windows Forms DataBinding displays ‘System.Data.DataRowView’ in text boxes and other controls

ERROR

Windows Forms DataBinding displays ‘System.Data.DataRowView’ in text boxes and other controls

SOLVED

Was a ComboBox databinding pointing at a field no longer in the dataset.

Entity Framework EntityState context.Entry().State

Entity Framework EntityState

context.Entry(<object>).State

http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry(v=vs.103).aspx