Imports CrystalDecisions.CrystalReports.Engine
Imports System
Imports System.Web.UI
Imports System.Data

Namespace QW2021C.Resultate
    Public Partial Class Report
        Inherits Page

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
            Dim report As String = Request.QueryString("rpt").ToString()
            Dim ds As DataSet = New DataSet()
            ds.ReadXml(MyBase.Session("Reportdaten").ToString())
            Dim rptfilename As String = Server.MapPath("~\Resultate\Reports\" & report.ToString())
            Dim cr As ReportDocument = New ReportDocument()
            cr.Load(rptfilename)
            Dim i As Integer
            'FormulaFieldDefinitions crFormulaFieldDefinitions;
            'FormulaFieldDefinition crFormulaFieldDefinition;
            'FormulaFieldDefinition=crim
            'Section section;

            'int LoopFormula;
            'string FormulaName;
            'crFormulaFieldDefinitions = cr.DataDefinition.

            'for (LoopFormula = 0; LoopFormula <= crFormulaFieldDefinitions.Count - 1; LoopFormula++)
            '{
            '    crFormulaFieldDefinition = crFormulaFieldDefinitions[LoopFormula];
            '    FormulaName = crFormulaFieldDefinition.Name;
            '    Console.WriteLine(FormulaName);
            '}

            Select Case report
                Case "Vierkampf.rpt"
                    Dim MyTextObject As TextObject
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtheader0"), TextObject)
                    MyTextObject.Text = MyBase.Session("Kopfzeile1").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtheader1"), TextObject)
                    MyTextObject.Text = MyBase.Session("Kopfzeile1").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txttitel"), TextObject)
                    MyTextObject.Text = MyBase.Session("Titel").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtstadion"), TextObject)
                    MyTextObject.Text = MyBase.Session("Stadion").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtorganisator"), TextObject)
                    MyTextObject.Text = MyBase.Session("Organisator").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtdatum"), TextObject)
                    MyTextObject.Text = MyBase.Session("Datum").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtwettkampfnr"), TextObject)
                    MyTextObject.Text = MyBase.Session("Wettkampfnr").ToString()
                    cr.SetDataSource(ds.Tables(1))
                    Me.CrystalReportViewer1.ReportSource = cr
                    Me.CrystalReportViewer1.RefreshReport()
                    Dim ci As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo()
                Case "erdgas.rpt"
                    'CrystalDecisions.CrystalReports.Engine.TextObject MyTextObject;
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtheader0"), TextObject)
                    MyTextObject.Text = MyBase.Session("Kopfzeile1").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtheader1"), TextObject)
                    MyTextObject.Text = MyBase.Session("Kopfzeile1").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txttitel"), TextObject)
                    MyTextObject.Text = MyBase.Session("Titel").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtstadion"), TextObject)
                    MyTextObject.Text = MyBase.Session("Stadion").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtorganisator"), TextObject)
                    MyTextObject.Text = MyBase.Session("Organisator").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtdatum"), TextObject)
                    MyTextObject.Text = MyBase.Session("Datum").ToString()
                    MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("txtwettkampfnr"), TextObject)
                    MyTextObject.Text = MyBase.Session("Wettkampfnr").ToString()
                    cr.SetDataSource(ds.Tables(1))
                    Me.CrystalReportViewer1.ReportSource = cr

                    'CrystalDecisions.Shared.ConnectionInfo ci = new CrystalDecisions.Shared.ConnectionInfo();
                    Me.CrystalReportViewer1.RefreshReport()
                Case Else
                    Dim ortliste = ""

                    For Each dr As DataRow In ds.Tables(0).Rows
                        If Not Equals(ortliste, "") Then ortliste = ortliste & ";"
                        ortliste = ortliste & dr(0).ToString()
                    Next

                    Dim dt As DataTable = New DataTable()

                    For Each c As DataColumn In ds.Tables(1).Columns

                        If Equals(c.ColumnName.ToString(), "Rang") OrElse Equals(c.ColumnName.ToString(), "BestPunkte") Then
                            dt.Columns.Add(c.ColumnName, Type.GetType("System.Int32"))
                        Else
                            dt.Columns.Add(c.ColumnName)
                        End If
                    Next

                    For Each dr As DataRow In ds.Tables(1).Rows
                        dt.ImportRow(dr)
                    Next

                    Dim splitter = ortliste.Split(";"c)

                    'int i;
                    For i = 1 To 9 - 1

                        Try
                            MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("Text" & i.ToString()), TextObject)
                            MyTextObject.Text = ""
                        Catch
                        End Try
                    Next

                    For i = 1 To splitter.Length
                        ' CrystalDecisions.CrystalReports.Engine.TextObject MyTextObject;
                        MyTextObject = TryCast(cr.ReportDefinition.ReportObjects("Text" & i.ToString()), TextObject)
                        MyTextObject.Text = ""

                        Try
                            MyTextObject.Text = splitter(i - 1).ToString()
                        Catch
                        End Try
                    Next

                    cr.SetDataSource(dt)
                    Me.CrystalReportViewer1.ReportSource = cr

                    'CrystalDecisions.Shared.ConnectionInfo ci = new CrystalDecisions.Shared.ConnectionInfo();
                    Me.CrystalReportViewer1.RefreshReport()
            End Select
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If MyBase.Page.IsPostBack = True Then Return
        End Sub
    End Class
End Namespace
