Imports System.Data
Imports System.IO
Imports System.Data.SqlClient


Public Class cSqlDb
    '    Public Shared Function GetNewOpenConnectionADMIN() As SqlConnection
    '        Dim cn As New SqlConnection()
    '        cn.ConnectionString = "Data Source=SQLGUIDE01.verag.ost.dmn\SQLEXPRESS;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;" 'BUCHHALTUNG
    '        cn.Open()
    '        Return cn
    '    End Function
    '    Public Shared Function GetNewOpenConnectionFMZOLL() As SqlConnection
    '        Dim cn As New SqlConnection()
    '        cn.ConnectionString = "Data Source=192.168.0.91\SQLFMZOLL;Initial Catalog=VERAG;Integrated Security=false;User ID=sa;Password=BmWr501956;" ' FMZOLL
    '        'cn.ConnectionString = "Data Source=192.168.0.50\DEVSQL;Initial Catalog=VERAG;Integrated Security=false;User ID=sa;Password=BmWr501956;" 'DEVELOPER
    '        cn.Open()
    '        Return cn
    '    End Function
    '    Public Shared Function GetNewOpenConnectionZOLARIS() As SqlConnection
    '        Dim cn As New SqlConnection()
    '        cn.ConnectionString = "Data Source=SQLGUIDE01.verag.ost.dmn\SQLEXPRESS;Initial Catalog=ezoll;Integrated Security=false;User ID=sa;Password=BmWr501956;" 'BUCHHALTUNG
    '        cn.Open()
    '        Return cn
    '    End Function

    '    Public Shared Function GetNewOpenConnectionAtlasSUB() As SqlConnection
    '        Dim cn As New SqlConnection()
    '        cn.ConnectionString = "Data Source=192.168.0.95\SQLATLAS;Initial Catalog=atlas;Integrated Security=false;User ID=sa;Password=sa;" 'VERAG-ATLAS1
    '        cn.Open()
    '        Return cn
    '    End Function
    '    Public Shared Function GetNewOpenConnectionAtlasSBG() As SqlConnection
    '        Dim cn As New SqlConnection()
    '        cn.ConnectionString = "Data Source=192.168.133.98;Initial Catalog=atlas;Integrated Security=false;User ID=sa;Password=????;" 'verag-atlas3
    '        cn.Open()
    '        Return cn
    '    End Function
End Class


Public Class cLogin

    Public USERBER As Integer
    Public USRNAME As String
    Public USRID As String


    Public Function getAktuelleVersion() As String
        Dim sql As String = "SELECT prog_aktuelleVersion FROM tblProgramme WHERE prog_codename=@codename"
        Dim dr As SqlDataReader
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@codename", "MONITORING")
                dr = cmd.ExecuteReader()
                Try
                    If dr.Read Then Return CStr(dr.Item("prog_aktuelleVersion"))
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return "0"
    End Function



End Class


