Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+
  • The page you are viewing does not exist in the .NET Standard 2.0+ platform documentation. This link will take you to the parent topic of the current section.
  • The page you are viewing does not exist in the .NET Core 3.0+ platform documentation. This link will take you to the parent topic of the current section.

Get Started - Create a Snap Report

  • 5 minutes to read

The following article describes how to get started with the SnapDocumentServer and create a simple report from scratch.

This tutorial consists of the following sections.

Create an Application

  1. Start Visual Studio and create a new project by selecting FILE | New | Project… in the main menu. In the invoked New Project dialog, select a Console Application, specify the project name and location, and click OK.

    SnapDSGettingStarted_1

  2. In the Solution Explorer, right-click the References node and select Add Reference… in the context menu.

    SnapDSGettingStarted_2

  3. In the invoked Reference Manager dialog, add references to the following libraries.

    • DevExpress.Data.v19.2.dll
    • DevExpress.Docs.v19.2.dll
    • DevExpress.Office.v19.2.Core.dll
    • DevExpress.RichEdit.v19.2.Core.dll
    • DevExpress.Snap.v19.2.Core.dll
  4. Paste the following code in the Main method of the Program.cs file (Main procedure of the Module1.vb file for Visual Basic). The code snippet creates a new SnapDocumentServer instance, associates it with the data source, generates the tabular report and opens it in the default application registered for this file type.

    Dim server As New SnapDocumentServer()
    Dim document As SnapDocument = server.Document
    
    Dim datasource As Object = GetDataSource()
    document.DataSource = datasource
    
    Console.Write("Generating Document... ")
    GenerateLayout(document)
    Console.WriteLine("Ok!")
    Console.Write("Press any key...")
    Console.ReadKey()
    System.Diagnostics.Process.Start("SnapDocumentServerTest.rtf")
    

Connect to Data

  • Open the PROJECT menu and click the Add New Data Source… command.

    How-to-Bind-Report-to-MS-SQL-Server-01

  • In the invoked Data Source Configuration Wizard, select Database and click Next.

    SnapGS_Wizard1

  • Select Dataset to specify the type of a database model and click Next.

    SnapGS_Wizard2

  • On the next page, click New Connection… to specify which data connection should be used.

    SnapGS_Wizard3

  • In the invoked Add Connection dialog, set your data source to Microsoft SQL Server Database Fie(SqlClient) and specify the database file path. This tutorial uses a connection to the sample Northwind database (the NWind.mdf file is included in the product installation).

    SnapGS_Wizard4

    Click OK to apply the changes. In the Data Source Configuration Wizard, click Next.

    SnapGS_Wizard5

  • Click Next on the following page to save the newly created connection string to the configuration file.

    SnapGS_Wizard6

  • The final wizard page allows you to choose which tables to obtain from the database. After this step, click Finish.

    SnapGS_Wizard7

  • Create a new DataSet object and populate it with data from the database by adding the following code.

    Private Shared Function GetDataSource() As Object
        Dim dataSource As New NWindDataSet()
        Dim connection = New SqlConnection()
        connection.ConnectionString = My.Settings.Default.NWindConnectionString
    
        Dim products As New ProductsTableAdapter()
        products.Connection = connection
        products.Fill(dataSource.Products)
    
        Return dataSource.Products
    End Function
    

Generate a Report Layout

  • Use the code below to generate a simple template and fill it with data. The code snippet created a snap list with three data entries and a header, and exports the result to the SnapDocumentServerTest.rtf file.

    Private Shared Sub GenerateLayout(ByVal document As SnapDocument)
        ' Add a Snap list to the document.
        Dim list As SnapList = document.CreateSnList(document.Range.End, "List")
        list.BeginUpdate()
        list.EditorRowLimit = 100500
    
        ' Add a header to the Snap list.                                                                   
        Dim listHeader As SnapDocument = list.ListHeader
        Dim listHeaderTable As Table = listHeader.Tables.Create(listHeader.Range.End, 1, 3)
        Dim listHeaderCells As TableCellCollection = listHeaderTable.FirstRow.Cells
        listHeader.InsertText(listHeaderCells(0).ContentRange.End, "Product Name")
        listHeader.InsertText(listHeaderCells(1).ContentRange.End, "Units in Stock")
        listHeader.InsertText(listHeaderCells(2).ContentRange.End, "Unit Price")
    
        'Create the row template and fill it with data
        Dim listRow As SnapDocument = list.RowTemplate
        Dim listRowTable As Table = listRow.Tables.Create(listRow.Range.End, 1, 3)
        Dim listRowCells As TableCellCollection = listRowTable.FirstRow.Cells
        listRow.CreateSnText(listRowCells(0).ContentRange.End, "ProductName")
        listRow.CreateSnText(listRowCells(1).ContentRange.End, "UnitsInStock")
        listRow.CreateSnText(listRowCells(2).ContentRange.End, "UnitPrice \$ $0.00")
    
        list.EndUpdate()
        list.Field.Update()
        document.ExportDocument("SnapDocumentServerTest.rtf", DocumentFormat.Rtf)
    End Sub
    

    Tip

    This example demonstrates how to generate the template in code. You can also use a document template created in the DevExpress Snap at runtime. To learn how to generate a template using the Snap UI, check the Snap List and Document Template topic.

The following image shows the report generated after executing the code above (the document is opened in Microsoft® Word®).

SnapGS_Result

Note

This tutorial concentrates on a very limited set of actions that can be performed with Snap. For more information on the Snap reporting functionality, see the tutorials in the Examples section.

See Also