Imports System.Data
Imports System.IO
Imports ADMIN.cMeineFunktionen
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports GrapeCity.Enterprise.Data.Expressions.Evaluation

Public Class cSqlDb
    Public Shared Function GetNewOpenConnection() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = My.Resources.connStringAdmin
        cn.Open()
        Return cn
    End Function
    Public Shared Function GetNewOpenConnectionFMZOLL() As SqlConnection

        Dim cn As New SqlConnection()

        If VERAG_PROG_ALLGEMEIN.cAllgemein.SQLGUIDE01_USED Then
            cn.ConnectionString = My.Resources.connStringFMZOLL_NEW
        Else
            cn.ConnectionString = My.Resources.connStringFMZOLL
        End If

        cn.Open()
        Return cn
    End Function
    Public Shared Function GetNewOpenConnectionFMZOLL_DEV() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = My.Resources.connStringFMZOLL_DEV
        cn.Open()
        Return cn
    End Function
End Class

Public Class sqlMitarbeiter2
    Public Shared Function GetNewOpenConnection() As SqlConnection
        Dim cn As New SqlConnection()
        cn.ConnectionString = My.Resources.connStringAdmin
        cn.Open()
        Return cn
    End Function
End Class
Public Class sqlConn
    Public Function getDataSQL(ByVal selectCommand As String) As SqlDataAdapter

        Dim d As New SqlDataAdapter()

        Try
            ' Specify a connection string. Replace the given value with a 
            ' valid connection string for a Northwind SQL Server sample
            ' database accessible to your system.
            Dim connectionString As String =
                "Data Source=DEVELOPER\DEVSQL;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;"

            ' Create a new data adapter based on the specified query.
            d = New SqlDataAdapter(selectCommand, connectionString)
            Return d
            ' Resize the DataGridView columns to fit the newly loaded content.

        Catch ex As SqlException
            MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.")
        End Try
        Return Nothing
    End Function

    Sub dotest()
        Dim cn As New SqlConnection()
        Dim CustomersDataSet As New DataSet()
        Dim da As SqlDataAdapter
        Dim cmdBuilder As SqlCommandBuilder
        cn.ConnectionString = "Data Source=DEVELOPER\DEVSQL;Initial Catalog=ADMIN;Integrated Security=false;User ID=sa;Password=BmWr501956;"
        cn.Open()
        da = New SqlDataAdapter("select * from tblBerechtigungen", cn)

        cmdBuilder = New SqlCommandBuilder(da)

        'Populate the dataset by running the Fill method of the SqlDataAdapter.
        da.Fill(CustomersDataSet, "tblBerechtigungen")

        'Modify the value of the CustName field.
        CustomersDataSet.Tables("tblBerechtigungen").Rows(0)("ber_name") = "Administrator"

        'Post the data modification to the database.
        da.Update(CustomersDataSet, "tblBerechtigungen")

        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": tblBerechtigungen ber_name updated successfully")

        'Close the database connection.
        cn.Close()

        Console.ReadLine()

    End Sub

    Public Function setBerechtigungenNEU(userId As Integer, bepr_berId As Integer, bepr_programm As String, allowed As Boolean) As Boolean


        Dim sql As String = " begin tran" &
                          " if exists (select * from tblBerProg with (updlock,serializable) where bepr_userId=@userId and bepr_berId=@bepr_berId and bepr_programm=@bepr_programm) " &
                                 " begin " &
                                 " UPDATE tblBerProg " &
                                 " SET ber_allowed=@allowed " &
                                 " WHERE bepr_userId = @userId and bepr_berId=@bepr_berId and bepr_programm=@bepr_programm " &
                          " End " &
                          " Else " &
                          " begin " &
                             " INSERT INTO tblBerProg " &
                             " ( bepr_userId,bepr_berId,bepr_programm,ber_allowed) VALUES " &
                             " ( @userId,@bepr_berId,@bepr_programm,@allowed) " &
                          "End " &
                          " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@userId", userId)
                cmd.Parameters.AddWithValue("@bepr_berId", bepr_berId)
                cmd.Parameters.AddWithValue("@bepr_programm", bepr_programm)
                cmd.Parameters.AddWithValue("@allowed", allowed)
                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
    End Function
End Class