Public Class cBrgDb


    Public Function loadDgvBySqlZOLARIS(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal kto As String, Optional where As String = "") As DataTable
        Dim sql As String =
         " SELECT * FROM  tblBrgAufschubDataAT WHERE  Abgabenkonto=@kto " &
         "	AND Datum BETWEEN @datumVon AND @datumBis "

        '"   SELECT zsAnmGdsItem.VorgangID AS VorgangID, zsAnmGdsItem.AnmID AS AnmeldungsID, ztIMsgGdsItemDutyCalc.Ty AS Abgabenart,  " &
        '" 	  sum(isnull(ztIMsgGdsItemDutyCalc.Base,0)) AS Berechnungsgrundlage, sum(isnull(ztIMsgGdsItemDutyCalc.Amnt,0)) AS Betrag,  " &
        '" 	  zsAnmRefs.CRN AS CRN, zsAnmRefs.LRN AS LRN, zsAnmHea.DefPayRef AS Abgabenkonto,zsAnmHea.DecDT, vwAnm_First_Rechnung.DRef AS RechnungsNr, zsAnmDHFRefs.InfoText," &
        '"       zsAnmDHFRefs.AnschreibeNr as Anschreibenummer ,zsAnmGdsItem.OperatorID as OperatorId" &
        '"         FROM zzAktivitaet" &
        '" 	INNER JOIN ztIMsgGdsItem ON zzAktivitaet.LizenzNr = ztIMsgGdsItem.LizenzNr AND zzAktivitaet.OperatorID = ztIMsgGdsItem.OperatorID " &
        '" 		AND zzAktivitaet.IMsgID = ztIMsgGdsItem.IMsgID " &
        '" 	INNER JOIN ztIMsgGdsItemDutyCalc ON ztIMsgGdsItem.LizenzNr = ztIMsgGdsItemDutyCalc.LizenzNr " &
        '" 		AND ztIMsgGdsItem.OperatorID = ztIMsgGdsItemDutyCalc.OperatorID AND ztIMsgGdsItem.IMsgID = ztIMsgGdsItemDutyCalc.IMsgID " &
        '" 		AND ztIMsgGdsItem.GdsItemID = ztIMsgGdsItemDutyCalc.GdsItemID " &
        '" 	INNER JOIN zsAnmGdsItem ON zzAktivitaet.LizenzNr = zsAnmGdsItem.LizenzNr AND zzAktivitaet.OperatorID = zsAnmGdsItem.OperatorID " &
        '" 		AND zzAktivitaet.VorgangID = zsAnmGdsItem.VorgangID AND zzAktivitaet.AnmID = zsAnmGdsItem.AnmID " &
        '" 		AND ztIMsgGdsItem.ItNr = zsAnmGdsItem.ItNr " &
        '" 	INNER JOIN zsAnmGdsItemDHF ON zsAnmGdsItem.LizenzNr = zsAnmGdsItemDHF.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmGdsItemDHF.OperatorID " &
        '" 		AND zsAnmGdsItem.VorgangID = zsAnmGdsItemDHF.VorgangID AND zsAnmGdsItem.AnmID = zsAnmGdsItemDHF.AnmID " &
        '" 		AND zsAnmGdsItem.GdsItemID = zsAnmGdsItemDHF.GdsItemID " &
        '" 	INNER JOIN zsAnmHea ON zsAnmGdsItem.LizenzNr = zsAnmHea.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmHea.OperatorID " &
        '" 		AND zsAnmGdsItem.VorgangID = zsAnmHea.VorgangID AND zsAnmGdsItem.AnmID = zsAnmHea.AnmID " &
        '" 	INNER JOIN zsAnmRefs ON zsAnmGdsItem.LizenzNr = zsAnmRefs.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmRefs.OperatorID " &
        '" 		AND zsAnmGdsItem.VorgangID = zsAnmRefs.VorgangID AND zsAnmGdsItem.AnmID = zsAnmRefs.AnmID " &
        '" 	INNER JOIN zsAnmDHFRefs ON zsAnmRefs.LizenzNr = zsAnmDHFRefs.LizenzNr AND zsAnmRefs.OperatorID = zsAnmDHFRefs.OperatorID " &
        '" 		AND zsAnmRefs.VorgangID = zsAnmDHFRefs.VorgangID AND zsAnmRefs.AnmID = zsAnmDHFRefs.AnmID " &
        '" 	LEFT OUTER JOIN vwAnm_First_Rechnung ON zsAnmGdsItem.LizenzNr = vwAnm_First_Rechnung.LizenzNr " &
        '" 		AND zsAnmGdsItem.OperatorID = vwAnm_First_Rechnung.OperatorID AND zsAnmGdsItem.VorgangID = vwAnm_First_Rechnung.VorgangID " &
        '" 		AND zsAnmGdsItem.AnmID = vwAnm_First_Rechnung.AnmID And zsAnmGdsItem.GdsItemID = vwAnm_First_Rechnung.GdsItemID " &
        '" WHERE (zsAnmDHFRefs.ErledigungsTypID LIKE 'F%') AND (zzAktivitaet.MsgTyp = 'EZ922') " &
        '" 	AND (NOT EXISTS (SELECT * FROM zzAktivitaet b WHERE b.MsgTyp = 'EZ922' AND zzAktivitaet.LizenzNr = b.LizenzNr " &
        '" 		AND zzAktivitaet.OperatorID = b.OperatorID AND zzAktivitaet.VorgangID = b.VorgangID AND zzAktivitaet.AnmID = b.AnmID " &
        '" 		AND zzAktivitaet.AktivitaetID > b.AktivitaetID)) " &
        '" 	AND (NOT (ztIMsgGdsItemDutyCalc.Amnt IS NULL)) " &
        '" 	AND (ztIMsgGdsItemDutyCalc.Ty like 'A%' OR ztIMsgGdsItemDutyCalc.Ty IN ('2VF', '2PE', '2KO', '2IG')) " &
        '" 	AND (zsAnmGdsItem.LizenzNr =  'S01122' ) " &
        '" 	AND (zsAnmHea.DecDT >=  convert(datetime, @datumVon,104)) " &
        '" 	AND (zsAnmHea.DecDT <  convert(datetime, @datumBis,104)) " &
        '" 	AND (zsAnmHea.DefPayRef IN (@kto)) " &
        '" GROUP BY ztIMsgGdsItem.IMsgID, zsAnmGdsItem.LizenzNr, zsAnmGdsItem.OperatorID, zsAnmGdsItem.VorgangID, zsAnmGdsItem.AnmID, " &
        '" 	 ztIMsgGdsItemDutyCalc.Ty, zsAnmRefs.CRN, " &
        '" 	zsAnmRefs.LRN, isnull(zsAnmRefs.FRN,''), zsAnmHea.DefPayRef,  " &
        '"         zsAnmHea.DecDT, vwAnm_First_Rechnung.DRef, zsAnmDHFRefs.ErledigungsTypID, zsAnmDHFRefs.InfoText, zzAktivitaet.AnmID, " &
        '"         zsAnmDHFRefs.AnschreibeNr " &
        '" HAVING sum(isnull(ztIMsgGdsItemDutyCalc.Amnt,0)) >0 " &
        '" ORDER BY zsAnmGdsItem.LizenzNr, zsAnmGdsItem.OperatorID, zsAnmGdsItem.VorgangID, zsAnmHea.DefPayRef, " &
        '"         zzAktivitaet.AnmID, ztIMsgGdsItemDutyCalc.Ty "


        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand(sql & where, conn)
                        cmd.Parameters.AddWithValue("@datumVon", datumVon)
                        cmd.Parameters.AddWithValue("@datumBis", datumBis)
                        cmd.Parameters.AddWithValue("@kto", kto)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function

    Public Function loadDgv(sql As String) As DataTable
        Try
            Dim myTable = New DataTable()
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand(sql, conn)
                    Dim dr As SqlDataReader = cmd.ExecuteReader()
                    myTable.Load(dr)
                    dr.Close()
                End Using
                conn.Close()
            End Using
            Return myTable  'While Schleife wird hier verlassen
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'loadDgv'")
        End Try
        Return Nothing
    End Function


    Public Function delBrgAufschubEinzahlung(id As String) As Boolean
        Try
            Dim myTable = New DataTable()
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("DELETE FROM tblBrgAufschub  WHERE brgak_id= '" & id & "'", conn)
                    cmd.ExecuteReader()
                    Return True
                End Using
                conn.Close()
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'loadDgv'")
        End Try
        Return False
    End Function

    Public Function loadDgvBySqlFromAufschubKto(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal brgKto As String, Optional where As String = "") As DataTable

        Dim sql As String = "SELECT [brgak_id] as Id,[brgak_datum] as Datum,[brgak_betrag] as Betrag,[brgak_filename] as Dateiname,[brgak_atc] as ATC " &
                    " FROM tblBrgAufschub WHERE ( brgak_datum BETWEEN '" & datumVon & " 00:00:00' AND '" & datumBis & " 23:59:59') AND brgak_brgaktoId = '" & brgKto & "' " & where

        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function

    Public Function loadDgvBySqlFromAufschubKto_Faelligkeit(ByVal faelligkeitWHERE As String, ByVal brgKto As String, Optional where As String = "") As DataTable

        Dim sql As String = "SELECT [brgak_id] as Id,[brgak_datum] as Datum,[brgak_betrag] as Betrag,[brgak_filename] as Dateiname,[brgak_atc] as ATC " &
                    " FROM tblBrgAufschub WHERE    brgak_brgaktoId = '" & brgKto & "' " & faelligkeitWHERE & where
        '    MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing

    End Function
    Public Function loadDgvBySqlFromBrgKtoAtlas(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable

        Dim sql As String = "SELECT [ID_NCTS], [basman_nr], [basman_nl], [veoant_beznr], [veoant_lfdnr], [veoant_stat], [veoant_arbnr], [veoant_mrn], [veoant_abgdst], [veoant_bedst], [veoant_wgdat], [veoant_anzpos], [veoant_andat], [veoant_korant], [veoant_vrbdat], [veoant_sb], [veopos_posnr], [veopos_stat], [veopos_wbsch1], [veopos_rohmas], [veoerz_sicbsc], [veoerz_basbtg],[veoerz_sicbtg],[vegdat_erldat] " &
                    " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where

        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function
    Public Function loadDgvBySqlFromBrgKtoUnionEZoll(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = " SELECT [CRN], [AnmeldungsDatum], [GarantieWert], [AnmID], [ErledigungsTypID], [Warenbezeichnung], [Absender], [Empfaenger], [Gesamtrohmasse], [Abgangszollstelle], [Bestimmungszollstelle], [Gestellungsfrist], [LRN], [DatumBestimmungErreicht], [GRN] " &
                  " FROM tblZolaris_EZoll_NCTS WHERE ( AnmeldungsDatum BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND GRN = '" & buergschaft & "' " & where

        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function

    Public Function loadDgvBySqlFromBrgKtoDAKOSY(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,  (nctsSA_AbgabenBetrag), ncts_dyaAnmID, ncts_ArtAnmeldung,nctsSA_GRN"
        sql &= " from tblDakosy_NCTSOut_Sicherheitsangaben inner join [VERAG].[dbo].[tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND nctsSA_GRN='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function


    Public Function loadDgvBySqlFromBrgKtoDAKOSY004(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,  ([nctsGRT_AmountToBeCovered]), ncts_dyaAnmID, ncts_Declarationtype ncts_ArtAnmeldung,ncts_Declarationtype,nctsGRT_GRN nctsSA_GRN,nctsGRT_GRN"
        sql &= " from tblDakosy_NCTSOut004_Guarantee inner join [tblDakosy_NCTSOut004] on ncts_Id = nctsGRT_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND [nctsGRT_GRN]='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function



    Public Function loadDgvBySqlFromBrgKtoDAKOSYDetails(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,ncts_Status,(nctsSA_AbgabenBetrag), ncts_dyaAnmID, ncts_ArtAnmeldung,nctsSA_GRN,ncts_Empfaenger_eori,ncts_Empfaenger_NLNR,ncts_Empfaenger_Name,ncts_Bearbeiter"
        sql &= "  ,[ncts_ObjectName],[ncts_Trans_DatumZeit] as Transaktionsdatum,[ncts_dyaArt],[ncts_ANR],[ncts_MRN],[ncts_ObjectAlias],[ncts_GesamtRohmasse],[ncts_GesamtAnzahlPackstuecke] "
        sql &= " from tblDakosy_NCTSOut_Sicherheitsangaben inner join [VERAG].[dbo].[tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND nctsSA_GRN='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function

    Public Function loadDgvBySqlFromBrgKtoDAKOSYDetails004(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,ncts_Status,[nctsGRT_AmountToBeCovered]nctsSA_AbgabenBetrag,[nctsGRT_AmountToBeCovered], ncts_dyaAnmID, ncts_Declarationtype ncts_ArtAnmeldung,ncts_Declarationtype,[nctsGRT_GRN] nctsSA_GRN,ncts_Consignee_IdentificationNumber ncts_Empfaenger_eori,ncts_Consignee_SubsidiaryNumber ncts_Empfaenger_NLNR,ncts_Consignee_Name,ncts_Bearbeiter"
        sql &= "  ,[ncts_ObjectName],[ncts_Trans_DatumZeit] as Transaktionsdatum,[ncts_dyaArt],[ncts_ANR],[ncts_MRN],[ncts_ObjectAlias],ncts_GrossMass ncts_GesamtRohmasse,0 As ncts_GesamtAnzahlPackstuecke "
        sql &= " from tblDakosy_NCTSOut004_Guarantee inner join [tblDakosy_NCTSOut004] on ncts_Id = nctsGRT_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND nctsGRT_GRN='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function



    Public Function loadDgvBySqlFromBrgKt_NCTS_FREMD_Details(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,ncts_Status,[nctsGRT_AmountToBeCovered]nctsSA_AbgabenBetrag,[nctsGRT_AmountToBeCovered], ncts_dyaAnmID, ncts_Declarationtype ncts_ArtAnmeldung,ncts_Declarationtype,[nctsGRT_GRN] nctsSA_GRN,ncts_Consignee_IdentificationNumber ncts_Empfaenger_eori,ncts_Consignee_SubsidiaryNumber ncts_Empfaenger_NLNR,ncts_Consignee_Name,ncts_Bearbeiter"
        sql &= "  ,[ncts_ObjectName],[ncts_Eroeffnung] as Transaktionsdatum,[ncts_dyaArt],[ncts_ANR],[ncts_MRN],[ncts_ObjectAlias],ncts_GrossMass ncts_GesamtRohmasse,0 As ncts_GesamtAnzahlPackstuecke "
        sql &= " from tblNCTS_FREMD_Guarantee inner join [tblNCTS_FREMD] on ncts_Id = nctsGRT_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND nctsGRT_GRN='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function



    Public Function loadDgvBySqlFromBrgKt_NCTS_TR_Details(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT ncts_MRN,ncts_Status,(nctsSA_AbgabenBetrag), ncts_dyaAnmID, ncts_ArtAnmeldung,nctsSA_GRN,ncts_Empfaenger_eori,ncts_Empfaenger_NLNR,ncts_Empfaenger_Name,ncts_Bearbeiter"
        sql &= "  ,[ncts_ObjectName],[ncts_Trans_DatumZeit] as Transaktionsdatum,[ncts_dyaArt],[ncts_ANR],[ncts_MRN],[ncts_ObjectAlias],[ncts_GesamtRohmasse],[ncts_GesamtAnzahlPackstuecke] "
        sql &= " from tblNCTS_TR_Sicherheitsangaben inner join tblNCTS_TR on ncts_Id = nctsSA_NctsId"
        sql &= " where ncts_Status between '" & statusVon & "' and '" & statusBis & "' AND nctsSA_GRN='" & buergschaft & "' " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlZOLARIS'")
            End Try
        End While

        Return Nothing
    End Function


    Public Function loadDgvBySqlFromBrgKtoTELOTECDetails(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As DataTable
        Dim sql As String = "SELECT dec_CreateDate asDatum, telanm_CRN as CRN,[telanm_BezugsNr] as LRN,telanm_Status as Status,([GVal]) as Betrag, telanm_ID as ID, telanm_ART as Art,GRN,ConeeTra_TIN as EORI,ConeeTra_Na as Empfänger,Represent_Na as Bearbeiter "
        sql &= " FROM tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId"
        sql &= " where [tblTelotec_Anmeldung].[dec_CreateDate] BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & statusVon & "' and '" & statusBis & "' AND GRN='" & buergschaft & "' AND [telnam_aktuellsteNachricht]=1 " & where
        'MsgBox(sql)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr As SqlDataReader = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                    conn.Close()
                End Using
                Return myTable  'While Schleife wird hier verlassen
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'loadDgvBySqlFromBrgKtoTELOTECDetails'")
            End Try
        End While

        Return Nothing
    End Function

    Public Function checkUser(pcname, brgusr_name) As Boolean
        Dim sql As String = " SELECT * FROM tblBrgUser WHERE brgusr_name = @brgusr_name "
        Dim dr As SqlDataReader
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@brgusr_name", pcname & "\" & brgusr_name)
                dr = cmd.ExecuteReader()
                Try
                    If dr.HasRows Then
                        dr.Read()
                        If dr.Item("brgusr_allowEinzahlung") = True Then
                            frmMain.allowEinzahlung = True
                        End If
                        If dr.Item("brgusr_allowBelaustungAufheben") = True Then
                            frmMain.brgusr_allowBelaustungAufheben = True
                        End If
                        If dr.Item("brgusr_AdminUser") = True Then
                            frmMain.brgusr_AdminUser = True
                        End If

                        frmMain.firma = dr.Item("brgusr_firma").ToString 'FIRMA GESETZT!

                        Return True
                    End If
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler: " & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                End Try
            End Using
        End Using
        Return False
    End Function



    Public Function insertBrgAufschubEinzahlung(brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc, brgak_faelligkeitsdatum) As Boolean
        Try
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("INSERT INTO tblBrgAufschub " &
                                            " (brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc, brgak_art,brgak_faelligkeitsdatum) VALUES" &
                                            " (@brgak_datum, @brgak_betrag, @brgak_brgaktoId, @brgak_filename, @brgak_atc,'einzahlung',@brgak_faelligkeitsdatum) ", conn)
                    cmd.Parameters.AddWithValue("@brgak_datum", brgak_datum)
                    cmd.Parameters.AddWithValue("@brgak_betrag", brgak_betrag)
                    cmd.Parameters.AddWithValue("@brgak_brgaktoId", brgak_brgaktoId)
                    cmd.Parameters.AddWithValue("@brgak_filename", brgak_filename)
                    cmd.Parameters.AddWithValue("@brgak_atc", brgak_atc)
                    cmd.Parameters.AddWithValue("@brgak_faelligkeitsdatum", brgak_faelligkeitsdatum)

                    Try
                        cmd.ExecuteNonQuery()
                        Return True
                    Catch ex As SqlException
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                    End Try
                End Using
            End Using
            Return False
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'X'")
        End Try
        Return Nothing
    End Function

    Public Function insertBrgAufschubAusgleichsbetrag(brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc) As Boolean
        Try
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("INSERT INTO tblBrgAufschub " &
                                            " (brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc, brgak_art) VALUES" &
                                            " (@brgak_datum, @brgak_betrag, @brgak_brgaktoId, @brgak_filename, @brgak_atc,'Ausgleichsbetrag') ", conn)
                    cmd.Parameters.AddWithValue("@brgak_datum", brgak_datum)
                    cmd.Parameters.AddWithValue("@brgak_betrag", brgak_betrag)
                    cmd.Parameters.AddWithValue("@brgak_brgaktoId", brgak_brgaktoId)
                    cmd.Parameters.AddWithValue("@brgak_filename", brgak_filename)
                    cmd.Parameters.AddWithValue("@brgak_atc", brgak_atc)

                    Try
                        cmd.ExecuteNonQuery()
                        Return True
                    Catch ex As SqlException
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                    End Try
                End Using
            End Using
            Return False
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'X'")
        End Try
        Return Nothing
    End Function


    Public Function deleteBrgAufschubAusgleichsbetrag(brgak_brgaktoId) As Boolean
        Try
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("DELETE FROM tblBrgAufschub " &
                                            " WHERE brgak_art='Ausgleichsbetrag' AND brgak_brgaktoId=@brgak_brgaktoId", conn)
                    cmd.Parameters.AddWithValue("@brgak_brgaktoId", brgak_brgaktoId)

                    Try
                        cmd.ExecuteNonQuery()
                        Return True
                    Catch ex As SqlException
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                    End Try
                End Using
            End Using
            Return False
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'X'")
        End Try
        Return Nothing
    End Function

    Public Function insertBrgAufschubZuBelastung(brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc) As Boolean
        Try
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand("INSERT INTO tblBrgAufschub " &
                                            " (brgak_datum, brgak_betrag, brgak_brgaktoId, brgak_filename, brgak_atc, brgak_art) VALUES" &
                                            " (@brgak_datum, @brgak_betrag, @brgak_brgaktoId, @brgak_filename, @brgak_atc,'belastung') ", conn)
                    cmd.Parameters.AddWithValue("@brgak_datum", brgak_datum)
                    cmd.Parameters.AddWithValue("@brgak_betrag", brgak_betrag)
                    cmd.Parameters.AddWithValue("@brgak_brgaktoId", brgak_brgaktoId)
                    cmd.Parameters.AddWithValue("@brgak_filename", brgak_filename)
                    cmd.Parameters.AddWithValue("@brgak_atc", brgak_atc)

                    Try
                        cmd.ExecuteNonQuery()
                        Return True
                    Catch ex As SqlException
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                    End Try
                End Using
            End Using
            Return False
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'X'")
        End Try
        Return Nothing
    End Function

    Public Function getBrgSumFromFMZOLL_Zabis(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String

        Dim tmp_sub As String = 0 'getBrgSumFromFMZOLL_Zabis_SUBEN(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Dim tmp_sbg As String = 0 ' getBrgSumFromFMZOLL_Zabis_SALZBURG(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Dim tmp_DY As String = getBrgSumFrom_DAKOSY(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Dim tmp_DY004 As String = getBrgSumFrom_DAKOSY_NCTS004(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        'Dim tmp_nctsTR As String = getBrgSumFrom_NCTS_TR(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Return CDbl(tmp_sub) + CDbl(tmp_sbg) + CDbl(tmp_DY) + CDbl(tmp_DY004) ' + CDbl(tmp_nctsTR)

    End Function

    Public Function getBrgSumFromFMZOLL_TELOTEC(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String

        Dim tmp_DY As String = getBrgSumFrom_TELOTEC(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Return CDbl(tmp_DY)


    End Function


    Public Function getBrgSumFromFMZOLL_NCTS_TR(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String

        Dim tmp_DY As String = getBrgSumFrom_NCTS_TR(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Dim tmp_FREMD As String = getBrgSumFrom_NCTS_FREMD(datumVon, datumBis, statusVon, statusBis, buergschaft, where)
        Return CDbl(tmp_DY) + CDbl(tmp_FREMD)


    End Function

    Public Function getBrgSumFromFMZOLL_Zabis_SUBEN(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        Return Nothing
        Dim sql As String = "SELECT SUM(veoerz_sicbtg) " &
                    " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function




    Public Function getBrgSumFrom_DAKOSY(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        'Return Nothing
        Dim sql As String = "SELECT sum(nctsSA_AbgabenBetrag)"
        sql &= " from tblDakosy_NCTSOut_Sicherheitsangaben inner join [tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId"
        sql &= " where ncts_Trans_DatumZeit BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND ncts_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND nctsSA_GRN = '" & buergschaft & "' " & where
        'MsgBox(sql)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            '   MsgBox(dr.GetValue(0))
                            Return dr.GetValue(0)

                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function


    Public Function getBrgSumFrom_DAKOSY_NCTS004(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        'Return Nothing

        Dim sql As String = "SELECT sum([nctsGRT_AmountToBeCovered])"
        sql &= " from tblDakosy_NCTSOut004_Guarantee inner join [tblDakosy_NCTSOut004] on ncts_Id = nctsGRT_NctsId"
        sql &= " where ncts_Trans_DatumZeit BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND ncts_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND [nctsGRT_GRN] = '" & buergschaft & "' " & where
        'MsgBox(sql)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            '   MsgBox(dr.GetValue(0))
                            Return dr.GetValue(0)

                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function


    Public Function getBrgSumFrom_NCTS_FREMD(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        'Return Nothing

        Dim sql As String = "SELECT sum([nctsGRT_AmountToBeCovered])"
        sql &= " from [tblNCTS_FREMD_Guarantee] inner join [tblNCTS_FREMD] on ncts_Id = nctsGRT_NctsId"
        sql &= " where [ncts_Eroeffnung] BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND ncts_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND [nctsGRT_GRN] = '" & buergschaft & "' " & where
        'MsgBox(sql)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            '   MsgBox(dr.GetValue(0))
                            Return dr.GetValue(0)

                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function


    Public Function getBrgSumFrom_NCTS_TR(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        'Return Nothing
        Dim sql As String = "SELECT sum(nctsSA_AbgabenBetrag)"
        sql &= " from tblNCTS_TR_Sicherheitsangaben inner join [tblNCTS_TR] on ncts_Id = nctsSA_NctsId"
        sql &= " where [ncts_Eroeffnung] BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND ncts_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND nctsSA_GRN = '" & buergschaft & "' " & where
        'MsgBox(sql)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            '   MsgBox(dr.GetValue(0))
                            Return dr.GetValue(0)

                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function



    Public Function getBrgSumFrom_TELOTEC(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String
        'Return Nothing
        Dim sql As String = " SELECT sum(tblTelotec_Sicherheit.GVal)
                               from tblTelotec_Sicherheit inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
                               where [tblTelotec_Anmeldung].[dec_CreateDate] BETWEEN '" & datumVon.ToShortDateString & " 00:00:00' AND '" & datumBis.ToShortDateString & " 23:23:59' AND telanm_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND GRN = '" & buergschaft & "' AND [telnam_aktuellsteNachricht]=1 "

        '   MsgBox(sql)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            '   MsgBox(dr.GetValue(0))
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function


    Public Function getBrgSumFromFMZOLL_Zabis_SALZBURG(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String

        Dim sql As String = "SELECT SUM(veoerz_sicbtg) " &
                    " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function

    Public Function getBrgSumFromBrgKto(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal brgKto As String, Optional where As String = "", Optional EinzahlungsbetraegeMitrechnen As Boolean = True, Optional faelligkeitsTAG As String = "", Optional faelligkeit As Object = Nothing) As String
        Dim einzahlung = ""
        If Not EinzahlungsbetraegeMitrechnen Then
            einzahlung = " AND [brgak_art] NOT IN ('einzahlung') "
        End If

        Dim sql As String = ""
        If faelligkeit Is Nothing Then

            Dim faelligkeitWhere As String = ""

            If faelligkeitsTAG <> "" Then
                If faelligkeitsTAG = "16" Then
                    faelligkeitWhere = " And (DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " Or [brgak_faelligkeitsdatum] Is null) "
                Else
                    faelligkeitWhere = " And DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " "
                End If
            End If

            sql = "SELECT SUM(brgak_betrag) " &
                    " FROM tblBrgAufschub WHERE ( brgak_datum BETWEEN '" & datumVon & " 00:00:00' AND '" & datumBis & " 23:59:59') AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung & faelligkeitWhere

        Else
            sql = "SELECT SUM(brgak_betrag) " &
                    " FROM tblBrgAufschub WHERE  brgak_faelligkeitsdatum='" & CDate(faelligkeit).ToShortDateString & "' AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung
        End If

        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function
    Public Function getBrgSumFromBrgKto_FelligZukunft(ByVal brgKto As String, Optional where As String = "", Optional EinzahlungsbetraegeMitrechnen As Boolean = True, Optional faelligkeitsTAG As String = "", Optional faelligkeit As Object = Nothing) As String
        Dim einzahlung = ""
        If Not EinzahlungsbetraegeMitrechnen Then
            einzahlung = " AND [brgak_art] NOT IN ('einzahlung') "
        End If

        Dim sql As String = ""
        If faelligkeit Is Nothing Then

            Dim faelligkeitWhere As String = ""

            If faelligkeitsTAG <> "" Then
                If faelligkeitsTAG = "16" Then
                    faelligkeitWhere = " And (DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " Or [brgak_faelligkeitsdatum] Is null) "
                Else
                    faelligkeitWhere = " And DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " "
                End If
            End If

            sql = "SELECT SUM(brgak_betrag) " &
                    " FROM tblBrgAufschub WHERE ( brgak_faelligkeitsdatum> getdate()) AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung & faelligkeitWhere

        Else
            sql = "SELECT SUM(brgak_betrag) " &
                    " FROM tblBrgAufschub WHERE  brgak_faelligkeitsdatum='" & CDate(faelligkeit).ToShortDateString & "' AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung
        End If

        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function

    Public Function getBrgFromBrgKto(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal brgKto As String, Optional where As String = "", Optional EinzahlungsbetraegeMitrechnen As Boolean = True, Optional faelligkeitsTAG As String = "", Optional faelligkeit As Object = Nothing) As DataTable
        Dim einzahlung = ""
        If Not EinzahlungsbetraegeMitrechnen Then
            einzahlung = " AND [brgak_art] NOT IN ('einzahlung') "
        End If

        Dim sql As String = ""
        If faelligkeit Is Nothing Then

            Dim faelligkeitWhere As String = ""

            If faelligkeitsTAG <> "" Then
                If faelligkeitsTAG = "16" Then
                    faelligkeitWhere = " And (DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " Or [brgak_faelligkeitsdatum] Is null) "
                Else
                    faelligkeitWhere = " And DATEPART(day,[brgak_faelligkeitsdatum]  )=" & faelligkeitsTAG & " "
                End If
            End If

            sql = "SELECT * " &
                    " FROM tblBrgAufschub WHERE ( brgak_datum BETWEEN '" & datumVon & " 00:00:00' AND '" & datumBis & " 23:59:59') AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung & faelligkeitWhere

        Else
            sql = "SELECT * " &
                    " FROM tblBrgAufschub WHERE  brgak_faelligkeitsdatum='" & CDate(faelligkeit).ToShortDateString & "' AND brgak_brgaktoId = '" & brgKto & "' " & where & einzahlung
        End If

        Dim ADMINSQL As New VERAG_PROG_ALLGEMEIN.SQL
        Return ADMINSQL.loadDgvBySql(sql, "ADMIN")

    End Function


    Public Function getBrgSumFromZolaris(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal kto As String, ByVal aufgehobenAbziehen As Boolean, Optional where As String = "") As String
        Try
            Dim sql As String =
                " SELECT SUM(Betrag) FROM  tblBrgAufschubDataAT WHERE  Abgabenkonto=@kto " &
                "	AND Datum BETWEEN @datumVon AND @datumBis " &
                "	AND [Abgabenart] IN ('1BD','0ML','0PB','0PG','0PS','1LG','1ZN','2AS','2DU','2EU','2IG','2KO','2PE','2RB','2SB','2SI','2VF','2VS','5DU','5EU','5SI','5VS','5ZO','5ZZ','6AK','6BE','6MS','6SS','6TS','6ZW','9EA','A20','A30','A35','A40','A45','C00','C10','D00','D10','E00') "
            If aufgehobenAbziehen Then sql &= " AND brgAT_Aufgehoben = 0 "

            '" SELECT sum(isnull(ztIMsgGdsItemDutyCalc.Amnt,0)) AS Betrag  " &
            '"         FROM zzAktivitaet " &
            '" 	INNER JOIN ztIMsgGdsItem ON zzAktivitaet.LizenzNr = ztIMsgGdsItem.LizenzNr AND zzAktivitaet.OperatorID = ztIMsgGdsItem.OperatorID  " &
            '" 		AND zzAktivitaet.IMsgID = ztIMsgGdsItem.IMsgID  " &
            '" 	INNER JOIN ztIMsgGdsItemDutyCalc ON ztIMsgGdsItem.LizenzNr = ztIMsgGdsItemDutyCalc.LizenzNr  " &
            '" 		AND ztIMsgGdsItem.OperatorID = ztIMsgGdsItemDutyCalc.OperatorID AND ztIMsgGdsItem.IMsgID = ztIMsgGdsItemDutyCalc.IMsgID  " &
            '" 		AND ztIMsgGdsItem.GdsItemID = ztIMsgGdsItemDutyCalc.GdsItemID  " &
            '" 	INNER JOIN zsAnmGdsItem ON zzAktivitaet.LizenzNr = zsAnmGdsItem.LizenzNr AND zzAktivitaet.OperatorID = zsAnmGdsItem.OperatorID  " &
            '" 		AND zzAktivitaet.VorgangID = zsAnmGdsItem.VorgangID AND zzAktivitaet.AnmID = zsAnmGdsItem.AnmID  " &
            '" 		AND ztIMsgGdsItem.ItNr = zsAnmGdsItem.ItNr  " &
            '" 	INNER JOIN zsAnmGdsItemDHF ON zsAnmGdsItem.LizenzNr = zsAnmGdsItemDHF.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmGdsItemDHF.OperatorID  " &
            '" 		AND zsAnmGdsItem.VorgangID = zsAnmGdsItemDHF.VorgangID AND zsAnmGdsItem.AnmID = zsAnmGdsItemDHF.AnmID  " &
            '" 		AND zsAnmGdsItem.GdsItemID = zsAnmGdsItemDHF.GdsItemID  " &
            '" 	INNER JOIN zsAnmHea ON zsAnmGdsItem.LizenzNr = zsAnmHea.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmHea.OperatorID  " &
            '" 		AND zsAnmGdsItem.VorgangID = zsAnmHea.VorgangID AND zsAnmGdsItem.AnmID = zsAnmHea.AnmID  " &
            '" 	INNER JOIN zsAnmRefs ON zsAnmGdsItem.LizenzNr = zsAnmRefs.LizenzNr AND zsAnmGdsItem.OperatorID = zsAnmRefs.OperatorID  " &
            '" 		AND zsAnmGdsItem.VorgangID = zsAnmRefs.VorgangID AND zsAnmGdsItem.AnmID = zsAnmRefs.AnmID  " &
            '" 	INNER JOIN zsAnmDHFRefs ON zsAnmRefs.LizenzNr = zsAnmDHFRefs.LizenzNr AND zsAnmRefs.OperatorID = zsAnmDHFRefs.OperatorID  " &
            '" 		AND zsAnmRefs.VorgangID = zsAnmDHFRefs.VorgangID AND zsAnmRefs.AnmID = zsAnmDHFRefs.AnmID  " &
            '" 	LEFT OUTER JOIN vwAnm_First_Rechnung ON zsAnmGdsItem.LizenzNr = vwAnm_First_Rechnung.LizenzNr  " &
            '" 		AND zsAnmGdsItem.OperatorID = vwAnm_First_Rechnung.OperatorID AND zsAnmGdsItem.VorgangID = vwAnm_First_Rechnung.VorgangID  " &
            '" 		AND zsAnmGdsItem.AnmID = vwAnm_First_Rechnung.AnmID And zsAnmGdsItem.GdsItemID = vwAnm_First_Rechnung.GdsItemID  " &
            '" WHERE (zsAnmDHFRefs.ErledigungsTypID LIKE 'F%') AND (zzAktivitaet.MsgTyp = 'EZ922')  " &
            '" 	AND (NOT EXISTS (SELECT * FROM zzAktivitaet b WHERE b.MsgTyp = 'EZ922' AND zzAktivitaet.LizenzNr = b.LizenzNr  " &
            '" 		AND zzAktivitaet.OperatorID = b.OperatorID AND zzAktivitaet.VorgangID = b.VorgangID AND zzAktivitaet.AnmID = b.AnmID  " &
            '" 		AND zzAktivitaet.AktivitaetID > b.AktivitaetID))  " &
            '" 	AND (NOT (ztIMsgGdsItemDutyCalc.Amnt IS NULL))  " &
            '" 	AND (ztIMsgGdsItemDutyCalc.Ty like 'A%' OR ztIMsgGdsItemDutyCalc.Ty IN ('2VF', '2PE', '2KO', '2IG')) " &
            '" 	AND (zsAnmGdsItem.LizenzNr =  'S01122' )  " &
            '" 	AND (zsAnmHea.DecDT >=  convert(datetime,@datumVon,104)) " &
            '" 	AND (zsAnmHea.DecDT <  convert(datetime,@datumBis,104)) " &
            '" 	AND (zsAnmHea.DefPayRef IN (@kto)) "


            '  MsgBox(datumVon & " - " & kto)
            ' MsgBox(sql)
            '  Dim daten As New List(Of cEntry)
            Dim dr As SqlDataReader
            Dim daten As New List(Of cBuergschaft)
            Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@datumVon", datumVon)
                    cmd.Parameters.AddWithValue("@datumBis", datumBis)
                    cmd.Parameters.AddWithValue("@kto", kto)
                    cmd.CommandTimeout = 9000 'teilweise sehr große Abfrage
                    dr = cmd.ExecuteReader()
                    Try
                        Dim cnt As Integer = 0
                        If dr.Read Then
                            If Not dr.GetValue(0) Is DBNull.Value Then
                                Return dr.GetValue(0)
                            End If
                        End If
                        Return "0"
                    Catch ex As Exception
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankabfrage:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                    Finally
                        dr.Close()
                    End Try
                End Using
            End Using
        Catch ex As Exception
            MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
        End Try
        Return Nothing

    End Function


    Public Function getMonitoringData() As cMonitoringBrg

        Dim sql As String = "SELECT * FROM tblMonitoringBrgUserSettings WHERE brgusrset_username='default' "
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Dim cMonitoringBrg As New cMonitoringBrg
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        cMonitoringBrg.brgusrset_id = dr.Item("brgusrset_id")

                        cMonitoringBrg.brgusrset_username = dr.Item("brgusrset_username")
                        cMonitoringBrg.brgusrset_gvvAt_summe = dr.Item("brgusrset_gvvAt_summe")
                        cMonitoringBrg.brgusrset_gvvAt_tag_warn1 = dr.Item("brgusrset_gvvAt_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_tag_warn2 = dr.Item("brgusrset_gvvAt_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvAt_woche_warn1 = dr.Item("brgusrset_gvvAt_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_woche_warn2 = dr.Item("brgusrset_gvvAt_woche_warn2")
                        cMonitoringBrg.brgusrset_gvvDe_summe = dr.Item("brgusrset_gvvDe_summe")
                        cMonitoringBrg.brgusrset_gvvDe_tag_warn1 = dr.Item("brgusrset_gvvDe_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvDe_tag_warn2 = dr.Item("brgusrset_gvvDe_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvDe_woche_warn1 = dr.Item("brgusrset_gvvDe_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvDe_woche_warn2 = dr.Item("brgusrset_gvvDe_woche_warn2")

                        cMonitoringBrg.brgusrset_gvvDe2_summe = dr.Item("brgusrset_gvvde2_summe")
                        cMonitoringBrg.brgusrset_gvvDe2_tag_warn1 = dr.Item("brgusrset_gvvde2_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvDe2_tag_warn2 = dr.Item("brgusrset_gvvde2_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvDe2_woche_warn1 = dr.Item("brgusrset_gvvde2_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvDe2_woche_warn2 = dr.Item("brgusrset_gvvde2_woche_warn2")

                        cMonitoringBrg.brgusrset_aufschub_AG_de_summe = dr.Item("brgusrset_aufschub_AG_de_summe")
                        cMonitoringBrg.brgusrset_aufschub_AG_de_warn1 = dr.Item("brgusrset_aufschub_AG_de_warn1")
                        cMonitoringBrg.brgusrset_aufschub_AG_de_warn2 = dr.Item("brgusrset_aufschub_AG_de_warn2")
                        cMonitoringBrg.brgusrset_aufschub_AG_at_summe = dr.Item("brgusrset_aufschub_AG_at_summe")
                        cMonitoringBrg.brgusrset_aufschub_AG_at_warn1 = dr.Item("brgusrset_aufschub_AG_at_warn1")
                        cMonitoringBrg.brgusrset_aufschub_AG_at_warn2 = dr.Item("brgusrset_aufschub_AG_at_warn2")
                        cMonitoringBrg.brgusrset_aufschub_CS_at_summe = dr.Item("brgusrset_aufschub_CS_at_summe")
                        cMonitoringBrg.brgusrset_aufschub_CS_at_warn1 = dr.Item("brgusrset_aufschub_CS_at_warn1")
                        cMonitoringBrg.brgusrset_aufschub_CS_at_warn2 = dr.Item("brgusrset_aufschub_CS_at_warn2")
                        cMonitoringBrg.brgusrset_aufschub_ZS_at_summe = dr.Item("brgusrset_aufschub_ZS_at_summe")
                        cMonitoringBrg.brgusrset_aufschub_ZS_at_warn1 = dr.Item("brgusrset_aufschub_ZS_at_warn1")
                        cMonitoringBrg.brgusrset_aufschub_ZS_at_warn2 = dr.Item("brgusrset_aufschub_ZS_at_warn2")

                        cMonitoringBrg.brgusrset_aufschub_imex_de_summe = dr.Item("brgusrset_aufschub_imex_de_summe")
                        cMonitoringBrg.brgusrset_aufschub_imex_de_warn1 = dr.Item("brgusrset_aufschub_imex_de_warn1")
                        cMonitoringBrg.brgusrset_aufschub_imex_de_warn2 = dr.Item("brgusrset_aufschub_imex_de_warn2")

                        cMonitoringBrg.brgusrset_gvvAt_imex_de_summe = dr.Item("brgusrset_gvvAt_imex_de_summe")
                        cMonitoringBrg.brgusrset_gvvAt_imex_tag_warn1 = dr.Item("brgusrset_gvvAt_imex_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_imex_tag_warn2 = dr.Item("brgusrset_gvvAt_imex_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvAt_imex_woche_warn1 = dr.Item("brgusrset_gvvAt_imex_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_imex_woche_warn2 = dr.Item("brgusrset_gvvAt_imex_woche_warn2")



                        cMonitoringBrg.brgusrset_gvvAt_ATILLA_de_summe = dr.Item("brgusrset_gvvAt_ATILLA_de_summe")
                        cMonitoringBrg.brgusrset_gvvAt_ATILLA_tag_warn1 = dr.Item("brgusrset_gvvAt_ATILLA_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_ATILLA_tag_warn2 = dr.Item("brgusrset_gvvAt_ATILLA_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvAt_ATILLA_woche_warn1 = dr.Item("brgusrset_gvvAt_ATILLA_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_ATILLA_woche_warn2 = dr.Item("brgusrset_gvvAt_ATILLA_woche_warn2")



                        cMonitoringBrg.brgusrset_gvvAt_CS_at_summe = dr.Item("brgusrset_gvvAt_CS_at_summe")
                        cMonitoringBrg.brgusrset_gvvAt_CS_tag_warn1 = dr.Item("brgusrset_gvvAt_CS_tag_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_CS_tag_warn2 = dr.Item("brgusrset_gvvAt_CS_tag_warn2")
                        cMonitoringBrg.brgusrset_gvvAt_CS_woche_warn1 = dr.Item("brgusrset_gvvAt_CS_woche_warn1")
                        cMonitoringBrg.brgusrset_gvvAt_CS_woche_warn2 = dr.Item("brgusrset_gvvAt_CS_woche_warn2")



                    End If
                    Return cMonitoringBrg
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return cMonitoringBrg
    End Function


    Public Function setMonitoringData(d As cMonitoringBrg) As Boolean
        Dim hAuswahl As String = ""




        'Änderung
        Dim sql = "UPDATE tblMonitoringBrgUserSettings " &
                         " SET brgusrset_username=@brgusrset_username, brgusrset_gvvAt_summe=@brgusrset_gvvAt_summe, brgusrset_gvvAt_tag_warn1=@brgusrset_gvvAt_tag_warn1, brgusrset_gvvAt_tag_warn2=@brgusrset_gvvAt_tag_warn2, brgusrset_gvvAt_woche_warn1=@brgusrset_gvvAt_woche_warn1, brgusrset_gvvAt_woche_warn2=@brgusrset_gvvAt_woche_warn2, brgusrset_gvvDe_summe=@brgusrset_gvvDe_summe, brgusrset_gvvDe_tag_warn1=@brgusrset_gvvDe_tag_warn1, brgusrset_gvvDe_tag_warn2=@brgusrset_gvvDe_tag_warn2, brgusrset_gvvDe_woche_warn1=@brgusrset_gvvDe_woche_warn1, brgusrset_gvvDe_woche_warn2=@brgusrset_gvvDe_woche_warn2, " &
                         " brgusrset_gvvde2_summe=@brgusrset_gvvde2_summe, brgusrset_gvvde2_tag_warn1=@brgusrset_gvvde2_tag_warn1, brgusrset_gvvde2_tag_warn2=@brgusrset_gvvde2_tag_warn2, brgusrset_gvvde2_woche_warn1=@brgusrset_gvvde2_woche_warn1, brgusrset_gvvde2_woche_warn2=@brgusrset_gvvde2_woche_warn2, " &
                         " brgusrset_aufschub_AG_de_summe=@brgusrset_aufschub_AG_de_summe, brgusrset_aufschub_AG_de_warn1=@brgusrset_aufschub_AG_de_warn1, brgusrset_aufschub_AG_de_warn2=@brgusrset_aufschub_AG_de_warn2, brgusrset_aufschub_AG_at_summe=@brgusrset_aufschub_AG_at_summe, brgusrset_aufschub_AG_at_warn1=@brgusrset_aufschub_AG_at_warn1, brgusrset_aufschub_AG_at_warn2=@brgusrset_aufschub_AG_at_warn2, brgusrset_aufschub_CS_at_summe=@brgusrset_aufschub_CS_at_summe, brgusrset_aufschub_CS_at_warn1=@brgusrset_aufschub_CS_at_warn1, brgusrset_aufschub_CS_at_warn2=@brgusrset_aufschub_CS_at_warn2, brgusrset_aufschub_ZS_at_summe=@brgusrset_aufschub_ZS_at_summe, brgusrset_aufschub_ZS_at_warn1=@brgusrset_aufschub_ZS_at_warn1, brgusrset_aufschub_ZS_at_warn2=@brgusrset_aufschub_ZS_at_warn2, " &
                         " brgusrset_aufschub_imex_de_summe=@brgusrset_aufschub_imex_de_summe,brgusrset_aufschub_imex_de_warn1=@brgusrset_aufschub_imex_de_warn1,brgusrset_aufschub_imex_de_warn2=@brgusrset_aufschub_imex_de_warn2,brgusrset_gvvAt_imex_de_summe =@brgusrset_gvvAt_imex_de_summe,brgusrset_gvvAt_imex_tag_warn1=@brgusrset_gvvAt_imex_tag_warn1,brgusrset_gvvAt_imex_tag_warn2=@brgusrset_gvvAt_imex_tag_warn2,brgusrset_gvvAt_imex_woche_warn1=@brgusrset_gvvAt_imex_woche_warn1,brgusrset_gvvAt_imex_woche_warn2=@brgusrset_gvvAt_imex_woche_warn2,   " &
                         " brgusrset_gvvAt_ATILLA_de_summe =@brgusrset_gvvAt_ATILLA_de_summe,brgusrset_gvvAt_ATILLA_tag_warn1=@brgusrset_gvvAt_ATILLA_tag_warn1,brgusrset_gvvAt_ATILLA_tag_warn2=@brgusrset_gvvAt_ATILLA_tag_warn2,brgusrset_gvvAt_ATILLA_woche_warn1=@brgusrset_gvvAt_ATILLA_woche_warn1,brgusrset_gvvAt_ATILLA_woche_warn2=@brgusrset_gvvAt_ATILLA_woche_warn2  " &
                         " brgusrset_gvvAt_CS_at_summe =@brgusrset_gvvAt_CS_at_summe,brgusrset_gvvAt_CS_tag_warn1=@brgusrset_gvvAt_CS_tag_warn1,brgusrset_gvvAt_CS_tag_warn2=@brgusrset_gvvAt_CS_tag_warn2,brgusrset_gvvAt_CS_woche_warn1=@brgusrset_gvvAt_CS_woche_warn1,brgusrset_gvvAt_CS_woche_warn2=@brgusrset_gvvAt_CS_woche_warn2  " &
                            " WHERE brgusrset_id=@brgusrset_id "



        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                'msgbo
                cmd.Parameters.AddWithValue("@brgusrset_id", d.brgusrset_id)
                cmd.Parameters.AddWithValue("@brgusrset_username", d.brgusrset_username)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_summe", d.brgusrset_gvvAt_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_tag_warn1", d.brgusrset_gvvAt_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_tag_warn2", d.brgusrset_gvvAt_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_woche_warn1", d.brgusrset_gvvAt_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_woche_warn2", d.brgusrset_gvvAt_woche_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe_summe", d.brgusrset_gvvDe_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe_tag_warn1", d.brgusrset_gvvDe_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe_tag_warn2", d.brgusrset_gvvDe_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe_woche_warn1", d.brgusrset_gvvDe_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe_woche_warn2", d.brgusrset_gvvDe_woche_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe2_summe", d.brgusrset_gvvDe2_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe2_tag_warn1", d.brgusrset_gvvDe2_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe2_tag_warn2", d.brgusrset_gvvDe2_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe2_woche_warn1", d.brgusrset_gvvDe2_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvDe2_woche_warn2", d.brgusrset_gvvDe2_woche_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_de_summe", d.brgusrset_aufschub_AG_de_summe)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_de_warn1", d.brgusrset_aufschub_AG_de_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_de_warn2", d.brgusrset_aufschub_AG_de_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_at_summe", d.brgusrset_aufschub_AG_at_summe)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_at_warn1", d.brgusrset_aufschub_AG_at_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_AG_at_warn2", d.brgusrset_aufschub_AG_at_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_CS_at_summe", d.brgusrset_aufschub_CS_at_summe)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_CS_at_warn1", d.brgusrset_aufschub_CS_at_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_CS_at_warn2", d.brgusrset_aufschub_CS_at_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_ZS_at_summe", d.brgusrset_aufschub_ZS_at_summe)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_ZS_at_warn1", d.brgusrset_aufschub_ZS_at_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_ZS_at_warn2", d.brgusrset_aufschub_ZS_at_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_imex_de_summe", d.brgusrset_aufschub_imex_de_summe)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_imex_de_warn1", d.brgusrset_aufschub_imex_de_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_aufschub_imex_de_warn2", d.brgusrset_aufschub_imex_de_warn2)

                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_imex_de_summe", d.brgusrset_gvvAt_imex_de_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_imex_tag_warn1", d.brgusrset_gvvAt_imex_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_imex_tag_warn2", d.brgusrset_gvvAt_imex_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_imex_woche_warn1", d.brgusrset_gvvAt_imex_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_imex_woche_warn2", d.brgusrset_gvvAt_imex_woche_warn2)



                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_ATILLA_de_summe", d.brgusrset_gvvAt_ATILLA_de_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_ATILLA_tag_warn1", d.brgusrset_gvvAt_ATILLA_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_ATILLA_tag_warn2", d.brgusrset_gvvAt_ATILLA_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_ATILLA_woche_warn1", d.brgusrset_gvvAt_ATILLA_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_ATILLA_woche_warn2", d.brgusrset_gvvAt_ATILLA_woche_warn2)


                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_CS_at_summe", d.brgusrset_gvvAt_CS_at_summe)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_CS_tag_warn1", d.brgusrset_gvvAt_CS_tag_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_CS_tag_warn2", d.brgusrset_gvvAt_CS_tag_warn2)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_CS_woche_warn1", d.brgusrset_gvvAt_CS_woche_warn1)
                cmd.Parameters.AddWithValue("@brgusrset_gvvAt_CS_woche_warn2", d.brgusrset_gvvAt_CS_woche_warn2)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Die Version kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Version")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function getBrgSumFromFMZOLL_Zolaris(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, Optional where As String = "") As String

        Dim sql As String = "SELECT SUM(GarantieWert) " &
                    " FROM tblZolaris_EZoll_NCTS WHERE ( AnmeldungsDatum BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND GRN = '" & buergschaft & "' " & where
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader

        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.Read Then
                        If Not dr.GetValue(0) Is DBNull.Value Then
                            Return dr.GetValue(0)
                        End If
                    End If
                    Return "0"
                Catch ex As Exception
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                Finally
                    dr.Close()
                End Try
            End Using
        End Using
        Return Nothing
    End Function


    Public Function insertupdateMeldung(brgmld_date, brgmld_kto, brgmld_stufe, brgmld_user) As Boolean

        Dim sql As String = " begin tran" &
                           " if exists (select * from tblBrgMeldungen with (updlock,serializable) where brgmld_date = @brgmld_date AND brgmld_kto=@brgmld_kto AND brgmld_user=@brgmld_user AND brgmld_stufe=@brgmld_stufe ) " &
                            " begin " &
                                  " UPDATE tblBrgMeldungen " &
                                  " SET brgmld_date=@brgmld_date, brgmld_kto=@brgmld_kto, brgmld_stufe=@brgmld_stufe, brgmld_user=@brgmld_user " &
                                  " WHERE brgmld_date = @brgmld_date AND brgmld_kto=@brgmld_kto AND brgmld_user=@brgmld_user AND brgmld_stufe=@brgmld_stufe " &
                                " End " &
                           " else " &
                                   " begin " &
                                  " INSERT INTO tblBrgMeldungen " &
                                  " (brgmld_date, brgmld_kto, brgmld_stufe, brgmld_user) VALUES " &
                                  " (@brgmld_date, @brgmld_kto, @brgmld_stufe, @brgmld_user) " &
                                " End " &
                           " commit tran "

        'Dim cn As New SqlConnection()
        'cn.ConnectionString = "Data Source=SQLGUIDE01.verag.ost.dmn;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;"
        'cn.Open()

        Dim cn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN
        Using cn
            Using cmd As New SqlCommand(sql, cn)
                cmd.Parameters.AddWithValue("@brgmld_date", brgmld_date)
                cmd.Parameters.AddWithValue("@brgmld_kto", brgmld_kto)
                cmd.Parameters.AddWithValue("@brgmld_stufe", brgmld_stufe)
                cmd.Parameters.AddWithValue("@brgmld_user", brgmld_user)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler: " & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function existsMeldung(brgmld_date, brgmld_kto, brgmld_stufe, brgmld_user) As Boolean

        Dim sql As String = " SELECT * FROM tblBrgMeldungen WHERE brgmld_date = @brgmld_date AND brgmld_kto=@brgmld_kto AND brgmld_user=@brgmld_user AND brgmld_stufe=@brgmld_stufe  "

        Dim dr As SqlDataReader
        Using conn As SqlConnection = VERAG_PROG_ALLGEMEIN.SQL.GetNewOpenConnectionADMIN()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@brgmld_date", brgmld_date)
                cmd.Parameters.AddWithValue("@brgmld_kto", brgmld_kto)
                cmd.Parameters.AddWithValue("@brgmld_stufe", brgmld_stufe)
                cmd.Parameters.AddWithValue("@brgmld_user", brgmld_user)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    If dr.HasRows Then Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler: " & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                End Try

            End Using
        End Using
        Return False
    End Function

End Class
