Code Snippet
Private Sub ListaProdutos()
Dim crReportDocument As New ReportDocument
Dim strConexao As String = ConnectionStrings("AWorks").ConnectionString()
Dim oConn As New SqlConnection
Dim strSQL As New StringBuilder
Dim oTable As New DataTable()
Try
'1 passo: carregar o relatório
Dim strPathreport As String = Server.MapPath("~\CrystalReports\rptProdutos.rpt")
crReportDocument.Load(strPathreport)
'configurando para impressão em Landscape
'crReportDocument.PrintOptions.PaperOrientation = PaperOrientation.Landscape
'2 passo: passar os dados para o relatório (datatable)
'abrindo conexão com o banco de dados
oConn.ConnectionString = strConexao
oConn.Open()
'carregando os dados
strSQL.Append(
" SELECT ")
strSQL.Append(
" Production.Product.ProductID, ")
strSQL.Append(
" Production.Product.Name, ")
strSQL.Append(
" Production.Product.Color, ")
strSQL.Append(
" Production.Product.ListPrice, ")
strSQL.Append(
" Production.ProductSubcategory.Name AS NameSubcategory ")
strSQL.Append(
" FROM ")
strSQL.Append(
" Production.Product INNER JOIN Production.ProductSubcategory ")
strSQL.Append(
" ON Production.Product.ProductSubcategoryID = Production.ProductSubcategory.ProductSubcategoryID ")
Dim oDA As New SqlDataAdapter(strSQL.ToString, oConn)
oDA.Fill(oTable)
'definindo o DataSource do relatório
crReportDocument.SetDataSource(oTable)
'visualizando o relatório
crPrintPreview.DisplayGroupTree =
False
crPrintPreview.HasCrystalLogo =
False
crPrintPreview.ReportSource = crReportDocument
Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
Finally
'liberando os objetos
oConn.Dispose()
oTable.Dispose()
oConn =
Nothing
oTable =
Nothing
End Try
End Sub