Public Class cBrgDb

    Public Function getBrgFromFMZOLL(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String) As List(Of cBuergschaft)

        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 " &
                    " FROM tblZabis_Atlas_NCTS WHERE ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "') AND  ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND veoerz_sicbsc = '" & buergschaft & "' "
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    While dr.Read
                        cnt += 1
                    End While
                    ' MsgBox(cnt)
                    Return daten
                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 Double
        'Return Nothing
        Dim sql As String = "SELECT isnull(sum(cast(nctsSA_AbgabenBetrag as float)),0)"
        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 0
    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 Double

        Dim sql As String = "SELECT ISNULL(SUM(veoerz_sicbtg),0) " &
                    " 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 = cSqlDb.GetNewOpenConnectionFMZOLL()
            '  MsgBox(sql)
            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 0
    End Function

    Public Function getBrgSumFromFMZOLL_Zodiak(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 Double

        '  Dim sql As String = "SELECT ISNULL(SUM(veoerz_sicbtg),0) " &
        '              " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        Dim sql As String = "SELECT ISNULL(sum(nctsSA_AbgabenBetrag),0) "
        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 daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            '  MsgBox(sql)
            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 0
    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 Double

        '  Dim sql As String = "SELECT ISNULL(SUM(veoerz_sicbtg),0) " &
        '              " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        Dim sql As String = " SELECT ISNULL(sum(cast(GVal as float)),0) 
        from [tblTelotec_Sicherheit] inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
          where dec_CreateDate BETWEEN '" & datumVon.ToShortDateString & " 00:00:000' AND '" & datumBis.ToShortDateString & " 23:59:59' AND telanm_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND GRN = '" & buergschaft & "'  AND [telnam_aktuellsteNachricht]=1 " & 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 = cSqlDb.GetNewOpenConnectionFMZOLL()
            '  MsgBox(sql)
            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 ""
                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 getBrgFromFMZOLL_Zabis(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String, ByVal nl 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, History " &
                    " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' "
        If nl <> "" Then sql &= " AND basman_nl= '" & nl & "' "
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim myTable = New DataTable()
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    myTable.Load(dr)
                    Return myTable
                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 getBrgFromFMZOLL_Zolaris(ByVal datumVon As DateTime, ByVal datumBis As DateTime, ByVal statusVon As String, ByVal statusBis As String, ByVal buergschaft As String) As DataTable

        Dim sql As String = "SELECT * " &
                    " FROM tblZolaris_EZoll_NCTS WHERE ( AnmeldungsDatum BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND GRN = '" & buergschaft & "' " 'AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim myTable = New DataTable()
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    myTable.Load(dr)
                    Return myTable
                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_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 Double

        Dim sql As String = "SELECT ISNULL(SUM(GarantieWert),0) " &
                    " 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 = cSqlDb.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 ""
                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 getBrgTop5FromFMZOLL_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 List(Of topValues)

        Dim sql As String = "SELECT TOP 5  GarantieWert " &
                    " FROM tblZolaris_EZoll_NCTS WHERE ( AnmeldungsDatum BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND GRN = '" & buergschaft & "' " & where & " ORDER BY GarantieWert DESC "
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of topValues)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    While dr.Read
                        Dim d As New topValues
                        d.warenwert = 0 ' Gibt es in der Auswertung nicht
                        d.sicherheitsbetrag = dr.Item("GarantieWert")
                        daten.Add(d)
                    End While
                    Return daten
                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 getBrgTop5FromFMZOLL_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 List(Of topValues)

        Dim sql As String = "SELECT TOP 5 veoerz_basbtg, 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 & " ORDER BY veoerz_sicbtg DESC "
        ' MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of topValues)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    While dr.Read
                        Dim d As New topValues
                        d.warenwert = dr.Item("veoerz_basbtg")
                        d.sicherheitsbetrag = dr.Item("veoerz_sicbtg")
                        daten.Add(d)
                    End While
                    Return daten
                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 getBrgTop5FromFMZOLL_Zodiak(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 List(Of topValues)

        '  Dim sql As String = "SELECT ISNULL(SUM(veoerz_sicbtg),0) " &
        '              " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        Dim sql As String = "SELECT TOP 5 nctsSA_AbgabenBetrag,0 as SiB "
        sql &= " from tblDakosy_NCTSOut_Sicherheitsangaben inner join [tblDakosy_NCTSOut] on ncts_Id = nctsSA_NctsId"
        sql &= " where ncts_Trans_DatumZeit BETWEEN '" & datumVon & "' AND '" & datumBis & "' AND ncts_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND nctsSA_GRN = '" & buergschaft & "' " & where

        '  MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of topValues)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    While dr.Read
                        Dim d As New topValues
                        d.warenwert = dr.Item("nctsSA_AbgabenBetrag")
                        d.sicherheitsbetrag = dr.Item("SiB")
                        daten.Add(d)
                    End While
                    Return daten
                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 getBrgTop5FromFMZOLL_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 List(Of topValues)

        '  Dim sql As String = "SELECT ISNULL(SUM(veoerz_sicbtg),0) " &
        '              " FROM tblZabis_Atlas_NCTS WHERE ( veoant_andat BETWEEN '" & datumVon & "' AND '" & datumBis & "') AND ( veoant_stat BETWEEN '" & statusVon & "' AND '" & statusBis & "')  AND veoerz_sicbsc = '" & buergschaft & "' " & where
        Dim sql As String = "  SELECT TOP 5 tblTelotec_Sicherheit.GVal,0 as SiB  
                                from [tblTelotec_Sicherheit] inner join [tblTelotec_Anmeldung] on telanm_id = telgrt_telanmId
                                where dec_CreateDate BETWEEN '" & datumVon & "' AND '" & datumBis & "' AND telanm_Status BETWEEN '" & statusVon & "' AND '" & statusBis & "'  AND GRN = '" & buergschaft & "'  AND [telnam_aktuellsteNachricht]=1 " & where

        '  MsgBox(sql)
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of topValues)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnectionFMZOLL()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    Dim cnt As Integer = 0
                    While dr.Read
                        Dim d As New topValues
                        d.warenwert = dr.Item("nctsSA_AbgabenBetrag")
                        d.sicherheitsbetrag = dr.Item("SiB")
                        daten.Add(d)
                    End While
                    Return daten
                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 setBrgUpdateInsertByDateAll(ByVal daten As cBuergschaft) As Boolean
        Dim sql As String = " begin tran" &
                            " if exists (select * from tblBuergschaft with (updlock,serializable) where brg_datum = @brg_datum) " &
                                   " begin " &
                                   " UPDATE tblBuergschaft " &
                                   " SET brg_datum=@brg_datum, brg_at_tag=@brg_at_tag, brg_at_woche=@brg_at_woche, brg_de_tag=@brg_de_tag, brg_de_woche=@brg_de_woche, brg_at_tag_atlas=@brg_at_tag_atlas, brg_at_tag_zolaris=@brg_at_tag_zolaris, brg_at_woche_atlas=@brg_at_woche_atlas, brg_at_woche_zolaris=@brg_at_woche_zolaris, brg_de_tag_atlas=@brg_de_tag_atlas,brg_de_woche_atlas=@brg_de_woche_atlas, " &
                                        " brg_de2_tag=@brg_de2_tag, brg_de2_woche=@brg_de2_woche, brg_de2_tag_atlas=@brg_de2_tag_atlas, brg_de2_woche_atlas=@brg_de2_woche_atlas " &
                                   " WHERE brg_datum=@brg_datum " &
                            " End " &
                            " Else " &
                            " begin " &
                                "INSERT INTO tblBuergschaft " &
                                "(brg_datum, brg_at_tag, brg_at_woche, brg_de_tag, brg_de_woche, brg_at_tag_atlas, brg_at_tag_zolaris, brg_at_woche_atlas, brg_at_woche_zolaris, brg_de_tag_atlas,brg_de_woche_atlas,brg_de2_tag,brg_de2_woche,brg_de2_tag_atlas,brg_de2_woche_atlas) VALUES " &
                                "(@brg_datum, @brg_at_tag, @brg_at_woche, @brg_de_tag, @brg_de_woche, @brg_at_tag_atlas, @brg_at_tag_zolaris, @brg_at_woche_atlas, @brg_at_woche_zolaris, @brg_de_tag_atlas, @brg_de_woche_atlas,@brg_de2_tag,@brg_de2_woche,@brg_de2_tag_atlas,@brg_de2_woche_atlas)" &
                            " End " &
                            " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@brg_datum", daten.brg_datum)
                cmd.Parameters.AddWithValue("@brg_at_tag", daten.brg_at_tag)
                cmd.Parameters.AddWithValue("@brg_at_woche", daten.brg_at_woche)
                cmd.Parameters.AddWithValue("@brg_de_tag", daten.brg_de_tag)
                cmd.Parameters.AddWithValue("@brg_de_woche", daten.brg_de_woche)
                cmd.Parameters.AddWithValue("@brg_at_tag_atlas", daten.brg_at_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_at_tag_zolaris", daten.brg_at_tag_zolaris)
                cmd.Parameters.AddWithValue("@brg_at_woche_atlas", daten.brg_at_woche_atlas)
                cmd.Parameters.AddWithValue("@brg_at_woche_zolaris", daten.brg_at_woche_zolaris)
                cmd.Parameters.AddWithValue("@brg_de_tag_atlas", daten.brg_de_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_de_woche_atlas", daten.brg_de_woche_atlas)
                cmd.Parameters.AddWithValue("@brg_id", daten.brg_id)

                cmd.Parameters.AddWithValue("@brg_de2_tag", daten.brg_de2_tag)
                cmd.Parameters.AddWithValue("@brg_de2_woche", daten.brg_de2_woche)
                cmd.Parameters.AddWithValue("@brg_de2_tag_atlas", daten.brg_de2_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_de2_woche_atlas", daten.brg_de2_woche_atlas)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function setBrgUpdateInsertByDateOnlyWochenRef_OLD(ByVal daten As cBuergschaft) As Boolean
        Dim sql As String = " begin tran" &
                            " if exists (select * from tblBuergschaft with (updlock,serializable) where brg_datum = @brg_datum) " &
                                   " begin " &
                                   " UPDATE tblBuergschaft " &
                                   " SET brg_datum=@brg_datum, brg_at_woche=@brg_at_woche, brg_de_woche=@brg_de_woche, brg_at_woche_atlas=@brg_at_woche_atlas, brg_at_woche_zolaris=@brg_at_woche_zolaris, brg_de_woche_atlas=@brg_de_woche_atlas " &
                                   " WHERE brg_id=@brg_id " &
                            " End " &
                            " Else " &
                            " begin " &
                                "INSERT INTO tblBuergschaft " &
                                "(brg_datum, brg_at_tag, brg_at_woche, brg_de_tag, brg_de_woche, brg_at_tag_atlas, brg_at_tag_zolaris, brg_at_woche_atlas, brg_at_woche_zolaris, brg_de_tag_atlas,brg_de_woche_atlas) VALUES " &
                                "(@brg_datum, @brg_at_tag, @brg_at_woche, @brg_de_tag, @brg_de_woche, @brg_at_tag_atlas, @brg_at_tag_zolaris, @brg_at_woche_atlas, @brg_at_woche_zolaris, @brg_de_tag_atlas, @brg_de_woche_atlas)" &
                            " End " &
                            " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@brg_datum", daten.brg_datum)
                cmd.Parameters.AddWithValue("@brg_at_tag", daten.brg_at_tag)
                cmd.Parameters.AddWithValue("@brg_at_woche", daten.brg_at_woche)
                cmd.Parameters.AddWithValue("@brg_de_tag", daten.brg_de_tag)
                cmd.Parameters.AddWithValue("@brg_de_woche", daten.brg_de_woche)
                cmd.Parameters.AddWithValue("@brg_at_tag_atlas", daten.brg_at_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_at_tag_zolaris", daten.brg_at_tag_zolaris)
                cmd.Parameters.AddWithValue("@brg_at_woche_atlas", daten.brg_at_woche_atlas)
                cmd.Parameters.AddWithValue("@brg_at_woche_zolaris", daten.brg_at_woche_zolaris)
                cmd.Parameters.AddWithValue("@brg_de_tag_atlas", daten.brg_de_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_de_woche_atlas", daten.brg_de_woche_atlas)
                cmd.Parameters.AddWithValue("@brg_id", daten.brg_id)
                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function
    Public Function getBrg(ByVal brg_datum As DateTime) As List(Of cBuergschaft)
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim myCal As Calendar = CultureInfo.InvariantCulture.Calendar
        ' MsgBox(brg_datum.Month & brg_datum.Year)
        Dim von As DateTime = "01" & brg_datum.ToString(".MM.yyyy")
        Dim bis As DateTime = myCal.GetDaysInMonth(brg_datum.Year, brg_datum.Month) & brg_datum.ToString(".MM.yyyy")
        'MsgBox("SELECT * FROM tblBuergschaft WHERE brg_datum <= '" & bis & "' and brg_datum >= '" & von & "'")
        Dim sql As String = "SELECT * FROM tblBuergschaft WHERE brg_datum <= '" & bis & "' and brg_datum >= '" & von & "'"
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    While dr.Read
                        Dim d As New cBuergschaft
                        d.brg_id = dr.Item("brg_id")
                        d.brg_datum = dr.Item("brg_datum")
                        d.brg_at_tag = dr.Item("brg_at_tag")
                        d.brg_at_woche = dr.Item("brg_at_woche")
                        d.brg_de_tag = dr.Item("brg_de_tag")
                        d.brg_de_woche = dr.Item("brg_de_woche")
                        d.brg_at_tag_atlas = dr.Item("brg_at_tag_atlas")
                        d.brg_at_tag_zolaris = dr.Item("brg_at_tag_zolaris")
                        d.brg_at_woche_atlas = dr.Item("brg_at_woche_atlas")
                        d.brg_at_woche_zolaris = dr.Item("brg_at_woche_zolaris")
                        d.brg_de_tag_atlas = dr.Item("brg_de_tag_atlas")
                        d.brg_de_woche_atlas = dr.Item("brg_de_woche_atlas")

                        d.brg_de2_tag = dr.Item("brg_de2_tag")
                        d.brg_de2_woche = dr.Item("brg_de2_woche")
                        d.brg_de2_tag_atlas = dr.Item("brg_de2_tag_atlas")
                        d.brg_de2_woche_atlas = dr.Item("brg_de2_woche_atlas")

                        daten.Add(d)
                    End While
                    Return daten
                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 getBrgVonBis(ByVal von As DateTime, ByVal bis As DateTime) As List(Of cBuergschaft)
        Dim sql As String = "SELECT * FROM tblBuergschaft WHERE brg_datum <= '" & bis & "' and brg_datum >= '" & von & "'"

        Dim dr As SqlDataReader
        Dim daten As New List(Of cBuergschaft)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    While dr.Read
                        Dim d As New cBuergschaft
                        d.brg_id = dr.Item("brg_id")
                        d.brg_datum = dr.Item("brg_datum")
                        d.brg_at_tag = dr.Item("brg_at_tag")
                        d.brg_at_woche = dr.Item("brg_at_woche")
                        d.brg_de_tag = dr.Item("brg_de_tag")
                        d.brg_de_woche = dr.Item("brg_de_woche")
                        d.brg_at_tag_atlas = dr.Item("brg_at_tag_atlas")
                        d.brg_at_tag_zolaris = dr.Item("brg_at_tag_zolaris")
                        d.brg_at_woche_atlas = dr.Item("brg_at_woche_atlas")
                        d.brg_at_woche_zolaris = dr.Item("brg_at_woche_zolaris")
                        d.brg_de_tag_atlas = dr.Item("brg_de_tag_atlas")
                        d.brg_de_woche_atlas = dr.Item("brg_de_woche_atlas")

                        d.brg_de2_tag = dr.Item("brg_de2_tag")
                        d.brg_de2_woche = dr.Item("brg_de2_woche")
                        d.brg_de2_tag_atlas = dr.Item("brg_de2_tag_atlas")
                        d.brg_de2_woche_atlas = dr.Item("brg_de2_woche_atlas")

                        daten.Add(d)
                    End While
                    Return daten
                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 getBrgbyDate(ByVal brg_datum As DateTime) As cBuergschaft

        'MsgBox("SELECT * FROM tblBuergschaft WHERE brg_datum <= '" & bis & "' and brg_datum >= '" & von & "'")
        Dim sql As String = "SELECT * FROM tblBuergschaft WHERE brg_datum = '" & brg_datum & "' "
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                Try
                    If dr.Read Then
                        Dim d As New cBuergschaft
                        d.brg_id = dr.Item("brg_id")
                        d.brg_datum = dr.Item("brg_datum")
                        d.brg_at_tag = dr.Item("brg_at_tag")
                        d.brg_at_woche = dr.Item("brg_at_woche")
                        d.brg_de_tag = dr.Item("brg_de_tag")
                        d.brg_de_woche = dr.Item("brg_de_woche")
                        d.brg_at_tag_atlas = dr.Item("brg_at_tag_atlas")
                        d.brg_at_tag_zolaris = dr.Item("brg_at_tag_zolaris")
                        d.brg_at_woche_atlas = dr.Item("brg_at_woche_atlas")
                        d.brg_at_woche_zolaris = dr.Item("brg_at_woche_zolaris")
                        d.brg_de_tag_atlas = dr.Item("brg_de_tag_atlas")
                        d.brg_de_woche_atlas = dr.Item("brg_de_woche_atlas")


                        d.brg_de2_tag = dr.Item("brg_de2_tag")
                        d.brg_de2_woche = dr.Item("brg_de2_woche")
                        d.brg_de2_tag_atlas = dr.Item("brg_de2_tag_atlas")
                        d.brg_de2_woche_atlas = dr.Item("brg_de2_woche_atlas")

                        Return d
                    End If
                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 setBuergschaft_OLD(ByVal daten As cBuergschaft) As Boolean
        Dim sql = ""
        If daten.brg_id >= 0 Then
            sql = "UPDATE tblBuergschaft " &
                       "SET brg_datum=@brg_datum, brg_at_tag=@brg_at_tag, brg_at_woche=@brg_at_woche, brg_de_tag=@brg_de_tag, brg_de_woche=@brg_de_woche, brg_at_tag_atlas=@brg_at_tag_atlas, brg_at_tag_zolaris=@brg_at_tag_zolaris, brg_at_woche_atlas=@brg_at_woche_atlas, brg_at_woche_zolaris=@brg_at_woche_zolaris, brg_de_tag_atlas=@brg_de_tag_atlas,brg_de_woche_atlas=@brg_de_woche_atlas " &
                       "WHERE brg_id=@brg_id"
        Else
            sql = "INSERT INTO tblBuergschaft " &
                       "(brg_datum, brg_at_tag, brg_at_woche, brg_de_tag, brg_de_woche, brg_at_tag_atlas, brg_at_tag_zolaris, brg_at_woche_atlas, brg_at_woche_zolaris, brg_de_tag_atlas,brg_de_woche_atlas) VALUES " &
                       "(@brg_datum, @brg_at_tag, @brg_at_woche, @brg_de_tag, @brg_de_woche, @brg_at_tag_atlas, @brg_at_tag_zolaris, @brg_at_woche_atlas, @brg_at_woche_zolaris, @brg_de_tag_atlas, @brg_de_woche_atlas)"
        End If

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@brg_datum", daten.brg_datum)
                cmd.Parameters.AddWithValue("@brg_at_tag", daten.brg_at_tag)
                cmd.Parameters.AddWithValue("@brg_at_woche", daten.brg_at_woche)
                cmd.Parameters.AddWithValue("@brg_de_tag", daten.brg_de_tag)
                cmd.Parameters.AddWithValue("@brg_de_woche", daten.brg_de_woche)
                cmd.Parameters.AddWithValue("@brg_at_tag_atlas", daten.brg_at_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_at_tag_zolaris", daten.brg_at_tag_zolaris)
                cmd.Parameters.AddWithValue("@brg_at_woche_atlas", daten.brg_at_woche_atlas)
                cmd.Parameters.AddWithValue("@brg_at_woche_zolaris", daten.brg_at_woche_zolaris)
                cmd.Parameters.AddWithValue("@brg_de_tag_atlas", daten.brg_de_tag_atlas)
                cmd.Parameters.AddWithValue("@brg_de_woche_atlas", daten.brg_de_woche_atlas)
                If daten.brg_id >= 0 Then cmd.Parameters.AddWithValue("@brg_id", daten.brg_id)
                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function
End Class




Public Class cProgramme


    Public ADMIN_VERSION As String
    Public UID_VERSION As String
    Public DISPO_VERSION As String
    Public PARKZEIT_VERSION As String
    Public ZOLL_VERSION As String
    Public SDL_VERSION As String
    Public AVISO_VERSION As String
    Public AVISO_ATILLA_VERSION As String

    Public MONITORING_VERSION As String
    Public DOKU_VERSION As String

    Public ADMIN_ID As String
    Public UID_ID As String
    Public DISPO_ID As String
    Public PARKZEIT_ID As String
    Public ZOLL_ID As String
    Public SDL_ID As String

    Public AVISO_ID As String
    Public AVISO_ATILLA_ID As String
    Public MONITORING_ID As String
    Public DOKU_ID As String


    Public Function AnzeigeProgBer(ByVal id As Integer) As DataTable
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblBerechtigungen WHERE ber_progId=" & id & "", 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 'AnzeigeTabelle'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function getProgrammByCodeName(ByVal codename As String) As cProgramm
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT * FROM tblProgramme WHERE prog_codename=@prog_codename"
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New cProgramm
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@prog_codename", codename)
                dr = cmd.ExecuteReader()
                Try
                    If dr.Read Then
                        daten.prog_id = dr.Item("prog_id")
                        daten.prog_codename = dr.Item("prog_codename")
                        daten.prog_name = dr.Item("prog_name")
                        daten.prog_aktuelleVersion = dr.Item("prog_aktuelleVersion")
                        daten.prog_info = dr.Item("prog_info")
                        daten.prog_source_path = dr.Item("prog_source_path")
                        daten.prog_dev_path = dr.Item("prog_dev_path")
                        daten.prog_prod_path = dr.Item("prog_prod_path")
                        daten.prog_installer_source_path = dr.Item("prog_installer_source_path")
                        daten.prog_installer_dev_path = dr.Item("prog_installer_dev_path")
                        daten.prog_installer_prod_path = dr.Item("prog_installer_prod_path")
                        If Not dr.Item("prog_installer_prod_path_atilla") Is DBNull.Value Then daten.prog_installer_prod_path_atilla = dr.Item("prog_installer_prod_path_atilla")
                        If Not dr.Item("prog_prod_path_atilla") Is DBNull.Value Then daten.prog_prod_path_atilla = dr.Item("prog_prod_path_atilla")
                        If Not dr.Item("prog_prod_path_imex") Is DBNull.Value Then daten.prog_prod_path_imex = dr.Item("prog_prod_path_imex")
                        If Not dr.Item("prog_installer_prod_path_imex") Is DBNull.Value Then daten.prog_installer_prod_path_imex = dr.Item("prog_installer_prod_path_imex")
                        If Not dr.Item("prog_prod_path_SBG") Is DBNull.Value Then daten.prog_prod_path_SBG = dr.Item("prog_prod_path_SBG")
                        If Not dr.Item("prog_installer_prod_path_SBG") Is DBNull.Value Then daten.prog_installer_prod_path_SBG = dr.Item("prog_installer_prod_path_SBG")
                        If Not dr.Item("prog_prod_path_WAI") Is DBNull.Value Then daten.prog_prod_path_WAI = dr.Item("prog_prod_path_WAI")
                        If Not dr.Item("prog_installer_prod_path_WAI") Is DBNull.Value Then daten.prog_installer_prod_path_WAI = dr.Item("prog_installer_prod_path_WAI")
                        If Not dr.Item("prog_prod_path_NKD") Is DBNull.Value Then daten.prog_prod_path_NKD = dr.Item("prog_prod_path_NKD")
                        If Not dr.Item("prog_installer_prod_path_NKD") Is DBNull.Value Then daten.prog_installer_prod_path_NKD = dr.Item("prog_installer_prod_path_NKD")
                        If Not dr.Item("prog_prod_path_NEU") Is DBNull.Value Then daten.prog_prod_path_NEU = dr.Item("prog_prod_path_NEU")
                        If Not dr.Item("prog_installer_prod_path_NEU") Is DBNull.Value Then daten.prog_installer_prod_path_NEU = dr.Item("prog_installer_prod_path_NEU")
                        If Not dr.Item("prog_prod_path_UNI") Is DBNull.Value Then daten.prog_prod_path_UNI = dr.Item("prog_prod_path_UNI")
                        If Not dr.Item("prog_installer_prod_path_UNI") Is DBNull.Value Then daten.prog_installer_prod_path_UNI = dr.Item("prog_installer_prod_path_UNI")
                        If Not dr.Item("prog_prod_path_AMB") Is DBNull.Value Then daten.prog_prod_path_AMB = dr.Item("prog_prod_path_AMB")
                        If Not dr.Item("prog_installer_prod_path_AMB") Is DBNull.Value Then daten.prog_installer_prod_path_AMB = dr.Item("prog_installer_prod_path_AMB")
                        If Not dr.Item("prog_prod_path_backup") Is DBNull.Value Then daten.prog_prod_path_Backup = dr.Item("prog_prod_path_backup")

                        Return daten
                    End If
                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 insertProgramm(ByVal daten As cProgramm) As Boolean
        Dim sql = "INSERT INTO tblProgramme " &
                       "(prog_codename, prog_name, prog_aktuelleVersion, prog_info, prog_source_path, prog_dev_path,prog_prod_path,prog_installer_source_path,prog_installer_dev_path,prog_installer_prod_path,prog_installer_prod_path_atilla,prog_prod_path_atilla,prog_prod_path_imex,prog_installer_prod_path_imex,prog_prod_path_SBG,prog_installer_prod_path_SBG,prog_prod_path_WAI,prog_installer_prod_path_WAI,prog_prod_path_NKD,prog_installer_prod_path_NKD,prog_prod_path_NEU,prog_installer_prod_path_NEU,prog_installer_prod_path_UNI,prog_installer_prod_path_AMB) VALUES " &
                       "(@prog_codename,@prog_name,@prog_aktuelleVersion,@prog_info,@prog_source_path,@prog_dev_path,@prog_prod_path,@prog_installer_source_path,@prog_installer_dev_path,@prog_installer_prod_path,@prog_installer_prod_path_atilla,@prog_prod_path_atilla,@prog_prod_path_imex,@prog_installer_prod_path_imex,@prog_prod_path_SBG,@prog_installer_prod_path_SBG,@prog_prod_path_WAI,@prog_installer_prod_path_WAI,@prog_prod_path_NKD,@prog_installer_prod_path_NKD,@prog_prod_path_NEU,@prog_installer_prod_path_NEU,@prog_installer_prod_path_UNI,@prog_installer_prod_path_AMB)"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@prog_codename", daten.prog_codename)
                cmd.Parameters.AddWithValue("@prog_name", daten.prog_name)
                cmd.Parameters.AddWithValue("@prog_aktuelleVersion", daten.prog_aktuelleVersion)
                cmd.Parameters.AddWithValue("@prog_info", daten.prog_info)
                cmd.Parameters.AddWithValue("@prog_source_path", daten.prog_source_path)
                cmd.Parameters.AddWithValue("@prog_dev_path", daten.prog_dev_path)
                cmd.Parameters.AddWithValue("@prog_prod_path", daten.prog_prod_path)
                cmd.Parameters.AddWithValue("@prog_installer_source_path", daten.prog_installer_source_path)
                cmd.Parameters.AddWithValue("@prog_installer_dev_path", daten.prog_installer_dev_path)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path", daten.prog_installer_prod_path)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_atilla", daten.prog_installer_prod_path_atilla)
                cmd.Parameters.AddWithValue("@prog_prod_path_atilla", daten.prog_prod_path_atilla)
                cmd.Parameters.AddWithValue("@prog_prod_path_imex", daten.prog_prod_path_imex)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_imex", daten.prog_installer_prod_path_imex)
                cmd.Parameters.AddWithValue("@prog_prod_path_SBG", daten.prog_prod_path_SBG)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_SBG", daten.prog_installer_prod_path_SBG)
                cmd.Parameters.AddWithValue("@prog_prod_path_WAI", daten.prog_prod_path_WAI)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_WAI", daten.prog_installer_prod_path_WAI)
                cmd.Parameters.AddWithValue("@prog_prod_path_NKD", daten.prog_prod_path_NKD)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_NKD", daten.prog_installer_prod_path_NKD)
                cmd.Parameters.AddWithValue("@prog_prod_path_NEU", daten.prog_prod_path_NEU)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_NEU", daten.prog_installer_prod_path_NEU)

                cmd.Parameters.AddWithValue("@prog_prod_path_UNI", daten.prog_prod_path_UNI)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_UNI", daten.prog_installer_prod_path_UNI)
                cmd.Parameters.AddWithValue("@prog_prod_path_AMB", daten.prog_prod_path_AMB)
                cmd.Parameters.AddWithValue("@prog_installer_prod_path_AMB", daten.prog_installer_prod_path_AMB)


                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function
    Public Function getProgrammVersions() As Boolean
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT prog_id, prog_codename, prog_aktuelleVersion FROM tblProgramme"
        '  Dim daten As New List(Of cEntry)
        Dim dr As SqlDataReader
        Dim daten As New cProgramm
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                dr = cmd.ExecuteReader()
                While dr.Read
                    Try
                        If dr.Item("prog_codename") = "ADMIN" Then ADMIN_VERSION = dr.Item("prog_aktuelleVersion") : ADMIN_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "UID" Then UID_VERSION = dr.Item("prog_aktuelleVersion") : UID_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "DISPO" Then DISPO_VERSION = dr.Item("prog_aktuelleVersion") : DISPO_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "PARKZEIT" Then PARKZEIT_VERSION = dr.Item("prog_aktuelleVersion") : PARKZEIT_ID = dr.Item("prog_id")

                        If dr.Item("prog_codename") = "ZOLL" Then ZOLL_VERSION = dr.Item("prog_aktuelleVersion") : ZOLL_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "SDL" Then SDL_VERSION = dr.Item("prog_aktuelleVersion") : SDL_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "AVISO_NEW" Then AVISO_VERSION = dr.Item("prog_aktuelleVersion") : AVISO_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "AVISO_ATILLA" Then AVISO_ATILLA_VERSION = dr.Item("prog_aktuelleVersion") : AVISO_ATILLA_ID = dr.Item("prog_id")

                        If dr.Item("prog_codename") = "MONITORING" Then MONITORING_VERSION = dr.Item("prog_aktuelleVersion") : MONITORING_ID = dr.Item("prog_id")
                        If dr.Item("prog_codename") = "DOKU" Then DOKU_VERSION = dr.Item("prog_aktuelleVersion") : DOKU_ID = dr.Item("prog_id")

                    Catch ex As Exception
                        MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
                    End Try
                End While
                Return True
                dr.Close()
            End Using
        End Using
        Return False
    End Function
    Public Function setProgrammVersion(ByVal codename As String, ByVal aktuelleVersion As String) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "UPDATE tblProgramme " &
                       "SET prog_aktuelleVersion=@prog_aktuelleVersion " &
                       "WHERE prog_codename=@prog_codename"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@prog_aktuelleVersion", aktuelleVersion)
                cmd.Parameters.AddWithValue("@prog_codename", codename)
                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 setParam(ByVal param As String, ByVal wert As String, ByVal codename As String) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "UPDATE tblProgramme " &
                       "SET " & param & "=@param " &
                       "WHERE prog_codename=@prog_codename"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@param", wert)
                cmd.Parameters.AddWithValue("@prog_codename", codename)
                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
End Class

Public Class cOptionenDAL

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




    Public Function getParameter(bezeichnung) As Object
        Dim sql As String = "SELECT wert FROM tblParameter WHERE bezeichnung=@bezeichnung"
        Dim dr As SqlDataReader
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bezeichnung", bezeichnung)
                dr = cmd.ExecuteReader()
                Try
                    If dr.Read Then Return dr.Item("wert")
                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 setParameter(bezeichnung, wert) As Object
        Dim sql As String = " begin tran" &
                          " if exists (select * from tblParameter with (updlock,serializable) where bezeichnung = @bezeichnung ) " &
                                 " begin " &
                                 " UPDATE tblParameter " &
                                 " SET wert=@wert " &
                                 " WHERE bezeichnung=@bezeichnung " &
                              " End " &
                          " Else " &
                          " begin " &
                               "INSERT INTO tblParameter " &
                               "( wert, bezeichnung ) VALUES " &
                               "( @wert, @bezeichnung ) " &
                          " End " &
                          " commit tran "
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bezeichnung", bezeichnung)
                cmd.Parameters.AddWithValue("@wert", wert)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Parameter kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Data")
                End Try
            End Using
        End Using
        Return False
    End Function



    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 = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@codename", "ADMIN")
                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



    Public Function getAdminLogin(ByVal user As String) As String
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT mit_id,mit_vname,mit_nname,mit_firma FROM tblMitarbeiter where mit_username=@mit_username  "
        'Dim daten As New List(Of cEntry)
        Dim dr


        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@mit_username", user)
                    dr = cmd.ExecuteReader()


                    'If dr.Read Then USERBER = dr.Item("mit_berechtigung") : USRNAME = dr.Item("mit_vname") & " " & dr.Item("mit_nname") : Return True
                    If dr.Read Then USRNAME = dr.Item("mit_vname") & " " & dr.Item("mit_nname") : USRID = dr.Item("mit_id") : USRFirma = dr.item("mit_firma") : Return True
                End Using
            End Using
            dr.Close()
        Catch ex As Exception
            MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler mit der Datenbankverbindung:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
            Return False
        End Try

        Return False
    End Function

    Public Function checkLogin(ByVal user As String, ByVal pwd As String) As String
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sqlstr As String = "SELECT mit_id,mit_vname,mit_nname,mit_firma,mit_username,mit_firmaFMZoll" &
            " FROM tblMitarbeiter,tblBerProg WHERE  bepr_programm='ADMIN' AND  bepr_userId=mit_id AND mit_username=@mit_username and mit_pwd=@mit_pwd COLLATE Latin1_General_CS_AS "
        'Dim daten As New List(Of cEntry)
        Dim dr

        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sqlstr, conn)
                    cmd.Parameters.AddWithValue("@mit_username", user)
                    cmd.Parameters.AddWithValue("@mit_pwd", pwd)
                    dr = cmd.ExecuteReader()


                    'If dr.Read Then USERBER = dr.Item("mit_berechtigung") : USRNAME = dr.Item("mit_vname") & " " & dr.Item("mit_nname") : Return True
                    If dr.Read Then
                        USRNAME = dr.Item("mit_vname") & " " & dr.Item("mit_nname")

                        USRID = dr.Item("mit_id")
                        USRFirma = dr.Item("mit_firma")
                        '    USERBER = dr.Item("ber_sicherheitsstufe")


                        VERAG_PROG_ALLGEMEIN.cAllgemein.PROGNAME = "ADMIN_TOOLS"
                        VERAG_PROG_ALLGEMEIN.cAllgemein.PROGID = 1
                        VERAG_PROG_ALLGEMEIN.cAllgemein.PROGVERSION = Application.ProductVersion

                        'VERAG_PROG_ALLGEMEIN.cAllgemein.USRBER = CInt(dr.Item("ber_sicherheitsstufe"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.USRID = CInt(dr.Item("mit_id"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.USR_VNAME = CStr(dr.Item("mit_vname"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.USR_NNAME = CStr(dr.Item("mit_nname"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME = CStr(dr.Item("mit_vname")) & " " & CStr(dr.Item("mit_nname"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME = CStr(dr.Item("mit_username"))
                        If dr.Item("mit_firmaFMZoll") Is DBNull.Value Then
                            MsgBox("Benutzer muss einer Firma zugeordnet sein!")
                            Return False
                        End If
                        'Ju18WA10
                        'VERAG_PROG_ALLGEMEIN.cAllgemein.NIEDERLASSUNG = CInt(dr.Item("mit_firmaFMZoll"))
                        ' VERAG_PROG_ALLGEMEIN.cAllgemein.ABTEILUNG = CInt(dr.Item("mit_firmaFMZoll"))

                        VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA_ID = CInt(dr.Item("mit_firmaFMZoll"))
                        VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA_NAME = SQL.getValueTxtBySql("SELECT Firma_Bez FROM tblFirma WHERE Firma_ID= " & VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA_ID, "FMZOLL")
                        VERAG_PROG_ALLGEMEIN.cAllgemein.LOAD_DATA()
                        VERAG_PROG_ALLGEMEIN.cAllgemein.LOAD_BER()

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

        Return False
    End Function


    Public Function AnzeigeTabelle(hSQL As String) As DataTable
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(hSQL, conn)
                        Dim dr = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                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 'AnzeigeTabelle'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function getDataMitarb() As IList(Of VERAG_PROG_ALLGEMEIN.cMitarbeiter)
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT mit_id,mit_username,mit_abweichenderAnzeigename,mit_vname, mit_nname ,mit_niederlassung,mit_abteilung,mit_email,mit_durchwahl,mit_gekuendigt FROM tblUser "
        Dim daten As New List(Of VERAG_PROG_ALLGEMEIN.cMitarbeiter)
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sql, conn)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read
                            'daten = New cEntry()
                            'MsgBox(daten.Count)
                            Try
                                Dim d As New VERAG_PROG_ALLGEMEIN.cMitarbeiter

                                d.mit_id = dr.Item("mit_id")
                                d.mit_username = dr.Item("mit_username")
                                d.mit_abweichenderAnzeigename = dr.Item("mit_abweichenderAnzeigename")
                                '  d.mit_pwd = dr.Item("mit_pwd")
                                '    d.mit_affix = dr.Item("mit_affix")
                                d.mit_vname = dr.Item("mit_vname")
                                d.mit_nname = dr.Item("mit_nname")
                                '   d.mit_gebdat = dr.Item("mit_gebdat")
                                d.mit_niederlassung = dr.Item("mit_niederlassung")
                                d.mit_abteilung = dr.Item("mit_abteilung")
                                '  d.mit_position = dr.Item("mit_position")
                                d.mit_durchwahl = dr.Item("mit_durchwahl")
                                d.mit_email = dr.Item("mit_email")
                                '  d.mit_mobiltel = dr.Item("mit_mobiltel")
                                '   d.mit_info = dr.Item("mit_info")
                                '   d.mit_einstiegsdatum = dr.Item("mit_einstiegsdatum")
                                d.mit_gekuendigt = dr.Item("mit_gekuendigt")
                                '   d.mit_kuendigungsdatum = dr.Item("mit_kuendigungsdatum")
                                '  d.mit_foto = dr.Item("mit_foto")

                                daten.Add(d)
                            Catch ex As Exception
                                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler beim Einlesen der Optionen" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
                            End Try
                        End While
                    End If
                    dr.Close()
                End Using
            End Using
            Return daten
        Catch ex As Exception
            MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler beim Lesen der Optionen:" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Datenbankfehler")
            Return Nothing
        End Try
    End Function

End Class

Public Class cMitarbeiter


    Public Function loadCboBySqlWithListItem(ByVal sql As String, Optional showValueInText As Boolean = True, Optional conn_art As String = "AVISO") As List(Of VERAG_PROG_ALLGEMEIN.MyListItem)
        Dim o As New List(Of VERAG_PROG_ALLGEMEIN.MyListItem)
        Try
            Dim conn As New SqlConnection
            Select Case conn_art
                Case "ADMIN" : conn = cSqlDb.GetNewOpenConnection()
                Case "AVISO" : conn = cSqlDb.GetNewOpenConnection()
                Case "FMZOLL" : conn = cSqlDb.GetNewOpenConnectionFMZOLL
            End Select

            Using cmd As New SqlCommand(sql, conn)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                While dr.Read
                    If showValueInText Then
                        o.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(CStr(dr.Item(0)) & vbTab & " - " & CStr(dr.Item(1)), CStr(dr.Item(0))))
                    Else
                        Try
                            o.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(CStr(dr.Item(1)), CStr(dr.Item(0))))
                        Catch ex As Exception
                            o.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(CStr(dr.Item(0)), CStr(dr.Item(0))))
                        End Try
                    End If

                End While
                dr.Close()
            End Using
            conn.Close()

            Return o  'While Schleife wird hier verlassen
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message)
        End Try

        Return Nothing
    End Function

    Public Function AnzeigeTabelle(ByVal sql As String) As DataTable
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                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 'AnzeigeTabelle'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function


    Public Function AnzeigeTabelleFirma(sql) As DataTable
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                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 'AnzeigeTabelle'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function getUserBerechtigungen(ByRef id As Integer) As IList(Of cUserBerechtigungen)
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT ber_id,ber_name,ber_sicherheitsstufe,bepr_programm FROM tblBerechtigungen, tblBerProg WHERE bepr_userId= '" & id & "' AND (bepr_berId=ber_id   OR bepr_berId is null) " 'AND tblBerProg.bepr_berId= '" & id & "' "
        Dim daten As New List(Of cUserBerechtigungen)
        Dim i As Integer = 0
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sql, conn)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read
                            Try
                                Dim d = New cUserBerechtigungen
                                If Not IsDBNull(dr.Item("ber_id")) Then d.ber_id = dr.Item("ber_id")
                                If Not IsDBNull(dr.Item("ber_name")) Then d.ber_name = dr.Item("ber_name")
                                If Not IsDBNull(dr.Item("ber_sicherheitsstufe")) Then d.ber_sicherheitsstufe = dr.Item("ber_sicherheitsstufe")
                                If Not IsDBNull(dr.Item("bepr_programm")) Then d.bepr_programm = dr.Item("bepr_programm")
                                daten.Add(d)
                            Catch ex As Exception
                                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler beim Einlesen der UserBerechtigungen" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
                            End Try
                            i = i + 1
                        End While

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

    Public Function getUserBerechtigungenNEU(ByRef id As Integer) As IList(Of cUserBerechtigungen)
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT [bepr_id],[bepr_userId],[bepr_berId],[bepr_programm],[ber_allowed]  FROM [tblBerProg] WHERE bepr_userId= '" & id & "'  and bepr_berId is null " 'AND tblBerProg.bepr_berId= '" & id & "' "
        Dim daten As New List(Of cUserBerechtigungen)
        Dim i As Integer = 0
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sql, conn)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read
                            Try
                                Dim d = New cUserBerechtigungen
                                d.ber_id = Nothing
                                d.ber_name = ""
                                d.ber_sicherheitsstufe = 0
                                If Not IsDBNull(dr.Item("bepr_programm")) Then d.bepr_programm = dr.Item("bepr_programm")
                                daten.Add(d)
                            Catch ex As Exception
                                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler beim Einlesen der UserBerechtigungen" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
                            End Try
                            i = i + 1
                        End While

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

    Public Function insertBerechtigungProg(ByVal bepr_userId As String, ByVal bepr_berId As String, ByVal bepr_programm As String, ber_allowed As Boolean) As Boolean
        Dim sql = "INSERT INTO tblBerProg " &
                       "(bepr_userId, bepr_berId, bepr_programm,ber_allowed) VALUES " &
                       "(@bepr_userId, @bepr_berId, @bepr_programm,@ber_allowed)"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bepr_userId", bepr_userId)
                cmd.Parameters.AddWithValue("@bepr_berId", If(bepr_berId, DBNull.Value))
                cmd.Parameters.AddWithValue("@bepr_programm", bepr_programm)
                cmd.Parameters.AddWithValue("@ber_allowed", ber_allowed)
                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
    End Function



    Public Function insertUpdateBerechtigungProg(ByVal bepr_userId As String, ByVal bepr_berId As String, ByVal bepr_programm As String, ber_allowed As Boolean) As Boolean
        Dim sql = " BEGIN TRAN IF EXISTS(SELECT * FROM [tblBerProg] WITH(updlock,serializable) WHERE bepr_userId=@bepr_userId AND bepr_programm=@bepr_programm) " &
                     " BEGIN " &
                             " UPDATE [tblBerProg] " &
                               "SET bepr_berId=@bepr_berId " &
                               "WHERE bepr_userId=@bepr_userId AND bepr_programm=@bepr_programm" &
                     " END " &
                     " Else " &
                     " BEGIN " &
                           " INSERT INTO [tblBerProg] " &
                               "(bepr_userId, bepr_berId, bepr_programm,ber_allowed) VALUES " &
                               "(@bepr_userId, @bepr_berId, @bepr_programm,@ber_allowed)" &
                    " END " &
                  " commit tran "
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bepr_userId", bepr_userId)
                cmd.Parameters.AddWithValue("@bepr_berId", If(bepr_berId, DBNull.Value))
                cmd.Parameters.AddWithValue("@bepr_programm", bepr_programm)
                cmd.Parameters.AddWithValue("@ber_allowed", ber_allowed)
                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
    End Function
    Public Function deleteBerechtigungProg(ByVal bepr_userId As String, ByVal bepr_programm As String) As Boolean
        Dim sql = "DELETE FROM tblBerProg " &
                   "WHERE bepr_userId=@bepr_userId AND bepr_programm=@bepr_programm "
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bepr_userId", bepr_userId)
                cmd.Parameters.AddWithValue("@bepr_programm", bepr_programm)
                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
    End Function
    Public Function updateBerechtigungProg(ByVal bepr_userId As String, ByVal bepr_berId As String, ByVal bepr_programm As String) As Boolean
        Dim sql = "UPDATE tblBerProg " &
                       "SET bepr_berId=@bepr_berId " &
                       "WHERE bepr_userId=@bepr_userId AND bepr_programm=@bepr_programm"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@bepr_berId", bepr_berId)
                cmd.Parameters.AddWithValue("@bepr_userId", bepr_userId)
                cmd.Parameters.AddWithValue("@bepr_programm", bepr_programm)
                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
    End Function

    Public Function getBerechtigungen() As IList(Of cBerechtigungen)
        'falls ID mitgegeben, dann diese laden, ansonsten den anderen Wert suchen
        Dim sql As String = "SELECT ber_name,ber_sicherheitsstufe,ber_progId,ber_id,prog_codename  FROM tblBerechtigungen,tblProgramme WHERE ber_progId=prog_id ORDER BY prog_codename ASC, ber_sicherheitsstufe ASC"
        Dim daten As New List(Of cBerechtigungen)
        Dim i As Integer = 0
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sql, conn)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read
                            Try
                                Dim d = New cBerechtigungen
                                If Not IsDBNull(dr.Item("ber_name")) Then d.ber_name = dr.Item("ber_name")
                                If Not IsDBNull(dr.Item("ber_sicherheitsstufe")) Then d.ber_sicherheitsstufe = dr.Item("ber_sicherheitsstufe")
                                If Not IsDBNull(dr.Item("ber_progId")) Then d.ber_progId = dr.Item("ber_progId")
                                If Not IsDBNull(dr.Item("prog_codename")) Then d.prog_codename = dr.Item("prog_codename")
                                If Not IsDBNull(dr.Item("ber_id")) Then d.ber_id = dr.Item("ber_id")
                                daten.Add(d)
                            Catch ex As Exception
                                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Fehler beim Einlesen der Berechtigungen " & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Exclamation)
                            End Try
                            i = i + 1
                        End While

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

    Public Function getMitarbeiterById(ByVal id As Integer) As VERAG_PROG_ALLGEMEIN.cMitarbeiter
        Dim m = New VERAG_PROG_ALLGEMEIN.cMitarbeiter
        If m.LOAD(id) Then
            Return m
        End If
        Return Nothing
    End Function

    Public Function getMitarbeiterByTimasId(ByVal id As Integer) As VERAG_PROG_ALLGEMEIN.cMitarbeiter
        Dim m = New VERAG_PROG_ALLGEMEIN.cMitarbeiter
        If m.LOAD(id, True) Then
            Return m
        End If
        Return Nothing
    End Function


    Public Function insertMitarbeiter(ByVal m As VERAG_PROG_ALLGEMEIN.cMitarbeiter, ByRef mid As Integer) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "INSERT INTO tblMitarbeiter " &
                       "(mit_username,mit_abweichenderAnzeigename, mit_pwd, mit_geschlecht, mit_affix, mit_vname, mit_nname, mit_gebdat, mit_strasse,mit_hausnr,mit_land,mit_plz,mit_ort,mit_niederlassung, mit_abteilung, mit_position, mit_durchwahl, mit_email, mit_mobiltel, mit_allowAnruf,mit_info, mit_einstiegsdatum, mit_gekuendigt, mit_kuendigungsdatum, mit_foto,mit_firma,mit_firmaFMZoll,mit_FMZollPersID,mit_FMZollPersIDKasseIsnotSpedBuch,mit_FMZollPersIDSpedBuch,mit_standartFiliale,mit_DpUserStandardDP,mit_EZOLL_SB,mit_ATLAS_SB,mit_FirmenDatenAnzeige,mit_DAKOSY_SB,mit_DAKOSY_SBFull,mit_DpUserStandardFirma,mit_DpUserStandardFirma2,mit_TESTonly,mit_PasswortChanged,mit_TELOTEC_SB,mit_AliasAD_Domain,mit_AliasAD_Username,mit_AliasAD_Domain2,mit_AliasAD_Username2,mit_teamId,mit_ChatBenutzer,mit_FirmaHaupt,mit_ChatEasteregg,mit_MAVerzeichnis_SettingsTstmp,mit_MAVerzeichnis_allowGebDat,mit_MAVerzeichnis_allowWohnadresse,mit_MAVerzeichnis_allowGebDatOhneJahr,mit_MAVerzeichnis_Spitzname,mit_foto_DocId,mit_MAVerzeichnis_Motto,mit_sprache,mit_fakturierungsgruppe, mit_CSuser, mit_CSpwd, mit_PersonalNr, mit_telefonnr,mit_ATGRNAccessCode_AT51FP7,mit_ATGRNAccessCode_AT52VA6 , mit_Sprachkentnisse,mit_EZOLL_SB_UNISPED, mit_homeoffice,mit_timasNichtStempeln,mit_emailprivat, mit_timasMehrereFirmen, mit_timasFirmen, mit_Funktion, mit_EMailbenachrichtigungen) OUTPUT INSERTED.mit_id VALUES " &
                       "(@mit_username,@mit_abweichenderAnzeigename,@mit_pwd,@mit_geschlecht,@mit_affix,@mit_vname,@mit_nname,@mit_gebdat,@mit_strasse,@mit_hausnr,@mit_land,@mit_plz,@mit_ort,@mit_niederlassung,@mit_abteilung,@mit_position,@mit_durchwahl,@mit_email,@mit_mobiltel,@mit_allowAnruf,@mit_info,@mit_einstiegsdatum,@mit_gekuendigt,@mit_kuendigungsdatum,@mit_foto,@mit_firma,@mit_firmaFMZoll,@mit_FMZollPersID,@mit_FMZollPersIDKasseIsnotSpedBuch,@mit_FMZollPersIDSpedBuch,@mit_standartFiliale,@mit_DpUserStandardDP,@mit_EZOLL_SB,@mit_ATLAS_SB,@mit_FirmenDatenAnzeige,@mit_DAKOSY_SB,@mit_DAKOSY_SBFull,@mit_DpUserStandardFirma,@mit_DpUserStandardFirma2,@mit_TESTonly,@mit_PasswortChanged,@mit_TELOTEC_SB,@mit_AliasAD_Domain,@mit_AliasAD_Username,@mit_AliasAD_Domain2,@mit_AliasAD_Username2,@mit_teamId,@mit_ChatBenutzer,@mit_FirmaHaupt,@mit_ChatEasteregg,@mit_MAVerzeichnis_SettingsTstmp,@mit_MAVerzeichnis_allowGebDat,@mit_MAVerzeichnis_allowWohnadresse,@mit_MAVerzeichnis_allowGebDatOhneJahr,@mit_MAVerzeichnis_Spitzname,@mit_foto_DocId,@mit_MAVerzeichnis_Motto ,@mit_sprache,@mit_fakturierungsgruppe, @mit_CSuser, @mit_CSpwd, @mit_PersonalNr, @mit_telefonnr,@mit_ATGRNAccessCode_AT51FP7 ,@mit_ATGRNAccessCode_AT52VA6, @mit_Sprachkentnisse,@mit_EZOLL_SB_UNISPED, @mit_homeoffice,@mit_timasNichtStempeln, @mit_emailprivat, @mit_timasMehrereFirmen, @mit_timasFirmen,@mit_Funktion, @mit_EMailbenachrichtigungen )"





        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@mit_username", m.mit_username)
                cmd.Parameters.AddWithValue("@mit_abweichenderAnzeigename", m.mit_abweichenderAnzeigename)
                cmd.Parameters.AddWithValue("@mit_pwd", m.mit_pwd)
                cmd.Parameters.AddWithValue("@mit_geschlecht", m.mit_geschlecht)
                cmd.Parameters.AddWithValue("@mit_affix", m.mit_affix)
                cmd.Parameters.AddWithValue("@mit_vname", m.mit_vname)
                cmd.Parameters.AddWithValue("@mit_nname", m.mit_nname)
                If m.mit_gebdat <> "" Then cmd.Parameters.AddWithValue("@mit_gebdat", m.mit_gebdat) Else cmd.Parameters.AddWithValue("@mit_gebdat", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_strasse", m.mit_strasse)
                cmd.Parameters.AddWithValue("@mit_hausnr", m.mit_hausnr)
                cmd.Parameters.AddWithValue("@mit_land", m.mit_land)
                cmd.Parameters.AddWithValue("@mit_plz", m.mit_plz)
                cmd.Parameters.AddWithValue("@mit_ort", m.mit_ort)
                cmd.Parameters.AddWithValue("@mit_niederlassung", m.mit_niederlassung)
                cmd.Parameters.AddWithValue("@mit_abteilung", m.mit_abteilung)
                cmd.Parameters.AddWithValue("@mit_position", m.mit_position)
                cmd.Parameters.AddWithValue("@mit_durchwahl", m.mit_durchwahl)
                cmd.Parameters.AddWithValue("@mit_email", m.mit_email)
                cmd.Parameters.AddWithValue("@mit_mobiltel", m.mit_mobiltel)
                cmd.Parameters.AddWithValue("@mit_allowAnruf", m.mit_allowAnruf)
                cmd.Parameters.AddWithValue("@mit_FMZollPersID", m.mit_FMZollPersID)
                cmd.Parameters.AddWithValue("@mit_FMZollPersIDSpedBuch", If(m.mit_FMZollPersIDSpedBuch, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_FMZollPersIDKasseIsnotSpedBuch", m.mit_FMZollPersIDKasseIsnotSpedBuch)
                cmd.Parameters.AddWithValue("@mit_standartFiliale", m.mit_standartFiliale)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardDP", m.mit_DpUserStandardDP)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardFirma", m.mit_DpUserStandardFirma)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardFirma2", m.mit_DpUserStandardFirma2)
                cmd.Parameters.AddWithValue("@mit_info", m.mit_info)
                If m.mit_einstiegsdatum <> "" Then cmd.Parameters.AddWithValue("@mit_einstiegsdatum", m.mit_einstiegsdatum) Else cmd.Parameters.AddWithValue("@mit_einstiegsdatum", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_gekuendigt", m.mit_gekuendigt)
                If m.mit_kuendigungsdatum <> "" Then cmd.Parameters.AddWithValue("@mit_kuendigungsdatum", m.mit_kuendigungsdatum) Else cmd.Parameters.AddWithValue("@mit_kuendigungsdatum", DBNull.Value)
                If m.mit_foto <> "" Then cmd.Parameters.AddWithValue("@mit_foto", m.mit_foto) Else cmd.Parameters.AddWithValue("@mit_foto", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_firma", m.mit_firma)
                cmd.Parameters.AddWithValue("@mit_firmaFMZoll", If(m.mit_firmaFMZoll, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_EZOLL_SB", If(m.mit_EZOLL_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_EZOLL_SB_UNISPED", If(m.mit_EZOLL_SB_UNISPED, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATLAS_SB", If(m.mit_ATLAS_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_DAKOSY_SB", If(m.mit_DAKOSY_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_DAKOSY_SBFull", If(m.mit_DAKOSY_SBFull, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_FirmenDatenAnzeige", If(m.mit_FirmenDatenAnzeige, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_TESTonly", m.mit_TESTonly)
                cmd.Parameters.AddWithValue("@mit_PasswortChanged", If(m.mit_PasswortChanged, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_TELOTEC_SB", If(m.mit_TELOTEC_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Domain", If(m.mit_AliasAD_Domain, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Username", If(m.mit_AliasAD_Username, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Domain2", If(m.mit_AliasAD_Domain2, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Username2", If(m.mit_AliasAD_Username2, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_teamId", If(m.mit_teamId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ChatBenutzer", m.mit_ChatBenutzer)
                cmd.Parameters.AddWithValue("@mit_FirmaHaupt", If(m.mit_FirmaHaupt, DBNull.Value))


                cmd.Parameters.AddWithValue("@mit_ChatEasteregg", m.mit_ChatEasteregg)
                cmd.Parameters.AddWithValue("@mit_ChatEasteregg2", m.mit_ChatEasteregg2)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_SettingsTstmp ", If(m.mit_MAVerzeichnis_SettingsTstmp, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowGebDat ", m.mit_MAVerzeichnis_allowGebDat)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowWohnadresse ", m.mit_MAVerzeichnis_allowWohnadresse)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowGebDatOhneJahr ", m.mit_MAVerzeichnis_allowGebDatOhneJahr)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_Spitzname ", If(m.mit_MAVerzeichnis_Spitzname, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_foto_DocId", If(m.mit_foto_DocId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_Motto", If(m.mit_MAVerzeichnis_Motto, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_sprache", m.mit_sprache)
                cmd.Parameters.AddWithValue("@mit_fakturierungsgruppe", m.mit_fakturierungsgruppe)
                cmd.Parameters.AddWithValue("@mit_CSuser", If(m.mit_CSuser, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_CSpwd", If(m.mit_CSpwd, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_id", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_PersonalNr", If(m.mit_PersonalNr, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_telefonnr", If(m.mit_telefonnr, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATGRNAccessCode_AT51FP7 ", If(m.mit_ATGRNAccessCode_AT51FP7, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATGRNAccessCode_AT52VA6 ", If(m.mit_ATGRNAccessCode_AT52VA6, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_Sprachkentnisse ", If(m.mit_Sprachkentnisse, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_homeoffice ", m.mit_homeoffice)
                cmd.Parameters.AddWithValue("@mit_timasNichtStempeln ", m.mit_timasNichtStempeln)
                cmd.Parameters.AddWithValue("@mit_emailprivat", m.mit_emailprivat)
                cmd.Parameters.AddWithValue("@mit_timasMehrereFirmen", m.mit_timasMehrereFirmen)
                cmd.Parameters.AddWithValue("@mit_timasFirmen", If(m.mit_timasFirmen, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_Funktion", If(m.mit_Funktion, DBNull.Value))
                'cmd.Parameters.AddWithValue("@mit_terminalTimasId", If(m.mit_terminalTimasId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_EMailbenachrichtigungen", If(m.mit_EMailbenachrichtigungen, DBNull.Value))

                Try
                    mid = cmd.ExecuteScalar()
                    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
    End Function


    Public Function updateMitarbeiter(ByVal m As VERAG_PROG_ALLGEMEIN.cMitarbeiter) As Boolean
        Dim hAuswahl As String = ""

        Dim sql = "UPDATE tblMitarbeiter " &
                     "SET mit_username=@mit_username, mit_abweichenderAnzeigename=@mit_abweichenderAnzeigename,mit_pwd=@mit_pwd, mit_geschlecht=@mit_geschlecht, mit_affix=@mit_affix, mit_vname=@mit_vname, mit_nname=@mit_nname, mit_gebdat=@mit_gebdat, mit_strasse=@mit_strasse, mit_hausnr=@mit_hausnr, mit_land=@mit_land, mit_plz=@mit_plz, mit_ort=@mit_ort, mit_niederlassung=@mit_niederlassung, mit_abteilung=@mit_abteilung, mit_position=@mit_position, mit_durchwahl=@mit_durchwahl, mit_email=@mit_email, mit_mobiltel=@mit_mobiltel, mit_allowAnruf=@mit_allowAnruf, mit_info=@mit_info, mit_einstiegsdatum=@mit_einstiegsdatum, mit_gekuendigt=@mit_gekuendigt, mit_kuendigungsdatum=@mit_kuendigungsdatum, mit_foto=@mit_foto, mit_firma=@mit_firma,mit_firmaFMZoll=@mit_firmaFMZoll,mit_FMZollPersID=@mit_FMZollPersID, mit_FMZollPersIDKasseIsnotSpedBuch=@mit_FMZollPersIDKasseIsnotSpedBuch,mit_FMZollPersIDSpedBuch=@mit_FMZollPersIDSpedBuch,mit_standartFiliale=@mit_standartFiliale,mit_standardPreisliste=@mit_standardPreisliste,mit_DpUserStandardDP=@mit_DpUserStandardDP, mit_EZOLL_SB=@mit_EZOLL_SB, mit_ATLAS_SB=@mit_ATLAS_SB,mit_FirmenDatenAnzeige=@mit_FirmenDatenAnzeige,mit_DAKOSY_SB=@mit_DAKOSY_SB, mit_DAKOSY_SBFull=@mit_DAKOSY_SBFull ,mit_DpUserStandardFirma=@mit_DpUserStandardFirma,mit_DpUserStandardFirma2=@mit_DpUserStandardFirma2, mit_TESTonly=@mit_TESTonly,mit_PasswortChanged=@mit_PasswortChanged,mit_TELOTEC_SB=@mit_TELOTEC_SB,mit_AliasAD_Domain=@mit_AliasAD_Domain,mit_AliasAD_Username=@mit_AliasAD_Username,mit_AliasAD_Domain2=@mit_AliasAD_Domain2,mit_AliasAD_Username2=@mit_AliasAD_Username2,mit_teamId=@mit_teamId,mit_ChatBenutzer=@mit_ChatBenutzer,mit_FirmaHaupt=@mit_FirmaHaupt ,mit_EZOLL_SB_UNISPED=@mit_EZOLL_SB_UNISPED " &
                     " ,mit_ChatEasteregg=@mit_ChatEasteregg,mit_MAVerzeichnis_SettingsTstmp=@mit_MAVerzeichnis_SettingsTstmp,mit_MAVerzeichnis_allowGebDat=@mit_MAVerzeichnis_allowGebDat,mit_MAVerzeichnis_allowWohnadresse=@mit_MAVerzeichnis_allowWohnadresse,mit_MAVerzeichnis_allowGebDatOhneJahr=@mit_MAVerzeichnis_allowGebDatOhneJahr,mit_MAVerzeichnis_Spitzname=@mit_MAVerzeichnis_Spitzname,mit_foto_DocId=@mit_foto_DocId,mit_MAVerzeichnis_Motto=@mit_MAVerzeichnis_Motto,mit_sprache=@mit_sprache,mit_fakturierungsgruppe=@mit_fakturierungsgruppe, mit_CSuser=@mit_CSuser, mit_CSpwd=@mit_CSpwd, mit_PersonalNr = @mit_PersonalNr, mit_timasId = @mit_timasId, mit_telefonnr = @mit_telefonnr, mit_ATGRNAccessCode_AT51FP7 =@mit_ATGRNAccessCode_AT51FP7 ,mit_ATGRNAccessCode_AT52VA6=@mit_ATGRNAccessCode_AT52VA6, mit_Sprachkentnisse=@mit_Sprachkentnisse, mit_homeoffice=@mit_homeoffice, mit_timasNichtStempeln=@mit_timasNichtStempeln, mit_emailprivat=@mit_emailprivat, mit_timasMehrereFirmen=@mit_timasMehrereFirmen, mit_timasFirmen=@mit_timasFirmen, mit_Funktion=@mit_Funktion, mit_terminalTimasId=@mit_terminalTimasId, mit_EMailbenachrichtigungen=@mit_EMailbenachrichtigungen" &
                     " WHERE mit_id=@mit_id "



        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@mit_username", m.mit_username)
                cmd.Parameters.AddWithValue("@mit_abweichenderAnzeigename", m.mit_abweichenderAnzeigename)
                cmd.Parameters.AddWithValue("@mit_pwd", m.mit_pwd)
                cmd.Parameters.AddWithValue("@mit_geschlecht", m.mit_geschlecht)
                cmd.Parameters.AddWithValue("@mit_affix", m.mit_affix)
                cmd.Parameters.AddWithValue("@mit_vname", m.mit_vname)
                cmd.Parameters.AddWithValue("@mit_nname", m.mit_nname)
                If m.mit_gebdat <> "" Then cmd.Parameters.AddWithValue("@mit_gebdat", m.mit_gebdat) Else cmd.Parameters.AddWithValue("@mit_gebdat", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_strasse", m.mit_strasse)
                cmd.Parameters.AddWithValue("@mit_hausnr", m.mit_hausnr)
                cmd.Parameters.AddWithValue("@mit_land", m.mit_land)
                cmd.Parameters.AddWithValue("@mit_plz", m.mit_plz)
                cmd.Parameters.AddWithValue("@mit_ort", m.mit_ort)
                cmd.Parameters.AddWithValue("@mit_niederlassung", m.mit_niederlassung)
                cmd.Parameters.AddWithValue("@mit_abteilung", m.mit_abteilung)
                cmd.Parameters.AddWithValue("@mit_position", m.mit_position)
                cmd.Parameters.AddWithValue("@mit_durchwahl", m.mit_durchwahl)
                cmd.Parameters.AddWithValue("@mit_email", m.mit_email)
                cmd.Parameters.AddWithValue("@mit_mobiltel", m.mit_mobiltel)
                cmd.Parameters.AddWithValue("@mit_allowAnruf", m.mit_allowAnruf)
                cmd.Parameters.AddWithValue("@mit_FMZollPersID", If(m.mit_FMZollPersID, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_FMZollPersIDSpedBuch", If(m.mit_FMZollPersIDSpedBuch, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_FMZollPersIDKasseIsnotSpedBuch", m.mit_FMZollPersIDKasseIsnotSpedBuch)
                cmd.Parameters.AddWithValue("@mit_standartFiliale", m.mit_standartFiliale)
                cmd.Parameters.AddWithValue("@mit_standardPreisliste", m.mit_standardPreisliste)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardDP", m.mit_DpUserStandardDP)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardFirma", m.mit_DpUserStandardFirma)
                cmd.Parameters.AddWithValue("@mit_DpUserStandardFirma2", m.mit_DpUserStandardFirma2)

                cmd.Parameters.AddWithValue("@mit_info", m.mit_info)
                If m.mit_einstiegsdatum <> "" Then cmd.Parameters.AddWithValue("@mit_einstiegsdatum", m.mit_einstiegsdatum) Else cmd.Parameters.AddWithValue("@mit_einstiegsdatum", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_gekuendigt", m.mit_gekuendigt)
                If m.mit_kuendigungsdatum <> "" Then cmd.Parameters.AddWithValue("@mit_kuendigungsdatum", m.mit_kuendigungsdatum) Else cmd.Parameters.AddWithValue("@mit_kuendigungsdatum", DBNull.Value)
                If m.mit_foto <> "" Then cmd.Parameters.AddWithValue("@mit_foto", m.mit_foto) Else cmd.Parameters.AddWithValue("@mit_foto", DBNull.Value)
                cmd.Parameters.AddWithValue("@mit_firma", m.mit_firma)
                cmd.Parameters.AddWithValue("@mit_firmaFMZoll", If(m.mit_firmaFMZoll, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_id", m.mit_id)

                cmd.Parameters.AddWithValue("@mit_EZOLL_SB", If(m.mit_EZOLL_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_EZOLL_SB_UNISPED", If(m.mit_EZOLL_SB_UNISPED, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATLAS_SB", If(m.mit_ATLAS_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_DAKOSY_SB", If(m.mit_DAKOSY_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_DAKOSY_SBFull", If(m.mit_DAKOSY_SBFull, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_FirmenDatenAnzeige", If(m.mit_FirmenDatenAnzeige, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_TESTonly", m.mit_TESTonly)
                cmd.Parameters.AddWithValue("@mit_PasswortChanged", If(m.mit_PasswortChanged, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_TELOTEC_SB", If(m.mit_TELOTEC_SB, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Domain", If(m.mit_AliasAD_Domain, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Username", If(m.mit_AliasAD_Username, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Domain2", If(m.mit_AliasAD_Domain2, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_AliasAD_Username2", If(m.mit_AliasAD_Username2, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_teamId", If(m.mit_teamId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ChatBenutzer", m.mit_ChatBenutzer)
                cmd.Parameters.AddWithValue("@mit_FirmaHaupt", If(m.mit_FirmaHaupt, DBNull.Value))


                cmd.Parameters.AddWithValue("@mit_ChatEasteregg", m.mit_ChatEasteregg)
                cmd.Parameters.AddWithValue("@mit_ChatEasteregg2", m.mit_ChatEasteregg2)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_SettingsTstmp ", If(m.mit_MAVerzeichnis_SettingsTstmp, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowGebDat ", m.mit_MAVerzeichnis_allowGebDat)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowWohnadresse ", m.mit_MAVerzeichnis_allowWohnadresse)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_allowGebDatOhneJahr ", m.mit_MAVerzeichnis_allowGebDatOhneJahr)
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_Spitzname ", If(m.mit_MAVerzeichnis_Spitzname, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_foto_DocId", If(m.mit_foto_DocId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_MAVerzeichnis_Motto", If(m.mit_MAVerzeichnis_Motto, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_sprache", m.mit_sprache)
                cmd.Parameters.AddWithValue("@mit_fakturierungsgruppe", m.mit_fakturierungsgruppe)
                cmd.Parameters.AddWithValue("@mit_CSuser", If(m.mit_CSuser, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_CSpwd", If(m.mit_CSpwd, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_PersonalNr", If(m.mit_PersonalNr, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_timasId", If(m.mit_timasId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_telefonnr", If(m.mit_telefonnr, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATGRNAccessCode_AT51FP7 ", If(m.mit_ATGRNAccessCode_AT51FP7, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_ATGRNAccessCode_AT52VA6 ", If(m.mit_ATGRNAccessCode_AT52VA6, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_Sprachkentnisse", If(m.mit_Sprachkentnisse, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_homeoffice", m.mit_homeoffice)
                cmd.Parameters.AddWithValue("@mit_timasNichtStempeln", m.mit_timasNichtStempeln)
                cmd.Parameters.AddWithValue("@mit_emailprivat", m.mit_emailprivat)
                cmd.Parameters.AddWithValue("@mit_timasMehrereFirmen", m.mit_timasMehrereFirmen)
                cmd.Parameters.AddWithValue("@mit_timasFirmen", If(m.mit_timasFirmen, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_Funktion", If(m.mit_Funktion, DBNull.Value))

                cmd.Parameters.AddWithValue("@mit_terminalTimasId", If(m.mit_terminalTimasId, DBNull.Value))
                cmd.Parameters.AddWithValue("@mit_EMailbenachrichtigungen", If(m.mit_EMailbenachrichtigungen, DBNull.Value))


                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 tblMitarbeiter")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Sub delData(ByVal usrID As Integer)
        delUsrBerechtigung(usrID)
        delUsrSeminar(usrID)
        delUsr(usrID)
    End Sub
    Public Sub delUsr(ByVal usrID As Integer)
        Dim sql As String = "DELETE FROM tblMitarbeiter WHERE mit_id = " & usrID
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                Try
                    cmd.ExecuteNonQuery()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
    End Sub
    Public Sub delUsrBerechtigung(ByVal usrID As Integer)
        Dim sql As String = "DELETE FROM tblBerProg WHERE bepr_userId = " & usrID
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                Try
                    cmd.ExecuteNonQuery()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
    End Sub
    Public Sub delUsrSeminar(ByVal usrID As Integer)
        Dim sql As String = "DELETE FROM tblSemMitarb WHERE semi_mitId = " & usrID
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                Try
                    cmd.ExecuteNonQuery()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
    End Sub
End Class


Public Class cSeminare


    Public Function insertSemMitarbeiter(ByVal semi_semId As Integer, ByVal semi_mitId As Integer) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "INSERT INTO tblSemMitarb " &
                       "(semi_semId, semi_mitId) VALUES " &
                       "(@semi_semId,@semi_mitId)"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@semi_semId", semi_semId)
                cmd.Parameters.AddWithValue("@semi_mitId", semi_mitId)
                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
    End Function

    Public Function delSemMitarbeiter(ByVal id As Integer) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "DELETE FROM tblSemMitarb WHERE semi_id=@semi_id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@semi_id", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function getSeminarById(ByVal id As Integer) As cSeminar
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim d As New cSeminar
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblSeminare WHERE sem_id=@semi_id ", conn)
                        cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        If dr.Read Then
                            d.sem_id = dr.Item("sem_id")
                            If Not dr.Item("sem_von") Is DBNull.Value Then d.sem_von = dr.Item("sem_von")
                            If Not dr.Item("sem_bis") Is DBNull.Value Then d.sem_bis = dr.Item("sem_bis")
                            If Not dr.Item("sem_dauer") Is DBNull.Value Then d.sem_dauer = dr.Item("sem_dauer")
                            d.sem_name = dr.Item("sem_name")
                            d.sem_vortragender = dr.Item("sem_vortragender")
                            If Not dr.Item("sem_vortragender_mitId") Is DBNull.Value Then d.sem_vortragender_mitId = dr.Item("sem_vortragender_mitId")
                            d.sem_ort = dr.Item("sem_ort")
                            d.sem_inhalt = dr.Item("sem_inhalt")
                            d.sem_inhalt2 = dr.Item("sem_inhalt2")
                            d.sem_info = dr.Item("sem_info")
                            If Not dr.Item("sem_isVeragSeminar") Is DBNull.Value Then d.sem_isVeragSeminar = dr.Item("sem_isVeragSeminar")
                            If Not dr.Item("sem_isIntAusbildung") Is DBNull.Value Then d.sem_isIntAusbildung = dr.Item("sem_isIntAusbildung")
                            If Not dr.Item("sem_bezeichnung") Is DBNull.Value Then d.sem_bezeichnung = dr.Item("sem_bezeichnung")
                            d.sem_type = dr.Item("sem_type")
                        End If
                        dr.Close()
                    End Using
                End Using
                Return d
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getSeminarById'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function
    Public Function insertSeminare(ByVal s As cSeminar) As Boolean
        Dim sql = "INSERT INTO tblSeminare " &
                       "(sem_von,sem_bis,sem_dauer,sem_name,sem_vortragender,sem_vortragender_mitId,sem_ort,sem_inhalt,sem_inhalt2,sem_info,sem_isVeragSeminar,sem_type,sem_isIntAusbildung,sem_bezeichnung) VALUES " &
                       "(@sem_von,@sem_bis,@sem_dauer,@sem_name,@sem_vortragender,@sem_vortragender_mitId,@sem_ort,@sem_inhalt,@sem_inhalt2,@sem_info,@sem_isVeragSeminar,@sem_type,@sem_isIntAusbildung,@sem_bezeichnung)"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@sem_von", s.sem_von)
                cmd.Parameters.AddWithValue("@sem_bis", s.sem_bis)
                cmd.Parameters.AddWithValue("@sem_dauer", s.sem_dauer)
                cmd.Parameters.AddWithValue("@sem_name", s.sem_name)
                cmd.Parameters.AddWithValue("@sem_vortragender", s.sem_vortragender)
                cmd.Parameters.AddWithValue("@sem_vortragender_mitId", s.sem_vortragender_mitId)
                cmd.Parameters.AddWithValue("@sem_ort", s.sem_ort)
                cmd.Parameters.AddWithValue("@sem_inhalt", s.sem_inhalt)
                cmd.Parameters.AddWithValue("@sem_inhalt2", s.sem_inhalt2)
                cmd.Parameters.AddWithValue("@sem_info", s.sem_info)
                cmd.Parameters.AddWithValue("@sem_isVeragSeminar", s.sem_isVeragSeminar)
                cmd.Parameters.AddWithValue("@sem_isIntAusbildung", s.sem_isIntAusbildung)
                cmd.Parameters.AddWithValue("@sem_bezeichnung", s.sem_bezeichnung)
                cmd.Parameters.AddWithValue("@sem_type", s.sem_type)

                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
    End Function

    Public Function updateSeminare(ByVal s As cSeminar) As Boolean
        Dim sql = "UPDATE tblSeminare " &
                       "SET sem_von=@sem_von, sem_bis=@sem_bis, sem_dauer=@sem_dauer, sem_name=@sem_name, sem_vortragender=@sem_vortragender, sem_vortragender_mitId=@sem_vortragender_mitId, sem_ort=@sem_ort, sem_inhalt=@sem_inhalt,sem_inhalt2=@sem_inhalt2, sem_info=@sem_info, sem_isVeragSeminar=@sem_isVeragSeminar, sem_type=@sem_type, sem_isIntAusbildung=@sem_isIntAusbildung,sem_bezeichnung=@sem_bezeichnung " &
                       "WHERE sem_id=@sem_id"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@sem_von", s.sem_von)
                cmd.Parameters.AddWithValue("@sem_bis", s.sem_bis)
                cmd.Parameters.AddWithValue("@sem_dauer", s.sem_dauer)
                cmd.Parameters.AddWithValue("@sem_name", s.sem_name)
                cmd.Parameters.AddWithValue("@sem_vortragender", s.sem_vortragender)
                cmd.Parameters.AddWithValue("@sem_vortragender_mitId", s.sem_vortragender_mitId)
                cmd.Parameters.AddWithValue("@sem_ort", s.sem_ort)
                cmd.Parameters.AddWithValue("@sem_inhalt", s.sem_inhalt)
                cmd.Parameters.AddWithValue("@sem_inhalt2", s.sem_inhalt2)
                cmd.Parameters.AddWithValue("@sem_info", s.sem_info)
                cmd.Parameters.AddWithValue("@sem_isVeragSeminar", s.sem_isVeragSeminar)
                cmd.Parameters.AddWithValue("@sem_isIntAusbildung", s.sem_isIntAusbildung)
                cmd.Parameters.AddWithValue("@sem_bezeichnung", s.sem_bezeichnung)
                cmd.Parameters.AddWithValue("@sem_type", s.sem_type)
                cmd.Parameters.AddWithValue("@sem_id", s.sem_id)

                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
    End Function
End Class



Public Class cDienstplan

    Public Function doSQLVarListID(ByVal id As Integer, ByVal sql As String, Optional conn_art As String = "ADMIN", Optional showErr As Boolean = True, Optional list As List(Of SQLVariable) = Nothing) As Integer

        Dim conn As New SqlConnection
        Select Case conn_art
            Case "ADMIN" : conn = cSqlDb.GetNewOpenConnection()
            Case "FMZOLL" : conn = cSqlDb.GetNewOpenConnectionFMZOLL()
        End Select
        Try
            Using cmd As New SqlCommand(sql, conn)
                If list IsNot Nothing Then
                    For Each i In list
                        cmd.Parameters.AddWithValue("@" & i.Scalarvariable.trim, IIf(i.Value Is Nothing, DBNull.Value, i.Value))
                    Next
                End If
                cmd.ExecuteNonQuery()
                If id < 0 Then
                    Dim newcmd As New SqlCommand("SELECT @@IDENTITY", conn)
                    id = CInt(newcmd.ExecuteScalar)
                End If

                'Return True
            End Using
            conn.Close()

        Catch ex As Exception
            If showErr Then MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & ex.StackTrace)
        End Try
        Return id
    End Function

    Public Function insertDienstMA(ByVal ma As cDienstMA, ByVal niederlassung As String) As Boolean
        Dim hAuswahl As String = ""

        Dim sql As String = " begin tran" &
                          " if exists (select * from tblDienstMitarb with (updlock,serializable) where dstma_id = @dstma_id ) " &
                                 " begin " &
                                 " UPDATE tblDienstMitarb " &
                                 " SET dstma_abteilung=@dstma_abteilung, dstma_funktion=@dstma_funktion, dstma_arbvh=@dstma_arbvh, dstma_wochenStunden=@dstma_wochenStunden, dstma_stammSchicht=@dstma_stammSchicht, dstma_kuerzel=@dstma_kuerzel, dstma_reihenfolge=@dstma_reihenfolge, dstma_farbe=@dstma_farbe, dstma_TageProWoche=@dstma_TageProWoche,dstma_niederlassung=@dstma_niederlassung,dstma_muster=@dstma_muster,dstma_4wo=@dstma_4wo,dstma_WEStdRegelAZ=@dstma_WEStdRegelAZ,dstma_inaktiv=@dstma_inaktiv,dstma_TzFeiertageStd=@dstma_TzFeiertageStd, dstma_land=@dstma_land " &
                                 " WHERE dstma_id=@dstma_id " &
                              " End " &
                          " Else " &
                          " begin " &
                               "INSERT INTO tblDienstMitarb " &
                               "( dstma_mitId, dstma_abteilung, dstma_funktion, dstma_arbvh, dstma_wochenStunden, dstma_stammSchicht,dstma_kuerzel,dstma_reihenfolge,dstma_farbe,dstma_TageProWoche,dstma_niederlassung, dstma_muster,dstma_4wo,dstma_WEStdRegelAZ,dstma_inaktiv ,dstma_TzFeiertageStd, dstma_land) VALUES " &
                               "( @dstma_mitId, @dstma_abteilung, @dstma_funktion, @dstma_arbvh, @dstma_wochenStunden, @dstma_stammSchicht,@dstma_kuerzel,@dstma_reihenfolge,@dstma_farbe,@dstma_TageProWoche,@dstma_niederlassung, @dstma_muster ,@dstma_4wo,@dstma_WEStdRegelAZ,@dstma_inaktiv,@dstma_TzFeiertageStd, @dstma_land) " &
                          " End " &
                          " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dstma_id", ma.dstma_id)
                cmd.Parameters.AddWithValue("@dstma_mitId", ma.dstma_mitId)
                cmd.Parameters.AddWithValue("@dstma_niederlassung", niederlassung)
                cmd.Parameters.AddWithValue("@dstma_abteilung", ma.dstma_abteilung)
                cmd.Parameters.AddWithValue("@dstma_funktion", ma.dstma_funktion)
                cmd.Parameters.AddWithValue("@dstma_arbvh", ma.dstma_arbvh)
                cmd.Parameters.AddWithValue("@dstma_wochenStunden", ma.dstma_wochenStunden)
                cmd.Parameters.AddWithValue("@dstma_stammSchicht", ma.dstma_stammSchicht)
                cmd.Parameters.AddWithValue("@dstma_kuerzel", ma.dstma_kuerzel)
                cmd.Parameters.AddWithValue("@dstma_reihenfolge", ma.dstma_reihenfolge)
                cmd.Parameters.AddWithValue("@dstma_farbe", ma.dstma_farbe)
                cmd.Parameters.AddWithValue("@dstma_TageProWoche", ma.dstma_TageProWoche)
                cmd.Parameters.AddWithValue("@dstma_muster", ma.dstma_muster)
                cmd.Parameters.AddWithValue("@dstma_4wo", ma.dstma_4wo)
                cmd.Parameters.AddWithValue("@dstma_WEStdRegelAZ", ma.dstma_WEStdRegelAZ)
                cmd.Parameters.AddWithValue("@dstma_inaktiv", ma.dstma_inaktiv)
                cmd.Parameters.AddWithValue("@dstma_TzFeiertageStd", If(ma.dstma_TzFeiertageStd Is Nothing, DBNull.Value, ma.dstma_TzFeiertageStd))
                cmd.Parameters.AddWithValue("@dstma_land", ma.dstma_land)

                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
    End Function




    Public Function insertifExistsDienstKWUeberstd(dusa_niederlassung As String, dusa_jahr As Integer, dusa_kw As Integer) As Boolean
        Dim sql = " if not exists(select * from tblDienstMitarb with (updlock,serializable) where dstma_mitId = @dstma_mitId ) " &
                    " Begin " &
                         "INSERT INTO tblDienstUeberstdAbgeschlossen " &
                           "(dusa_niederlassung, dusa_jahr, dusa_kw) VALUES " &
                           "(@dusa_niederlassung,@dusa_jahr,@dusa_kw)" &
                    " End " &
                  " commit tran "
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dusa_niederlassung", dusa_niederlassung)
                cmd.Parameters.AddWithValue("@dusa_jahr", dusa_jahr)
                cmd.Parameters.AddWithValue("@dusa_kw", dusa_kw)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function existsEntryKWUeberstd(dusa_niederlassung As String, dusa_jahr As Integer, dusa_kw As Integer) As Boolean
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstUeberstdAbgeschlossen WHERE dusa_niederlassung= @dusa_niederlassung AND dusa_jahr= @dusa_jahr AND  dusa_kw= @dusa_kw ", conn)
                    cmd.Parameters.AddWithValue("@dusa_niederlassung", dusa_niederlassung)
                    cmd.Parameters.AddWithValue("@dusa_jahr", dusa_jahr)
                    cmd.Parameters.AddWithValue("@dusa_kw", dusa_kw)

                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then Return True

                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'existsEntryKWUeberstd'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return False
    End Function

    Public Function getMaxEntryUestdAbgeschl(dusa_niederlassung As String) As String
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT TOP 1 [dusa_jahr],[dusa_kw] FROM tblDienstUeberstdAbgeschlossen WHERE dusa_niederlassung= @dusa_niederlassung ORDER BY [dusa_jahr] desc, [dusa_kw] desc ", conn)
                    cmd.Parameters.AddWithValue("@dusa_niederlassung", dusa_niederlassung)


                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("dusa_jahr") & dr.Item("dusa_kw")
                    End If
                    dr.Close()
                End Using
            End Using
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'existsEntryKWUeberstd'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return False
    End Function

    Public Function updateDienstMaStdADD(id, plus) As Boolean


        Dim sql = "UPDATE [tblDienstMitarb] " &
                       "SET dstma_ueberstdCounter=(dstma_ueberstdCounter + @dstma_ueberstdCounter) " &
                       "WHERE dstma_id=@dstma_id"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstma_id", id)
                cmd.Parameters.AddWithValue("@dstma_ueberstdCounter", plus)
                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

    End Function



    Public Function updateDienstMaStd(id, dstma_ueberstdCounter) As Boolean
        Dim sql = "UPDATE [tblDienstMitarb] " &
                       "SET dstma_ueberstdCounter=@dstma_ueberstdCounter " &
                       "WHERE dstma_id=@dstma_id"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstma_id", id)
                cmd.Parameters.AddWithValue("@dstma_ueberstdCounter", dstma_ueberstdCounter)
                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

    End Function




    Public Function insertupdateDienstplanUeberSdtMa(usma_maId As Integer, usma_idKW As Integer, usma_idJahr As Integer, usma_stunden As Double, niederlassung As String) As Boolean
        Dim sql As String = " begin tran" &
                                 " if exists (select * from tblDienstplanUeberSdtMa with (updlock,serializable) WHERE usma_idKW=@usma_idKW AND usma_idJahr=@usma_idJahr AND usma_maId=@usma_maId ) " &
                                        " begin " &
                                        "UPDATE [tblDienstplanUeberSdtMa] " &
                                           "SET usma_stunden=@usma_stunden " &
                                           "WHERE usma_idKW=@usma_idKW AND usma_idJahr=@usma_idJahr AND usma_maId=@usma_maId" &
                                     " End " &
                                 " Else " &
                                 " begin " &
                                      "INSERT INTO [tblDienstplanUeberSdtMa] " &
                                           "(usma_maId, usma_idKW, usma_idJahr, usma_stunden,usma_stdAuszahlung,usma_niederlassung) VALUES " &
                                           "(@usma_maId,@usma_idKW,@usma_idJahr,@usma_stunden,@usma_stdAuszahlung,@usma_niederlassung)" &
                                 " End " &
                                 " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@usma_maId", usma_maId)
                cmd.Parameters.AddWithValue("@usma_idKW", usma_idKW)
                cmd.Parameters.AddWithValue("@usma_idJahr", usma_idJahr)
                cmd.Parameters.AddWithValue("@usma_stunden", usma_stunden)
                cmd.Parameters.AddWithValue("@usma_stdAuszahlung", 0)
                cmd.Parameters.AddWithValue("@usma_niederlassung", niederlassung)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function insertupdateDienstplanUeberSdtMaAuszahlung(usma_maId As Integer, usma_idKW As Integer, usma_idJahr As Integer, usma_stdAuszahlung As Double, niederlassung As String) As Boolean
        Dim sql As String = " begin tran" &
                                 " if exists (select * from tblDienstplanUeberSdtMa with (updlock,serializable) WHERE usma_idKW=@usma_idKW AND usma_idJahr=@usma_idJahr AND usma_maId=@usma_maId ) " &
                                        " begin " &
                                        "UPDATE [tblDienstplanUeberSdtMa] " &
                                           "SET usma_stdAuszahlung=@usma_stdAuszahlung " &
                                           "WHERE usma_idKW=@usma_idKW AND usma_idJahr=@usma_idJahr AND usma_maId=@usma_maId" &
                                     " End " &
                                 " Else " &
                                 " begin " &
                                      "INSERT INTO [tblDienstplanUeberSdtMa] " &
                                           "(usma_maId, usma_idKW, usma_idJahr, usma_stunden,usma_stdAuszahlung,usma_niederlassung) VALUES " &
                                           "(@usma_maId,@usma_idKW,@usma_idJahr,@usma_stunden,@usma_stdAuszahlung,@usma_niederlassung)" &
                                 " End " &
                                 " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@usma_maId", usma_maId)
                cmd.Parameters.AddWithValue("@usma_idKW", usma_idKW)
                cmd.Parameters.AddWithValue("@usma_idJahr", usma_idJahr)
                cmd.Parameters.AddWithValue("@usma_stunden", 0)
                cmd.Parameters.AddWithValue("@usma_stdAuszahlung", usma_stdAuszahlung)
                cmd.Parameters.AddWithValue("@usma_niederlassung", niederlassung)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function getDienstplanUeberSdtMaSUMMEBisKW(usma_maId As Integer, usma_idKW As Integer, usma_idJahr As Integer, niederlassung As String, Optional inklAktKW As Boolean = False) As Double
        Dim minKw = 40
        Dim minJahr = 2016

        Dim DP_SETTINGS = New cDienstSettings(niederlassung)
        minKw = DP_SETTINGS.dpset_startKW
        minJahr = DP_SETTINGS.dpset_startJahr

        Dim sqlStr = ""
        If inklAktKW Then
            sqlStr = "SELECT isnull(sum(usma_stunden - usma_stdAuszahlung),0) as SUM FROM tblDienstplanUeberSdtMa WHERE usma_maId=@usma_maId AND ((usma_idJahr=@usma_idJahr AND  usma_idKW<=@usma_idKW) OR usma_idJahr<@usma_idJahr )   " &
                                            " AND ((usma_idJahr=@minJahr AND  usma_idKW>=@minKW) OR usma_idJahr>@minJahr ) "

        Else
            sqlStr = "SELECT isnull(sum(usma_stunden - usma_stdAuszahlung),0) as SUM FROM tblDienstplanUeberSdtMa WHERE usma_maId=@usma_maId AND ((usma_idJahr=@usma_idJahr AND  usma_idKW<@usma_idKW) OR usma_idJahr<@usma_idJahr )   " &
                                            " AND ((usma_idJahr=@minJahr AND  usma_idKW>=@minKW) OR usma_idJahr>@minJahr ) "

        End If
        Try
            Dim d As New cDienstAbwesendheitenKW
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(sqlStr, conn)

                    cmd.Parameters.AddWithValue("@usma_maId", usma_maId)
                    cmd.Parameters.AddWithValue("@usma_idKW", usma_idKW)
                    cmd.Parameters.AddWithValue("@usma_idJahr", usma_idJahr)
                    cmd.Parameters.AddWithValue("@minJahr", minJahr)
                    cmd.Parameters.AddWithValue("@minKW", minKw)

                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("SUM")
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return 0
    End Function

    Public Function getDienstplanUeberSdtMaSUMM_KW(usma_maId As Integer, usma_idKW As Integer, usma_idJahr As Integer) As Double
        Try
            Dim d As New cDienstAbwesendheitenKW
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT isnull(sum(usma_stunden - usma_stdAuszahlung),0) as SUM FROM tblDienstplanUeberSdtMa WHERE usma_maId=@usma_maId AND usma_idJahr=@usma_idJahr AND usma_idKW=@usma_idKW ", conn)

                    cmd.Parameters.AddWithValue("@usma_maId", usma_maId)
                    cmd.Parameters.AddWithValue("@usma_idKW", usma_idKW)
                    cmd.Parameters.AddWithValue("@usma_idJahr", usma_idJahr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("SUM")
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return 0
    End Function



    Public Function getDienstplanAuszSdtMaSUMM_KW(usma_maId As Integer, usma_idKW As Integer, usma_idJahr As Integer) As Double
        Try
            Dim d As New cDienstAbwesendheitenKW
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT isnull(usma_stdAuszahlung,0) as SUM FROM tblDienstplanUeberSdtMa WHERE usma_maId=@usma_maId AND usma_idJahr=@usma_idJahr AND usma_idKW=@usma_idKW ", conn)

                    cmd.Parameters.AddWithValue("@usma_maId", usma_maId)
                    cmd.Parameters.AddWithValue("@usma_idKW", usma_idKW)
                    cmd.Parameters.AddWithValue("@usma_idJahr", usma_idJahr)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("SUM")
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return 0
    End Function

    Public Function insertDienstNA(dstna_dstmaId As Integer, dstna_datum_von As Date, dstna_datum_bis As Date, dstna_grund As String, dstna_info As String, dstna_niederlassung As String, dstna_dstnkId As Integer) As Boolean
        Dim sql = "INSERT INTO tblDienstNichtAnwesend " &
                       "(dstna_dstmaId, dstna_datum_von, dstna_datum_bis, dstna_grund, dstna_info,dstna_niederlassung,dstna_dstnkId) VALUES " &
                       "(@dstna_dstmaId,@dstna_datum_von,@dstna_datum_bis,@dstna_grund,@dstna_info,@dstna_niederlassung,@dstna_dstnkId)"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstna_dstmaId", dstna_dstmaId)
                cmd.Parameters.AddWithValue("@dstna_niederlassung", dstna_niederlassung)
                cmd.Parameters.AddWithValue("@dstna_datum_von", dstna_datum_von.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstna_datum_bis", dstna_datum_bis.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstna_grund", dstna_grund)
                cmd.Parameters.AddWithValue("@dstna_info", dstna_info)
                cmd.Parameters.AddWithValue("@dstna_dstnkId", dstna_dstnkId)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function insertDienstNA_KW(dstnk_dstmaId As Integer, dstnk_kw As Integer, dstnk_jahr As Integer, dstnk_grund As String, dstnk_info As String, dstnk_niederlassung As String) As Integer
        Dim id As Integer = -1
        Dim sql = "INSERT INTO [tblDienstNichtAnwesendKW] " &
                       "(dstnk_dstmaId, dstnk_kw, dstnk_jahr, dstnk_grund, dstnk_info,dstnk_niederlassung) VALUES " &
                       "(@dstnk_dstmaId,@dstnk_kw,@dstnk_jahr,@dstnk_grund,@dstnk_info,@dstnk_niederlassung); SELECT CAST(scope_identity() AS int);"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstnk_dstmaId", dstnk_dstmaId)
                cmd.Parameters.AddWithValue("@dstnk_niederlassung", dstnk_niederlassung)
                cmd.Parameters.AddWithValue("@dstnk_kw", dstnk_kw)
                cmd.Parameters.AddWithValue("@dstnk_jahr", dstnk_jahr)
                cmd.Parameters.AddWithValue("@dstnk_grund", dstnk_grund)
                cmd.Parameters.AddWithValue("@dstnk_info", dstnk_info)

                Try

                    'cmd.ExecuteNonQuery()
                    id = (Convert.ToInt32(cmd.ExecuteScalar()))
                    '  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 Programm")
                End Try


            End Using
        End Using
        Return id
    End Function


    Public Function getDienstAbwesendheitenKW(kw As Integer, maId As Integer, jahr As Integer, ByVal niederlassung As String) As List(Of cDienstAbwesendheitenKW)
        Try
            Dim data As New List(Of cDienstAbwesendheitenKW)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesendKW WHERE dstnk_dstmaId= '" & maId & "' AND dstnk_niederlassung= '" & niederlassung & "' AND dstnk_kw= '" & kw & "' AND dstnk_jahr= '" & jahr & "' ", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheitenKW

                        d.dstnk_id = dr.Item("dstnk_id")
                        d.dstnk_mitId = dr.Item("dstnk_dstmaId")
                        d.dstnk_kw = dr.Item("dstnk_kw")
                        d.dstnk_jahr = dr.Item("dstnk_jahr")
                        d.dstnk_grund = dr.Item("dstnk_grund")
                        d.dstnk_info = dr.Item("dstnk_info")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function

    Public Function getDienstAbwesendheitenKWByArt(kw As Integer, maId As Integer, jahr As Integer, ByVal niederlassung As String, dstnk_grund As String) As List(Of cDienstAbwesendheitenKW)
        Try
            Dim data As New List(Of cDienstAbwesendheitenKW)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesendKW WHERE dstnk_dstmaId= '" & maId & "' AND dstnk_niederlassung= '" & niederlassung & "' AND dstnk_kw= '" & kw & "' AND dstnk_jahr= '" & jahr & "' AND dstnk_grund= '" & dstnk_grund & "'", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheitenKW

                        d.dstnk_id = dr.Item("dstnk_id")
                        d.dstnk_mitId = dr.Item("dstnk_dstmaId")
                        d.dstnk_kw = dr.Item("dstnk_kw")
                        d.dstnk_jahr = dr.Item("dstnk_jahr")
                        d.dstnk_grund = dr.Item("dstnk_grund")
                        d.dstnk_info = dr.Item("dstnk_info")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function



    Public Function getDienstAbwesendheitenKWByID(kwId As Integer) As cDienstAbwesendheitenKW
        Try
            Dim d As New cDienstAbwesendheitenKW
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDienstNichtAnwesendKW WHERE dstnk_id= " & kwId & " ", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        d.dstnk_id = dr.Item("dstnk_id")
                        d.dstnk_mitId = dr.Item("dstnk_dstmaId")
                        d.dstnk_kw = dr.Item("dstnk_kw")
                        d.dstnk_jahr = dr.Item("dstnk_jahr")
                        d.dstnk_grund = dr.Item("dstnk_grund")
                        d.dstnk_info = dr.Item("dstnk_info")
                        Return d
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function

    Public Function getDienstAbwesendheitenKWByMA(dstnk_dstmaId As Integer, kw As Integer, jahr As Integer, ByVal niederlassung As String) As List(Of cDienstAbwesendheitenKW)
        Try
            Dim data As New List(Of cDienstAbwesendheitenKW)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesendKW WHERE dstnk_dstmaId= '" & dstnk_dstmaId & "' AND dstnk_niederlassung= '" & niederlassung & "' AND dstnk_kw= '" & kw & "' AND dstnk_jahr= '" & jahr & "' ", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheitenKW

                        d.dstnk_id = dr.Item("dstnk_id")
                        d.dstnk_mitId = dr.Item("dstnk_dstmaId")
                        d.dstnk_kw = dr.Item("dstnk_kw")
                        d.dstnk_jahr = dr.Item("dstnk_jahr")
                        d.dstnk_grund = dr.Item("dstnk_grund")
                        d.dstnk_info = dr.Item("dstnk_info")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheitenkw'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function

    Public Function delDienstNichtAnwesendKW(ByVal dstnk_id As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesendKW WHERE dstnk_id=@dstnk_id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstnk_id", dstnk_id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function delDienstplanEintraege(ByVal von As Date, ByVal bis As Date, niederlassung As String) As Boolean
        Dim sql = "DELETE FROM [tblDienstplanEintraege] WHERE dstetr_datum BETWEEN @von AND @bis AND dstetr_niederlassung=@niederlassung"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@niederlassung", niederlassung)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function




    Public Function delDienstplanUeberSdtMa(ByVal kw As Integer, ByVal jahr As Integer, niederlassung As String) As Boolean
        Dim sql = "DELETE FROM [tblDienstplanUeberSdtMa] WHERE [usma_idKW]=@kw AND [usma_idJahr]=@jahr AND usma_niederlassung=@niederlassung"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@jahr", jahr)
                cmd.Parameters.AddWithValue("@kw", kw)
                cmd.Parameters.AddWithValue("@niederlassung", niederlassung)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function getDienstAbwesendheiten(von As Date, bis As Date, ByVal niederlassung As String) As List(Of cDienstAbwesendheiten)
        Try
            Dim data As New List(Of cDienstAbwesendheiten)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesend WHERE dstna_niederlassung= '" & niederlassung & "' AND (dstna_datum_von <= '" & bis.ToShortDateString & "' AND dstna_datum_bis >= '" & von.ToShortDateString & "') OR (dstna_datum_von <= '" & von.ToShortDateString & "' AND dstna_datum_bis >= '" & von.ToShortDateString & "') ", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheiten

                        d.dstna_id = dr.Item("dstna_id")
                        d.dstna_mitId = dr.Item("dstna_dstmaId")
                        d.dstna_datum_von = dr.Item("dstna_datum_von")
                        d.dstna_datum_bis = dr.Item("dstna_datum_bis")
                        d.dstna_grund = dr.Item("dstna_grund")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function

    Public Function getDienstAbwesendheitenByID(von As Date, bis As Date, ByVal niederlassung As String, ByVal grund As String, maId As Integer) As List(Of cDienstAbwesendheiten)
        Try
            Dim data As New List(Of cDienstAbwesendheiten)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesend WHERE dstna_niederlassung= @niederlassung  AND dstna_dstmaId= @id AND dstna_grund = @grund  AND (dstna_datum_von <= '" & bis.ToShortDateString & "' AND dstna_datum_bis >= '" & von.ToShortDateString & "') OR (dstna_datum_von <= '" & von.ToShortDateString & "' AND dstna_datum_bis >= '" & von.ToShortDateString & "') ", conn)
                    cmd.Parameters.AddWithValue("@niederlassung", niederlassung)
                    cmd.Parameters.AddWithValue("@id", maId)
                    cmd.Parameters.AddWithValue("@grund", grund)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheiten

                        d.dstna_id = dr.Item("dstna_id")
                        d.dstna_mitId = dr.Item("dstna_dstmaId")
                        d.dstna_datum_von = dr.Item("dstna_datum_von")
                        d.dstna_datum_bis = dr.Item("dstna_datum_bis")
                        d.dstna_grund = dr.Item("dstna_grund")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function


    Public Function getDienstAbwesendheitenByKWID(kwId As Integer) As List(Of cDienstAbwesendheiten)
        Try
            Dim data As New List(Of cDienstAbwesendheiten)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstNichtAnwesend WHERE dstna_dstnkId= @kwId ", conn)
                    cmd.Parameters.AddWithValue("@kwId", kwId)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstAbwesendheiten

                        d.dstna_id = dr.Item("dstna_id")
                        d.dstna_mitId = dr.Item("dstna_dstmaId")
                        d.dstna_datum_von = dr.Item("dstna_datum_von")
                        d.dstna_datum_bis = dr.Item("dstna_datum_bis")
                        d.dstna_grund = dr.Item("dstna_grund")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getDienstAbwesendheiten'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function


    Public Function delDienstNichtAnwesend(ByVal dstna_id As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesend WHERE dstna_id=@dstna_id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstna_id", dstna_id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDienstNichtAnwesendIdArtDatum(ByVal dstna_dstmaId As Integer, von As Date, bis As Date) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesend WHERE dstna_dstmaId=@dstna_dstmaId AND (dstna_datum_von between @dstna_datum_von AND @dstna_datum_bis) AND  dstna_datum_von=dstna_datum_bis "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstna_dstmaId", dstna_dstmaId)
                cmd.Parameters.AddWithValue("@dstna_datum_von", von.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstna_datum_bis", bis.ToShortDateString)
                ' cmd.Parameters.AddWithValue("@dstna_grund", art)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDienstNichtAnwesendKW_ByKW_ID(ByVal KW_ID As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesendKW WHERE dstnk_id=@KW_ID "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@KW_ID", KW_ID)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function delDienstNichtAnwesend_ByKW_ID(ByVal KW_ID As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesend WHERE dstna_dstnkID=@KW_ID "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@KW_ID", KW_ID)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDienstNichtAnwesendKW_ByKwJahr(ByVal dstnk_dstmaId As Integer, kw As Integer, jahr As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesendKW WHERE dstnk_dstmaId=@dstnk_dstmaId AND dstnk_kw=@dstnk_kw AND dstnk_jahr=@dstnk_jahr "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstnk_dstmaId", dstnk_dstmaId)
                cmd.Parameters.AddWithValue("@dstnk_kw", kw)
                cmd.Parameters.AddWithValue("@dstnk_jahr", jahr)
                ' cmd.Parameters.AddWithValue("@dstnk_grund", art)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function delDienstTeilzeit(ByVal id As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstTeilzeit WHERE dsttz_maId=@dsttz_maId"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dsttz_maId", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDienstTeilzeitByDedetId(ByVal id As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstplanSchichtenZeiten WHERE dsz_dedetId=@dsz_dedetId"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dsz_dedetId", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDienstNichtAnwesend2(ByVal id As Integer) As Boolean
        Dim sql = "DELETE FROM tblDienstNichtAnwesend WHERE dstna_dstmaId=@dstna_dstmaId"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstna_dstmaId", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function getAllDienstTeilzeit(id) As List(Of cDienstTeilzeit)
        Try
            Dim data As New List(Of cDienstTeilzeit)
            'MsgBox("id" & id)
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblDienstTeilzeit WHERE  dsttz_maId=@dsttz_maId ", conn)
                    cmd.Parameters.AddWithValue("@dsttz_maId", id)
                    '      cmd.Parameters.AddWithValue("@dsttz_niederlassung", niederlassung)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cDienstTeilzeit

                        d.dsttz_maId = dr.Item("dsttz_maId")
                        d.dsttz_wotag = dr.Item("dsttz_wotag")
                        d.dsttz_abt = dr.Item("dsttz_abt")
                        d.dsttz_von = dr.Item("dsttz_von")
                        d.dsttz_bis = dr.Item("dsttz_bis")
                        d.dsttz_dienstplan = dr.Item("dsttz_dienstplan")
                        d.dsttz_pause = dr.Item("dsttz_pause")
                        d.dsttz_bemerkung = dr.Item("dsttz_bemerkung")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getSeminarById'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function

    Public Function getDienstTeilzeitBySchicht(id, schicht, woTag, schicht4wo) As cDienstTeilzeit
        Try
            If schicht4wo Then

            Else '2Wochen-Wechsel
                If schicht.ToString.StartsWith("BLAU") Then schicht = "BLAU%"
                If schicht.ToString.StartsWith("ROT") Then schicht = "ROT%"
            End If


            Dim d As New cDienstTeilzeit

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()

                Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDienstTeilzeit WHERE  dsttz_maId=@dsttz_maId AND dsttz_dienstplan LIKE @dsttz_dienstplan AND dsttz_woTag=@dsttz_woTag", conn)
                    cmd.Parameters.AddWithValue("@dsttz_maId", id)
                    cmd.Parameters.AddWithValue("@dsttz_dienstplan", schicht)
                    cmd.Parameters.AddWithValue("@dsttz_woTag", woTag)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read

                        d.dsttz_maId = dr.Item("dsttz_maId")
                        d.dsttz_wotag = dr.Item("dsttz_wotag")
                        d.dsttz_abt = dr.Item("dsttz_abt")
                        d.dsttz_von = dr.Item("dsttz_von")
                        d.dsttz_bis = dr.Item("dsttz_bis")
                        d.dsttz_dienstplan = dr.Item("dsttz_dienstplan")
                        d.dsttz_pause = dr.Item("dsttz_pause")
                        d.dsttz_bemerkung = dr.Item("dsttz_bemerkung")

                    End While
                    dr.Close()
                End Using
            End Using
            Return d
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                          "Problem in Function 'getSeminarById'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try

        Return Nothing
    End Function


    Public Function insertDienstTeilzeit(ByVal ma As cDienstTeilzeit) As Boolean
        Dim hAuswahl As String = ""

        Dim sql As String = "INSERT INTO tblDienstTeilzeit " &
                               "( dsttz_maId, dsttz_wotag, dsttz_abt, dsttz_von, dsttz_bis, dsttz_dienstplan, dsttz_pause,dsttz_bemerkung) VALUES " &
                               "( @dsttz_maId, @dsttz_wotag, @dsttz_abt, @dsttz_von, @dsttz_bis, @dsttz_dienstplan, @dsttz_pause,@dsttz_bemerkung) "


        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dsttz_maId", ma.dsttz_maId)
                cmd.Parameters.AddWithValue("@dsttz_wotag", ma.dsttz_wotag)
                cmd.Parameters.AddWithValue("@dsttz_abt", ma.dsttz_abt)
                cmd.Parameters.AddWithValue("@dsttz_von", ma.dsttz_von)
                cmd.Parameters.AddWithValue("@dsttz_bis", ma.dsttz_bis)
                cmd.Parameters.AddWithValue("@dsttz_dienstplan", ma.dsttz_dienstplan)
                cmd.Parameters.AddWithValue("@dsttz_pause", ma.dsttz_pause)
                cmd.Parameters.AddWithValue("@dsttz_bemerkung", ma.dsttz_bemerkung)

                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
    End Function



    Public Function insertDienstSchichtZeit(dsz_dedetId, dsz_von, dsz_bis, dsz_pause, dsz_wotag) As Boolean
        Dim hAuswahl As String = ""

        Dim sql As String = "INSERT INTO [tblDienstplanSchichtenZeiten] " &
                               "( dsz_dedetId, dsz_wotag, dsz_von, dsz_bis, dsz_pause) VALUES " &
                               "( @dsz_dedetId, @dsz_wotag, @dsz_von, @dsz_bis, @dsz_pause) "




        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dsz_dedetId", dsz_dedetId)
                cmd.Parameters.AddWithValue("@dsz_von", dsz_von)
                cmd.Parameters.AddWithValue("@dsz_bis", dsz_bis)
                cmd.Parameters.AddWithValue("@dsz_pause", dsz_pause.replace(",", "."))
                cmd.Parameters.AddWithValue("@dsz_wotag", dsz_wotag)

                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
    End Function

    Public Function insertEintraegeDetails(dedet_info, dedet_niederlassung, dedet_aktiv, dedet_abt) As Boolean

        Dim sql As String = "INSERT INTO [tblDienstplanEintraegeDetails] " &
                               "(dedet_info, dedet_niederlassung, dedet_aktiv, dedet_abt ) VALUES " &
                               "(@dedet_info, @dedet_niederlassung, @dedet_aktiv, @dedet_abt) "


        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dedet_info", dedet_info)
                cmd.Parameters.AddWithValue("@dedet_niederlassung", dedet_niederlassung)
                cmd.Parameters.AddWithValue("@dedet_aktiv", dedet_aktiv)
                cmd.Parameters.AddWithValue("@dedet_abt", dedet_abt)

                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
    End Function



    Public Function updateEintraegeDetails_Benuterdef(dedet_id, dedet_bezeichnungDP, dedet_ProzentGrafik, dedet_bezeichnungExcel, dedet_bezeichnungExcel2, dedet_bezeichnungExcel3, dedet_ZeilenExcel, dedet_benutzerdefinierteSchicht, dedet_stdWieArbeitszeit, dedet_reihenfolge, dedet_TagesWechsel, dedet_WechselZuSchicht, dedet_HotKey, dedet_bgAlternativeFarbe, dedet_ExcelBgFarbe, dedet_bezLeiter, dedet_ExcelMonatFarbe, dedet_ExcelMonatBezeichnung, dedet_aktiv, dedet_splitschicht, dedet_hauptsplitschicht) As Boolean
        Dim hAuswahl As String = ""

        Dim sql As String = "UPDATE tblDienstplanEintraegeDetails " &
                               "SET dedet_bezeichnungDP=@dedet_bezeichnungDP,dedet_ProzentGrafik=@dedet_ProzentGrafik,dedet_bezeichnungExcel=@dedet_bezeichnungExcel,dedet_bezeichnungExcel2=@dedet_bezeichnungExcel2,dedet_bezeichnungExcel3=@dedet_bezeichnungExcel3,dedet_ZeilenExcel=@dedet_ZeilenExcel,dedet_benutzerdefinierteSchicht=@dedet_benutzerdefinierteSchicht,dedet_stdWieArbeitszeit=@dedet_stdWieArbeitszeit, " &
                               " dedet_reihenfolge=@dedet_reihenfolge,dedet_TagesWechsel=@dedet_TagesWechsel,dedet_WechselZuSchicht=@dedet_WechselZuSchicht,dedet_HotKey=@dedet_HotKey ,dedet_bgAlternativeFarbe=@dedet_bgAlternativeFarbe, dedet_ExcelBgFarbe=@dedet_ExcelBgFarbe,dedet_bezLeiter=@dedet_bezLeiter,dedet_ExcelMonatFarbe=@dedet_ExcelMonatFarbe,dedet_ExcelMonatBezeichnung=@dedet_ExcelMonatBezeichnung,dedet_aktiv=@dedet_aktiv, dedet_splitschicht=@dedet_splitschicht, dedet_hauptsplitschicht=@dedet_hauptsplitschicht " &
                               " WHERE dedet_id=@dedet_id "




        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dedet_id", dedet_id)
                cmd.Parameters.AddWithValue("@dedet_bezeichnungDP", dedet_bezeichnungDP)
                cmd.Parameters.AddWithValue("@dedet_ProzentGrafik", dedet_ProzentGrafik)
                cmd.Parameters.AddWithValue("@dedet_bezeichnungExcel", dedet_bezeichnungExcel)
                cmd.Parameters.AddWithValue("@dedet_bezeichnungExcel2", dedet_bezeichnungExcel2)
                cmd.Parameters.AddWithValue("@dedet_bezeichnungExcel3", dedet_bezeichnungExcel3)
                cmd.Parameters.AddWithValue("@dedet_ZeilenExcel", dedet_ZeilenExcel)
                cmd.Parameters.AddWithValue("@dedet_benutzerdefinierteSchicht", dedet_benutzerdefinierteSchicht)
                cmd.Parameters.AddWithValue("@dedet_stdWieArbeitszeit", dedet_stdWieArbeitszeit)
                cmd.Parameters.AddWithValue("@dedet_reihenfolge", dedet_reihenfolge)
                cmd.Parameters.AddWithValue("@dedet_TagesWechsel", dedet_TagesWechsel)
                cmd.Parameters.AddWithValue("@dedet_WechselZuSchicht", dedet_WechselZuSchicht)
                cmd.Parameters.AddWithValue("@dedet_HotKey", dedet_HotKey)
                cmd.Parameters.AddWithValue("@dedet_bgAlternativeFarbe", dedet_bgAlternativeFarbe)
                cmd.Parameters.AddWithValue("@dedet_ExcelBgFarbe", dedet_ExcelBgFarbe)
                cmd.Parameters.AddWithValue("@dedet_bezLeiter", dedet_bezLeiter)
                cmd.Parameters.AddWithValue("@dedet_ExcelMonatFarbe", dedet_ExcelMonatFarbe)
                cmd.Parameters.AddWithValue("@dedet_ExcelMonatBezeichnung", dedet_ExcelMonatBezeichnung)
                cmd.Parameters.AddWithValue("@dedet_aktiv", dedet_aktiv)
                cmd.Parameters.AddWithValue("@dedet_splitschicht", dedet_splitschicht)
                cmd.Parameters.AddWithValue("@dedet_hauptsplitschicht", dedet_hauptsplitschicht)

                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
    End Function

    Public Function delEintraegeDetails(ByVal dedet_id As Integer) As Boolean

        Dim sql = "DELETE FROM tblDienstplanEintraegeDetails WHERE dedet_id=@dedet_id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dedet_id", dedet_id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function delDienstNichtAnwesendKW_ByMaID(ByVal id As Integer) As Boolean
        Dim hAuswahl As String = ""
        'tblDienstNichtAnwesend
        'Änderung
        Dim sql = "DELETE FROM tblDienstNichtAnwesendKW WHERE dstnk_dstmaId=@dstnk_dstmaId"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstnk_dstmaId", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function delDienstNichtAnwesend_ByMaID(ByVal id As Integer) As Boolean
        Dim hAuswahl As String = ""
        'tblDienstNichtAnwesend
        'Änderung
        Dim sql = "DELETE FROM tblDienstNichtAnwesend WHERE dstna_dstmaId=@dstna_dstmaId"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstna_dstmaId", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False

    End Function
    Public Function delDienstMitarbeiter(ByVal id As Integer) As Boolean
        Dim hAuswahl As String = ""

        'Änderung
        Dim sql = "DELETE FROM tblDienstMitarb WHERE dstma_id=@dstma_id"

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstma_id", id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function getAllDienstMA(ByVal niederlassung As String, ByVal montag As Date, Optional where As String = "") As List(Of cDienstMA)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim data As New List(Of cDienstMA)
                'MsgBox("SELECT * FROM tblDienstMitarb WHERE dstma_niederlassung = '" & niederlassung & "' " & where & " ORDER BY dstma_kuerzel ASC")
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblDienstMitarb INNER JOIN tblMitarbeiter ON mit_id=dstma_mitId  WHERE    dstma_inaktiv=0 AND (mit_gekuendigt =0  OR mit_kuendigungsdatum>='" & montag.ToShortDateString & "') AND dstma_niederlassung = '" & niederlassung & "' " & where & " ORDER BY dstma_kuerzel ASC", conn)
                        'cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim d As New cDienstMA

                            d.dstma_id = dr.Item("dstma_id")
                            d.dstma_mitId = dr.Item("dstma_mitId")
                            d.dstma_abteilung = dr.Item("dstma_abteilung")
                            d.dstma_funktion = dr.Item("dstma_funktion")
                            d.dstma_arbvh = dr.Item("dstma_arbvh")
                            d.dstma_wochenStunden = dr.Item("dstma_wochenStunden")
                            d.dstma_stammSchicht = dr.Item("dstma_stammSchicht")
                            d.dstma_kuerzel = dr.Item("dstma_kuerzel")
                            d.dstma_farbe = dr.Item("dstma_farbe")
                            d.dstma_muster = dr.Item("dstma_muster")
                            d.dstma_4wo = dr.Item("dstma_4wo")
                            d.dstma_WEStdRegelAZ = dr.Item("dstma_WEStdRegelAZ")
                            d.dstma_land = dr.Item("dstma_land")


                            If Not dr.Item("dstma_TageProWoche") Is DBNull.Value Then d.dstma_TageProWoche = dr.Item("dstma_TageProWoche")


                            d.dstma_inaktiv = dr.Item("dstma_inaktiv")
                            If Not dr.Item("dstma_TzFeiertageStd") Is DBNull.Value Then d.dstma_TzFeiertageStd = dr.Item("dstma_TzFeiertageStd")


                            data.Add(d)
                        End While
                        dr.Close()
                    End Using
                End Using
                Return data
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getAllDienstMA'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function getAllDienstDetails(niederlassung) As List(Of cDienstDetails)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim data As New List(Of cDienstDetails)

                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblDienstplanEintraegeDetails as det INNER JOIN [tblDienstplanSchichtenZeiten] as sz ON det.dedet_id=sz.dsz_dedetId WHERE dedet_niederlassung=@dedet_niederlassung AND dedet_splitschicht=0 ", conn)
                        cmd.Parameters.AddWithValue("@dedet_niederlassung", niederlassung)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim d As New cDienstDetails

                            d.dedet_id = dr.Item("dedet_id")
                            d.dedet_abt = dr.Item("dedet_abt")
                            d.dedet_info = dr.Item("dedet_info")
                            d.dedet_von = dr.Item("dsz_von")
                            d.dedet_bis = dr.Item("dsz_bis")
                            d.dedet_pause = IIf(IsNumeric(dr.Item("dsz_pause")), dr.Item("dsz_pause"), 0)
                            d.dedet_woTag = dr.Item("dsz_woTag")

                            '    d.dedet_bezeichnungDP = dr.Item("dedet_bezeichnungDP")
                            '    d.dedet_ProzentGrafik = dr.Item("dedet_ProzentGrafik")
                            ''   d.dedet_bezeichnungExcel = dr.Item("dedet_bezeichnungExcel")
                            '   d.dedet_ZeilenExcel = dr.Item("dedet_ZeilenExcel")


                            data.Add(d)
                        End While
                        dr.Close()
                    End Using
                End Using
                Return data
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getAllDienstDetails'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function



    Public Function getDienstDetailsWoTagByArt(niederlassung, art) As List(Of cDienstDetails)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim data As New List(Of cDienstDetails)

                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblDienstplanEintraegeDetails as det INNER JOIN [tblDienstplanSchichtenZeiten] as sz ON det.dedet_id=sz.dsz_dedetId WHERE dedet_niederlassung=@dedet_niederlassung AND dedet_abt=@dedet_abt", conn)
                        cmd.Parameters.AddWithValue("@dedet_niederlassung", niederlassung)
                        cmd.Parameters.AddWithValue("@dedet_abt", art)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim d As New cDienstDetails

                            d.dedet_id = dr.Item("dedet_id")
                            d.dedet_abt = dr.Item("dedet_abt")
                            d.dedet_info = dr.Item("dedet_info")
                            d.dedet_von = dr.Item("dsz_von")
                            d.dedet_bis = dr.Item("dsz_bis")
                            d.dedet_pause = IIf(IsNumeric(dr.Item("dsz_pause")), dr.Item("dsz_pause"), 0)
                            d.dedet_woTag = dr.Item("dsz_woTag")

                            data.Add(d)
                        End While
                        dr.Close()
                    End Using
                End Using
                Return data
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getAllDienstDetails'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function getDienstDetailsWoTagByArtAndWoTag(niederlassung, art, wotag) As cDienstDetails
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                '  MsgBox("SELECT TOP 1 * FROM tblDienstplanEintraegeDetails as det INNER JOIN [tblDienstplanSchichtenZeiten] as sz ON det.dedet_id=sz.dsz_dedetId WHERE dedet_niederlassung=" & niederlassung & " AND dedet_abt=" & art & "  AND dsz_woTag=" & wotag & " ")
                Dim d As New cDienstDetails
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT TOP 1 * FROM tblDienstplanEintraegeDetails as det INNER JOIN [tblDienstplanSchichtenZeiten] as sz ON det.dedet_id=sz.dsz_dedetId WHERE dedet_niederlassung=@dedet_niederlassung AND dedet_abt=@dedet_abt AND dsz_woTag=@dsz_woTag", conn)
                        cmd.Parameters.AddWithValue("@dedet_niederlassung", niederlassung)
                        cmd.Parameters.AddWithValue("@dedet_abt", art)
                        cmd.Parameters.AddWithValue("@dsz_woTag", wotag)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read


                            d.dedet_id = dr.Item("dedet_id")
                            d.dedet_abt = dr.Item("dedet_abt")
                            d.dedet_info = dr.Item("dedet_info")
                            d.dedet_von = dr.Item("dsz_von")
                            d.dedet_bis = dr.Item("dsz_bis")
                            d.dedet_pause = IIf(IsNumeric(dr.Item("dsz_pause")), dr.Item("dsz_pause"), 0)
                            d.dedet_woTag = dr.Item("dsz_woTag")


                        End While
                        dr.Close()
                    End Using
                End Using
                Return d
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getAllDienstDetails'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing

    End Function
    Public Function getAllDienstDetailsNurSchichten2() As List(Of cDienstDetails)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim data As New List(Of cDienstDetails)

                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT * FROM tblDienstplanEintraegeDetails ", conn)
                        'cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim d As New cDienstDetails

                            d.dedet_id = dr.Item("dedet_id")
                            d.dedet_abt = dr.Item("dedet_abt")
                            d.dedet_info = dr.Item("dedet_info")
                            d.dedet_von = dr.Item("dedet_von")
                            d.dedet_bis = dr.Item("dedet_bis")
                            d.dedet_pause = IIf(IsNumeric(dr.Item("dsz_pause")), dr.Item("dsz_pause"), 0)

                            data.Add(d)
                        End While
                        dr.Close()
                    End Using
                End Using
                Return data
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle),
                                              "Problem in Function 'getAllDienstDetails'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function
    Public Function getAllMA(niederlassung) As List(Of cMAShort)

        If niederlassung = "BEIDE" Then
            niederlassung = ""
        ElseIf niederlassung <> "ATILLA" Then
            niederlassung = "AND mit_firma='VERAG'"
        Else
            niederlassung = "AND mit_firma='ATILLA'"
        End If
        Try
            Dim data As New List(Of cMAShort)

            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand("SELECT * FROM tblMitarbeiter WHERE 1=1 " & niederlassung & " ORDER BY mit_nname DESC ", conn)
                    'cmd.Parameters.AddWithValue("@semi_id", id)
                    Dim dr = cmd.ExecuteReader()
                    While dr.Read
                        Dim d As New cMAShort

                        d.mit_id = dr.Item("mit_id")
                        d.mit_vname = dr.Item("mit_vname")
                        d.mit_nname = dr.Item("mit_nname")

                        data.Add(d)
                    End While
                    dr.Close()
                End Using
            End Using
            Return data
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'getAllMA'")
            If antwort <> MsgBoxResult.Retry Then
                'Programm wird beendet
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                Environment.Exit(0)
            End If
        End Try


        Return Nothing
    End Function

    Public Function getDstMAShort() As List(Of cMAShort)
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim data As New List(Of cMAShort)

                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand("SELECT mit_id,dstma_mitId,mit_vname,mit_nname,dstma_reihenfolge FROM tblDienstMitarb INNER JOIN tblMitarbeiter ON mit_id=dstma_mitId  WHERE mit_firma='VERAG' ORDER BY dstma_reihenfolge DESC, mit_nname DESC ", conn)
                        'cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            Dim d As New cMAShort

                            d.mit_id = dr.Item("dstma_mitId")
                            d.mit_vname = dr.Item("mit_vname")
                            d.mit_nname = dr.Item("mit_nname")

                            data.Add(d)
                        End While
                        dr.Close()
                    End Using
                End Using
                Return data
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function 'getAllMA'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function


    Public Function getDstMA(maId) As cDienstMA
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim d As New cDienstMA
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(" SELECT dstma_id,dstma_mitId,dstma_kuerzel, dstma_abteilung,dstma_funktion,dstma_arbvh,dstma_wochenStunden,dstma_stammSchicht,dstma_reihenfolge,dstma_farbe,dstma_TageProWoche,dstma_muster,dstma_4wo,dstma_WEStdRegelAZ,dstma_inaktiv,dstma_TzFeiertageStd, dstma_land FROM tblDienstMitarb INNER JOIN tblMitarbeiter ON mit_id=dstma_mitId WHERE dstma_id=" & maId & " ", conn)
                        'cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            d.dstma_id = dr.Item("dstma_id")
                            d.dstma_mitId = dr.Item("dstma_mitId")
                            d.dstma_kuerzel = dr.Item("dstma_kuerzel")
                            d.dstma_abteilung = dr.Item("dstma_abteilung")
                            d.dstma_arbvh = dr.Item("dstma_arbvh")
                            d.dstma_stammSchicht = dr.Item("dstma_stammSchicht")
                            d.dstma_wochenStunden = dr.Item("dstma_wochenStunden")
                            d.dstma_reihenfolge = dr.Item("dstma_reihenfolge")
                            d.dstma_farbe = dr.Item("dstma_farbe")
                            d.dstma_muster = dr.Item("dstma_muster")
                            d.dstma_4wo = dr.Item("dstma_4wo")
                            d.dstma_WEStdRegelAZ = dr.Item("dstma_WEStdRegelAZ")
                            d.dstma_land = dr.Item("dstma_land")

                            If Not dr.Item("dstma_TageProWoche") Is DBNull.Value Then d.dstma_TageProWoche = dr.Item("dstma_TageProWoche")

                            d.dstma_inaktiv = dr.Item("dstma_inaktiv")
                            If Not dr.Item("dstma_TzFeiertageStd") Is DBNull.Value Then d.dstma_TzFeiertageStd = dr.Item("dstma_TzFeiertageStd")

                        End While
                        dr.Close()
                    End Using
                End Using
                Return d
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function


    Public Function getDstMAByDstMaId(dstmaId) As cDienstMA
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim d As New cDienstMA

                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(" SELECT dstma_id,dstma_mitId,dstma_kuerzel, dstma_abteilung,dstma_funktion,dstma_arbvh,dstma_wochenStunden,dstma_stammSchicht,dstma_reihenfolge,dstma_farbe,dstma_TageProWoche,dstma_inaktiv,dstma_TzFeiertageStd, dstma_land  FROM tblDienstMitarb WHERE dstma_id=" & dstmaId & " ", conn)
                        'cmd.Parameters.AddWithValue("@semi_id", id)
                        Dim dr = cmd.ExecuteReader()
                        While dr.Read
                            d.dstma_id = dr.Item("dstma_id")
                            d.dstma_mitId = dr.Item("dstma_mitId")
                            d.dstma_kuerzel = dr.Item("dstma_kuerzel")
                            d.dstma_abteilung = dr.Item("dstma_abteilung")
                            d.dstma_arbvh = dr.Item("dstma_arbvh")
                            d.dstma_stammSchicht = dr.Item("dstma_stammSchicht")
                            d.dstma_wochenStunden = dr.Item("dstma_wochenStunden")
                            d.dstma_reihenfolge = dr.Item("dstma_reihenfolge")
                            d.dstma_farbe = dr.Item("dstma_farbe")
                            If Not dr.Item("dstma_TageProWoche") Is DBNull.Value Then d.dstma_TageProWoche = dr.Item("dstma_TageProWoche")

                            d.dstma_inaktiv = dr.Item("dstma_inaktiv")
                            If Not dr.Item("dstma_TzFeiertageStd") Is DBNull.Value Then d.dstma_TzFeiertageStd = dr.Item("dstma_TzFeiertageStd")

                            If Not dr.Item("dstma_land") Is DBNull.Value Then d.dstma_land = dr.Item("dstma_land")

                        End While
                        dr.Close()
                    End Using
                End Using
                Return d
            Catch ex As Exception
                Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function

    Public Function delDstEintraege(von As Date, bis As Date, niederlassung As String, Optional onlyFehlzeiten As Boolean = False) As Boolean

        Dim sqlFehlzeiten As String = ""
        If onlyFehlzeiten Then sqlFehlzeiten &= " AND ([dstetr_art] = 'URL' OR [dstetr_art] = 'DR' OR [dstetr_art] = 'BS') "

        Dim sql = "DELETE FROM tblDienstplanEintraege " &
                   "WHERE dstetr_niederlassung= '" & niederlassung & "' AND dstetr_datum BETWEEN @von AND @bis " & sqlFehlzeiten
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                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
    End Function

    Public Function delDstEintraegeMaId(von As Date, bis As Date, niederlassung As String, dstetr_dstmaId As Integer, Optional onlyFehlzeiten As Boolean = False) As Boolean
        Dim sqlFehlzeiten = ""
        If onlyFehlzeiten Then sqlFehlzeiten &= " AND ([dstetr_art] = 'URL' OR [dstetr_art] = 'DR' OR [dstetr_art] = 'BS') "
        Dim sql = "DELETE FROM tblDienstplanEintraege " &
                   "WHERE dstetr_niederlassung= '" & niederlassung & "' AND dstetr_datum BETWEEN @von AND @bis AND dstetr_dstmaId=@dstetr_dstmaId" & sqlFehlzeiten
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                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
    End Function

    Public Function delOnlyDstEintraegeSplitschichtenMaId(datum, niederlassung, dstetr_dstmaId, Optional dstetr_id = -1) As Boolean


        Dim sql = "DELETE FROM tblDienstplanEintraege " &
                   "WHERE dstetr_niederlassung= '" & niederlassung & "' AND dstetr_datum = @datum AND dstetr_dstmaId=@dstetr_dstmaId AND dstetr_splitschicht = 1"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@datum", datum)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim löschen")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function delDstEintraegeSplitschichtenId(dstetr_id) As Boolean

        Dim sql = "DELETE FROM tblDienstplanEintraege " &
                   "WHERE  dstetr_id = @dstetr_id AND dstetr_splitschicht = 1"
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)
                Try
                    cmd.ExecuteNonQuery()
                    Return True
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gelöscht werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim löschen")
                End Try
            End Using
        End Using
        Return False
    End Function




    Public Function getMaIDFromMitID(mit_id As Integer) As String
        Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
        Return SQL.getValueTxtBySql("Select [dstma_id]  FROM [tblDienstMitarb] INNER JOIN tblMitarbeiter On mit_id=dstma_mitId WHERE mit_id=" & mit_id, "ADMIN")

    End Function



    Public Function delDstEintraegeAllMaId(dstetr_dstmaId) As Boolean
        Dim sql = "DELETE FROM tblDienstplanEintraege " &
                   "WHERE dstetr_dstmaId=@dstetr_dstmaId "
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                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
    End Function

    Public Function loadDGV(ByVal sql As String) As DataTable
        While True  'Endlosschleife; wird verlassen durch Return oder Application.Exit()
            Try
                Dim myTable = New DataTable()
                Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                    Using cmd As New SqlCommand(sql, conn)
                        Dim dr = cmd.ExecuteReader()
                        myTable.Load(dr)
                        dr.Close()
                    End Using
                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'")
                If antwort <> MsgBoxResult.Retry Then
                    'Programm wird beendet
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
                    Environment.Exit(0)
                End If
            End Try
        End While

        Return Nothing
    End Function


    Public Function getValueTxtBySql(ByVal sql As String, Optional conn_art As String = "SDL") As String
        Dim s As String = ""
        Try
            'MsgBox("1")
            Dim conn As New SqlConnection
            Select Case conn_art
                Case "FMZOLL" : conn = cSqlDb.GetNewOpenConnectionFMZOLL()
                Case "ADMIN" : conn = cSqlDb.GetNewOpenConnection()
            End Select

            Using cmd As New SqlCommand(sql, conn)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                If dr.HasRows Then
                    dr.Read()
                    If Not dr.Item(0) Is DBNull.Value Then
                        s = CStr(dr.Item(0))
                    End If
                End If
                dr.Close()
            End Using
            conn.Close()

            Return s
        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message)
        End Try
        'MsgBox("3")
        Return ""
    End Function


    Public Function updateInsertByDienstEintrag(ByVal daten As cDienstEintrag, niederlassung As String, Optional dstetr_id As Integer = -1, Optional enableMultiEntriesPerArt As Boolean = True) As Boolean

        Dim SQLclause As String = ""

        If enableMultiEntriesPerArt Then
            SQLclause &= " AND dstetr_art=@dstetr_art"
        End If

        If dstetr_id > 0 Then
            SQLclause = " AND dstetr_id=@dstetr_id"
        End If

        Dim sql As String = " begin tran" &
                            " if exists (select  * from tblDienstplanEintraege with (updlock,serializable) where dstetr_datum = @dstetr_datum AND dstetr_dstmaId=@dstetr_dstmaId AND dstetr_niederlassung=@dstetr_niederlassung" & SQLclause & ") " &
                                   " begin " &
                                   " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_dstmaId=@dstetr_dstmaId, dstetr_art=@dstetr_art, dstetr_von=@dstetr_von, dstetr_bis=@dstetr_bis, dstetr_grund=@dstetr_grund, dstetr_info=@dstetr_info,dstetr_niederlassung=@dstetr_niederlassung,dstetr_pause=@dstetr_pause,dstetr_hauptsplitschicht=@dstetr_hauptsplitschicht,dstetr_splitschicht=@dstetr_splitschicht " &
                                   " WHERE dstetr_datum = @dstetr_datum AND dstetr_dstmaId=@dstetr_dstmaId AND dstetr_niederlassung=@dstetr_niederlassung" & SQLclause &
                                " End " &
                            " Else " &
                            " begin " &
                                "INSERT INTO tblDienstplanEintraege " &
                                "( dstetr_dstmaId, dstetr_datum, dstetr_art, dstetr_von, dstetr_bis, dstetr_grund, dstetr_info, dstetr_niederlassung,dstetr_pause,dstetr_hauptsplitschicht,dstetr_splitschicht) VALUES " &
                                "( @dstetr_dstmaId, @dstetr_datum, @dstetr_art, @dstetr_von, @dstetr_bis, @dstetr_grund, @dstetr_info,@dstetr_niederlassung,@dstetr_pause,@dstetr_hauptsplitschicht,@dstetr_splitschicht )" &
                            " End " &
                            " commit tran "

        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_id", daten.dstetr_id)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", daten.dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@dstetr_datum", daten.dstetr_datum)
                cmd.Parameters.AddWithValue("@dstetr_art", daten.dstetr_art)
                cmd.Parameters.AddWithValue("@dstetr_von", daten.dstetr_von)
                cmd.Parameters.AddWithValue("@dstetr_bis", daten.dstetr_bis)
                cmd.Parameters.AddWithValue("@dstetr_pause", CDbl(daten.dstetr_pause))
                cmd.Parameters.AddWithValue("@dstetr_grund", daten.dstetr_grund)
                cmd.Parameters.AddWithValue("@dstetr_info", daten.dstetr_info)
                cmd.Parameters.AddWithValue("@dstetr_niederlassung", niederlassung)
                cmd.Parameters.AddWithValue("@dstetr_hauptsplitschicht", daten.dstetr_hauptsplitschicht)
                cmd.Parameters.AddWithValue("@dstetr_splitschicht", daten.dstetr_splitschicht)

                'MsgBox(daten.dstetr_dstmaId)
                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function getDienstEintragList(datum As Date, niederlassung As String) As List(Of cDienstEintrag)
        Dim sql As String = " SELECT * FROM tblDienstplanEintraege WHERE dstetr_datum = @dstetr_datum AND dstetr_niederlassung=@dstetr_niederlassung "
        Dim daten As List(Of cDienstEintrag) = Nothing
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dstetr_datum", datum)
                cmd.Parameters.AddWithValue("@dstetr_niederlassung", niederlassung)


                'MsgBox(daten.dstetr_dstmaId)
                Try
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read()
                            Dim d As New cDienstEintrag

                            d.dstetr_datum = dr.Item("dstetr_id")
                            d.dstetr_dstmaId = dr.Item("dstetr_dstmaId")
                            d.dstetr_datum = dr.Item("dstetr_datum")
                            d.dstetr_art = dr.Item("dstetr_art")
                            d.dstetr_von = dr.Item("dstetr_von")
                            d.dstetr_bis = dr.Item("dstetr_bis")
                            d.dstetr_pause = dr.Item("dstetr_pause")
                            d.dstetr_grund = dr.Item("dstetr_grund")
                            d.dstetr_info = dr.Item("dstetr_info")
                            '     d.dstetr_niederlassung = dr.Item("niederlassung")


                            dr.Item(0).ToString()
                        End While

                    End If
                    dr.Close()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
        End Using
        Return daten
    End Function

    Public Function getDienstEintragListBenSchicht(datum As Date, niederlassung As String) As List(Of cDienstEintrag)
        Dim sql As String = " SELECT * FROM tblDienstplanEintraege WHERE dstetr_datum = @dstetr_datum AND dstetr_niederlassung=@dstetr_niederlassung AND [dstetr_art] IN (SELECT case WHEN dedet_bezeichnungDP<>'' then dedet_bezeichnungDP ELSE dedet_abt END   FROM [tblDienstplanEintraegeDetails] WHERE dedet_niederlassung=@dstetr_niederlassung AND dedet_benutzerdefinierteSchicht=1)"

        Dim daten As New List(Of cDienstEintrag)
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@dstetr_datum", datum)
                cmd.Parameters.AddWithValue("@dstetr_niederlassung", niederlassung)


                'MsgBox(daten.dstetr_dstmaId)
                Try
                    Dim dr = cmd.ExecuteReader()
                    If dr.HasRows Then
                        While dr.Read()
                            Dim d As New cDienstEintrag

                            d.dstetr_datum = dr.Item("dstetr_id")
                            d.dstetr_dstmaId = dr.Item("dstetr_dstmaId")
                            d.dstetr_datum = dr.Item("dstetr_datum")
                            d.dstetr_art = dr.Item("dstetr_art")
                            d.dstetr_von = dr.Item("dstetr_von")
                            d.dstetr_bis = dr.Item("dstetr_bis")
                            d.dstetr_pause = dr.Item("dstetr_pause")
                            d.dstetr_grund = dr.Item("dstetr_grund")
                            d.dstetr_info = dr.Item("dstetr_info")
                            '     d.dstetr_niederlassung = dr.Item("niederlassung")
                            daten.Add(d)

                        End While

                    End If
                    dr.Close()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
        End Using
        Return daten
    End Function



    Public Function updateDienstEintragArtChange(ByVal dstetr_id As Integer, ByVal dstetr_datum As String, ByVal abteilung As String, ByVal von As String, ByVal bis As String, pause As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_art=@abteilung, dstetr_von=@von, dstetr_bis=@bis , dstetr_pause=@dstetr_pause" &
                                   " WHERE dstetr_id=@dstetr_id "
        'dedet_abt
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@abteilung", abteilung)
                cmd.Parameters.AddWithValue("@dstetr_datum", dstetr_datum)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@dstetr_pause", CDbl(pause))

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function updateDienstEintragInfo(ByVal dstetr_id As Integer, ByVal dstetr_info As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_info=@dstetr_info " &
                                   " WHERE dstetr_id=@dstetr_id "
        'dedet_abt
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_info", dstetr_info)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function updateDienstEintragArtChangeByDstMaId(ByVal dstetr_dstmaId As Integer, ByVal dstetr_datum As String, ByVal abteilung As String, ByVal von As String, ByVal bis As String, ByVal dstetr_info As String, ByVal pause As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_art=@abteilung, dstetr_von=@von, dstetr_bis=@bis, dstetr_info=@dstetr_info, dstetr_pause=@pause " &
                                   " WHERE dstetr_datum=@dstetr_datum AND dstetr_dstmaId=@dstetr_dstmaId "
        'dedet_abt
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@abteilung", abteilung)
                cmd.Parameters.AddWithValue("@dstetr_datum", dstetr_datum)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@dstetr_info", dstetr_info)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@pause", CDbl(pause))

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function


    Public Function updateDienstEintragArtChangeByMitId(ByVal dstetr_dstmaId As Integer, ByVal dstetr_datum As Date, ByVal abteilung As String, ByVal von As String, ByVal bis As String, ByVal pause As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_art=@abteilung, dstetr_von=@von, dstetr_bis=@bis, dstetr_pause=@pause " &
                                   " WHERE dstetr_dstmaId=@dstetr_dstmaId AND dstetr_datum=@dstetr_datum "
        'dedet_abt
        '  MsgBox(" UPDATE tblDienstplanEintraege " &
        '                            " SET dstetr_art=" & abteilung & ", dstetr_von=" & von & ", dstetr_bis=" & bis & " " &
        '                            " WHERE dstetr_dstmaId=" & dstetr_dstmaId & " AND dstetr_datum=" & dstetr_datum.ToShortDateString & "")
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@abteilung", abteilung)
                cmd.Parameters.AddWithValue("@dstetr_datum", dstetr_datum.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@pause", CDbl(pause))

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function updateDienstEintragVonBisBemWOCHEChangeByMitId(ByVal dstetr_dstmaId As Integer, ByVal dstetr_datum_von As Date, ByVal dstetr_datum_bis As Date, ByVal von As String, ByVal bis As String, ByVal dstetr_info As String, dstetr_pause As String, niederlassung As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_info=@dstetr_info, dstetr_von=@von, dstetr_bis=@bis , dstetr_pause=@dstetr_pause " &
                                   " WHERE dstetr_dstmaId=@dstetr_dstmaId AND (dstetr_datum BETWEEN @dstetr_datum_von AND @dstetr_datum_bis) AND [dstetr_art] IN (SELECT case WHEN dedet_bezeichnungDP<>'' then dedet_bezeichnungDP ELSE dedet_abt END   FROM [tblDienstplanEintraegeDetails] WHERE dedet_niederlassung=@dstetr_niederlassung AND dedet_benutzerdefinierteSchicht=1)"
        'dedet_abt


        '  MsgBox(" UPDATE tblDienstplanEintraege " &
        '                            " SET dstetr_art=" & abteilung & ", dstetr_von=" & von & ", dstetr_bis=" & bis & " " &
        '                            " WHERE dstetr_dstmaId=" & dstetr_dstmaId & " AND dstetr_datum=" & dstetr_datum.ToShortDateString & "")
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_datum_von", dstetr_datum_von.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstetr_datum_bis", dstetr_datum_bis.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstetr_pause", CDbl(dstetr_pause))
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@dstetr_info", dstetr_info)
                cmd.Parameters.AddWithValue("@dstetr_niederlassung", niederlassung)


                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function updateDienstEintragArtBemerkungChange(ByVal dstetr_id As Integer, ByVal von As String, ByVal bis As String, ByVal dstetr_info As String, dstetr_pause As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_von=@von, dstetr_bis=@bis, dstetr_info=@dstetr_info ,dstetr_pause=@dstetr_pause" &
                                   " WHERE dstetr_id=@dstetr_id "
        'dedet_abt
        '   MsgBox(CDbl(dstetr_pause))
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)
                cmd.Parameters.AddWithValue("@von", von)
                cmd.Parameters.AddWithValue("@bis", bis)
                cmd.Parameters.AddWithValue("@dstetr_pause", CDbl(dstetr_pause))
                cmd.Parameters.AddWithValue("@dstetr_info", dstetr_info)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function getMaIdByEtrId(ByVal dstetr_id As Integer) As String
        Dim sql As String = " SELECT dstetr_dstmaId" &
                                   " FROM tblDienstplanEintraege " &
                                   " WHERE dstetr_id=@dstetr_id "
        Dim maId As String = ""
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_id", dstetr_id)
                Try
                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    If dr.HasRows Then
                        dr.Read()
                        maId = dr.Item(0).ToString
                    End If
                    dr.Close()
                Catch ex As SqlException
                    MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Datensatz kann nicht gespeichert werden!" & vbCrLf & vbCrLf & ex.Message, vbExclamation, "Fehler beim Speichern Programm")
                End Try
            End Using
        End Using
        Return maId
    End Function


    Public Function updateDienstBemerkungChange(dstetr_dstmaId As Integer, ByVal dstetr_datum_von As Date, ByVal dstetr_datum_bis As Date, ByVal dstetr_info As String, niederlassung As String) As Boolean
        Dim sql As String = " UPDATE tblDienstplanEintraege " &
                                   " SET dstetr_info=@dstetr_info " &
                                   " WHERE dstetr_dstmaId=@dstetr_dstmaId AND (dstetr_datum BETWEEN @dstetr_datum_von AND @dstetr_datum_bis) AND  [dstetr_art] IN (SELECT case WHEN dedet_bezeichnungDP<>'' then dedet_bezeichnungDP ELSE dedet_abt END   FROM [tblDienstplanEintraegeDetails] WHERE dedet_niederlassung=@niederlassung AND dedet_benutzerdefinierteSchicht=1)"
        'dedet_abt
        ' 
        Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
            Using cmd As New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@dstetr_dstmaId", dstetr_dstmaId)
                cmd.Parameters.AddWithValue("@dstetr_datum_von", dstetr_datum_von.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstetr_datum_bis", dstetr_datum_bis.ToShortDateString)
                cmd.Parameters.AddWithValue("@dstetr_info", dstetr_info)
                cmd.Parameters.AddWithValue("@niederlassung", niederlassung)

                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 Programm")
                End Try
            End Using
        End Using
        Return False
    End Function

    Public Function getDstStunden(maId As Integer, datum_von As Date, datum_bis As Date, niederlassung As String, Optional land As String = "", Optional onlySplitschichten As Boolean = False) As Double
        Try
            Dim stunden As Double
            Dim rgx As New Regex("^\d{2}:\d{2}$")
            Dim whereSQL As String = ""
            If onlySplitschichten Then
                whereSQL = " AND dedet_splitschicht = 1"
            Else
                whereSQL = " AND dedet_splitschicht = 0"
            End If


            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                'Using cmd As New SqlCommand(" SELECT dstetr_datum,dstetr_von,dstetr_bis,dstetr_pause,dstetr_art FROM tblDienstplanEintraege WHERE dstetr_dstmaId=" & maId & " AND dstetr_datum BETWEEN '" & datum_von.ToShortDateString & "' AND '" & datum_bis.ToShortDateString & "' AND (dstetr_art LIKE 'ZOLL%' OR dstetr_art IN('QS','KS','URL','DBL' ,'NACHT','SO','BS','DR')) ", conn)
                Using cmd As New SqlCommand(" SELECT dstetr_datum,dstetr_von,dstetr_bis,dstetr_pause,dstetr_art FROM tblDienstplanEintraege WHERE dstetr_dstmaId=" & maId & " AND dstetr_datum BETWEEN '" & datum_von.ToShortDateString & "' AND '" & datum_bis.ToShortDateString & "' AND replace( replace([dstetr_art] ,'BS','DR') ,'SO','DR') IN (SELECT case WHEN dedet_bezeichnungDP<>'' then dedet_bezeichnungDP ELSE dedet_abt END   FROM [tblDienstplanEintraegeDetails] WHERE dedet_niederlassung=@niederlassung AND ( dedet_benutzerdefinierteSchicht=1 OR dedet_stdWieArbeitszeit=1 )" & whereSQL & ") ", conn)
                    cmd.Parameters.AddWithValue("@niederlassung", niederlassung)

                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    While dr.Read
                        ' MsgBox(rgx.IsMatch(dsttz_von))
                        Dim s As Double = 0

                        'Feiertage: Wochenstunden abziehen

                        ' 
                        Dim datum As Date = CDate(dr.Item("dstetr_datum"))
                        Dim woTag = datum.ToString("ddd", New CultureInfo("de-DE")).ToUpper

                        Dim FT As New VERAG_PROG_ALLGEMEIN.cFeiertage(datum.Year)
                        '  If FT.isFeiertag(datum) And woTag <> "SO" And woTag <> "SA" Then
                        '     s +=
                        ' End If


                        Dim von As String = dr.Item("dstetr_von")
                        Dim bis As String = dr.Item("dstetr_bis")

                        If von <> "" And bis <> "" Then

                            '   If False Then

                            If Not von.Contains(":") AndAlso von.Length = 2 AndAlso IsNumeric(von) Then

                                von &= ":00"

                            End If
                            If Not bis.Contains(":") AndAlso bis.Length = 2 AndAlso IsNumeric(bis) Then
                                bis &= ":00"
                            End If

                            'End If
                            If cPF.isValidTime(von) And cPF.isValidTime(bis) Then ' Wenn Format stimmt
                                s += cPF.getDiff(von, bis, niederlassung, (FT.isFeiertag(datum, IIf(land <> "", land, cDienstSettings.getLand(niederlassung))) Or woTag = "SO"))
                                If IsNumeric(dr.Item("dstetr_pause")) Then
                                    s -= CDbl(dr.Item("dstetr_pause"))

                                    'Wenn Feiertag oder Sonntag, werden die Stunden (von-bis) doppelt gerechent, also müssen die Pausenstunden doppelt abgezoben werden:
                                    If (FT.isFeiertag(datum, IIf(land <> "", land, cDienstSettings.getLand(niederlassung))) Or woTag = "SO") Then s -= CDbl(dr.Item("dstetr_pause"))

                                End If
                            Else

                                MsgBox("Falsches Format, Zeitberechnung ist fehlerhaft!")

                            End If
                            '  If dr.Item("dstetr_art") = "ZA" Then s *= -1 ' ZA wird abgezogen
                            ' If FT.isFeiertag(datum) Or woTag = "SO" Then s *= 2 ' An Sonn- und Feiertagen zählen die Stunden doppelt!

                        End If
                        stunden += s
                    End While
                    dr.Close()
                End Using
            End Using
            Return stunden
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)

            End If
        End Try

        Return 0
    End Function

    Public Function getCountEntrys(maId As Integer, datum_von As Date, datum_bis As Date, Optional onlySplitschichten As Boolean = False, Optional art As String = "") As Integer

        Dim sqlWhere As String = ""
        If onlySplitschichten Then
            sqlWhere &= "AND dstetr_splitschicht = 1"
        End If

        If art <> "" Then
            sqlWhere &= "And dstetr_art = '" & art & "'"
        End If
        Try
            Dim stunden As Integer
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(" SELECT COUNT(*) FROM tblDienstplanEintraege WHERE dstetr_dstmaId=" & maId & " AND dstetr_datum BETWEEN '" & datum_von.ToShortDateString & "' AND '" & datum_bis.ToShortDateString & "' " & sqlWhere, conn)
                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    If dr.HasRows Then
                        dr.Read()
                        Return CInt(dr.Item(0))
                    End If
                    dr.Close()
                End Using
            End Using
            Return stunden
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)

            End If
        End Try
        Return 0
    End Function


    Public Function getCountDays(maId As Integer, datum_von As Date, datum_bis As Date) As Integer
        Try
            Dim stunden As Integer
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(" SELECT COUNT (dstetr_dstmaId) FROM tblDienstplanEintraege WHERE dstetr_dstmaId=" & maId & " AND dstetr_datum BETWEEN '" & datum_von.ToShortDateString & "' AND '" & datum_bis.ToShortDateString & "' AND (dstetr_art LIKE 'ZOLL%' OR dstetr_art LIKE 'QS') ", conn)
                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    If dr.HasRows Then
                        dr.Read()
                        Return CInt(dr.Item(0))
                    End If
                    dr.Close()
                End Using
            End Using
            Return stunden
        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)

            End If
        End Try
        Return 0
    End Function

    Public Function getSchichtOnDate(maId As Integer, datum As Date) As String
        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(" SELECT dstetr_art FROM tblDienstplanEintraege WHERE dstetr_dstmaId=" & maId & " AND dstetr_datum = '" & datum.ToShortDateString & "' ", conn)
                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("dstetr_art").ToString
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
            End If
        End Try

        Return ""
    End Function

    Public Function getLatestDienstplanEintraegNr() As Integer

        Try
            Using conn As SqlConnection = cSqlDb.GetNewOpenConnection()
                Using cmd As New SqlCommand(" select MAX([dstetr_id]) as MaxId from tblDienstplanEintraege ", conn)
                    Dim dr = cmd.ExecuteReader()
                    Dim cPF As New cProgramFunctions
                    If dr.HasRows Then
                        dr.Read()
                        Return dr.Item("MaxId")
                    End If
                    dr.Close()
                End Using
            End Using

        Catch ex As Exception
            Dim antwort As MsgBoxResult = MsgBox(ex.Message, CType(MsgBoxStyle.RetryCancel + MsgBoxStyle.Exclamation, MsgBoxStyle), "Problem in Function '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'")
            If antwort <> MsgBoxResult.Retry Then
                MsgBox(System.Reflection.MethodInfo.GetCurrentMethod.Name & ": Programm wird aufgrund eines kritischen Problems beendet.", vbInformation)
            End If
        End Try

        Return -1

    End Function

End Class


