Imports DevExpress.XtraGrid.Views.Tile
Imports DevExpress.XtraEditors
Imports System.Text.RegularExpressions

Public Class frmConstructorDesigner
    Dim SELECTED_ENTITY_ID As Integer
    Dim SELECTED_CONSTRUCTOR_DETAIL_ID As Integer
    Dim formloaded As Boolean = False
    Dim AtPlaceholderPattern As String = "@[A-Za-z_]+"
    Private Sub TBPMO_FORM_CONSTRUCTORBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_FORM_CONSTRUCTORBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.TBPMO_FORM_CONSTRUCTORBindingSource.EndEdit()
            If DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = Environment.UserName
                TBPMO_FORM_CONSTRUCTORTableAdapter.Update(DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR)
                lblSave.Visible = True
            Else
                lblSave.Visible = False
            End If

        Catch ex As Exception
            MsgBox("Error in Saving FormConstructor:" & vbNewLine & ex.Message)
        End Try


    End Sub

    Private Sub frmConstructorDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_ECMAdmin1.TBPMO_FORM_CONSTRUCTOR". Sie können sie bei Bedarf verschieben oder entfernen.
        Try
            XtraTabControl2.SelectedTabPageIndex = 0
            formloaded = False
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Connection.ConnectionString = MyConnectionString
            Load_Head()
            Dim sql1 As String = "SELECT T.GUID, T1.FORM_TITLE FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1 ORDER BY T1.FORM_TITLE"
            Dim DTFORMS As DataTable = ClassDatabase.Return_Datatable(sql1, "")
            'FORM_IDComboBox.DataSource = DTFORMS
            'FORM_IDComboBox.DisplayMember = DTFORMS.Columns(1).ColumnName
            'FORM_IDComboBox.ValueMember = DTFORMS.Columns(0).ColumnName

            Dim sql2 As String = "SELECT T.GUID, T1.FORM_TITLE FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.FORM_TYPE_ID IN (1,2) AND T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1 ORDER BY T1.FORM_TITLE"
            Dim DTParent As DataTable = ClassDatabase.Return_Datatable(sql2, "")
            'PARENT_IDComboBox.DataSource = DTParent
            'PARENT_IDComboBox.DisplayMember = DTParent.Columns(1).ColumnName
            'PARENT_IDComboBox.ValueMember = DTParent.Columns(0).ColumnName

        Catch ex As Exception
            MsgBox("Error Loading Form:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Sub Load_Head()
        Try
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR, USER_LANGUAGE, CURRENT_SCREEN_ID)
        Catch ex As Exception
            MsgBox("Error Loading Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub Load_Detail(ID As Integer)
        Try
            Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL, ID)
            SELECTED_ENTITY_ID = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & ID, True)
        Catch ex As System.Exception
            MsgBox("Error Loading Details:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Exit Sub
        End Try
        Select Case XtraTabControl2.SelectedTabPageIndex
            Case 0
                Me.btnSetHeadline.Visible = False
                Load_QuickDisplay()
            Case 2
                RecordSearch_Config()
        End Select

    End Sub
    Public Sub Load_QuickDisplay()
        Try
            If SELECTED_CONSTRUCTOR_DETAIL_ID = 0 Then Exit Sub
            Dim formId = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & SELECTED_CONSTRUCTOR_DETAIL_ID, True)
            Dim sql = "SELECT T.GUID, T.COL_NAME AS 'COL_NAME' FROM TBPMO_CONTROL T, TBPMO_CONTROL_SCREEN T1, TBPMO_CONTROL_TYPE T2 WHERE T.SHOW_COLUMN = 1 AND T.[COL_NAME] <> '' AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = " & formId & " AND LEN(ISNULL(T.COL_NAME,'')) > 0 and t.CONTROL_TYPE_ID not in (1,5,12) " & _
                                "AND T2.GUID not in (1,5,7,8,9) AND T.GUID NOT IN (SELECT CONTROL_ID FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & ") ORDER BY T1.TAB_INDEX"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, "LOAD QUICKDISPLAY")
            listBoxControls.DataSource = DT
            listBoxControls.DisplayMember = DT.Columns(1).ColumnName
            listBoxControls.ValueMember = DT.Columns(0).ColumnName

            Dim DT2 = ClassDatabase.Return_Datatable("SELECT T.GUID, T1.COL_NAME FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY T, TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID <> 6 AND T.CONTROL_ID = T1.GUID AND T.CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " ORDER BY T.SEQUENCE", "LOAD QUICKDISPLAY")
            ListBoxConfiguredDisplay.DataSource = DT2
            ListBoxConfiguredDisplay.DisplayMember = DT2.Columns(1).ColumnName
            ListBoxConfiguredDisplay.ValueMember = DT2.Columns(0).ColumnName

            Dim DT3 As DataTable = ClassDatabase.Return_Datatable("SELECT T.GUID, T1.COL_NAME FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY T, TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID = 6 AND T.CONTROL_ID = T1.GUID AND T.CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID, "LOAD QUICKDISPLAY PICTURE")
            If DT3.Rows.Count = 1 Then
                Me.txtQuickViewPicture.Text = DT3.Rows(0).Item(1)
                Me.txtQuickViewPicture.Tag = DT3.Rows(0).Item(0)
            Else
                Me.txtQuickViewPicture.Text = "NOT CONFIGURED"
                Me.txtQuickViewPicture.Tag = Nothing
            End If
            Try
                Me.TBTEMP_QUICKDISPLAYTableAdapter.Connection.ConnectionString = MyConnectionString
                Me.TBTEMP_QUICKDISPLAYTableAdapter.Fill(Me.DD_DMSDataSet.TBTEMP_QUICKDISPLAY, SELECTED_CONSTRUCTOR_DETAIL_ID)
            Catch ex As System.Exception
                MsgBox("Error in Load_Preview_QuickDisplay:" & vbNewLine & ex.Message)
            End Try
        Catch ex As Exception
            MsgBox("Error Load_QuickDisplay:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Public Sub Load_ConstructorUser()
        Try
            Dim Sql = String.Format("SELECT GUID,USERNAME FROM TBDD_USER WHERE MODULE_RECORD_ORG = 1 AND GUID NOT IN (SELECT USER_ID FROM TBPMO_CONSTRUCTOR_USER WHERE CONSTRUCT_ID = {0}) ORDER By USERNAME", CONSTRUCTOR_IDTextBox.Text)
            Dim DT_USER = ClassDatabase.Return_Datatable(Sql)
            Sql = String.Format("SELECT T1.GUID, T.USERNAME + ' - ' + CASE T1.HOME_MENUE WHEN 1 THEN 'HOME' ELSE 'STANDARD' END AS USERNAME FROM TBDD_USER T, TBPMO_CONSTRUCTOR_USER T1 WHERE T1.USER_ID = T.GUID AND T1.CONSTRUCT_ID = {0} ORDER BY T.USERNAME", CONSTRUCTOR_IDTextBox.Text)
            Dim DT_USER_CONSTR As DataTable = ClassDatabase.Return_Datatable(Sql, "LOAD USERS FOR CONSTRUCT")
            chksbxUserConstructor.Items.Clear()
            For Each row As DataRow In DT_USER.Rows
                chksbxUserConstructor.Items.Add(New MyListBoxItem() With {.Text = row.Item(1), .ExtraData = row.Item(0)})
            Next

            ListBoxUsersFORConstructor.DataSource = DT_USER_CONSTR
            ListBoxUsersFORConstructor.DisplayMember = DT_USER_CONSTR.Columns(1).ColumnName
            ListBoxUsersFORConstructor.ValueMember = DT_USER_CONSTR.Columns(0).ColumnName

        Catch ex As Exception
            MsgBox("UnexpectedError Load_ConstructorUser:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Public Sub RecordSearch_Config()
        Try
            'Zurücksetzen
            txtWindreamsuche.Text = ""
            cmbSearchPattern1.SelectedIndex = -1
            cmbSearchPattern2.SelectedIndex = -1
            cmbSearchPattern3.SelectedIndex = -1
            cmbSearchPattern4.SelectedIndex = -1
            cmbSearchPattern5.SelectedIndex = -1
            Dim res = ClassDatabase.Execute_Scalar("SELECT DOCUMENT_VIEW FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)

            If res = True Then
                chkWDSearch_Active.Checked = True

                Dim DT As DataTable = ClassDatabase.Return_Datatable("SELECT WINDREAM_SEARCH,SEARCH_PATTERN1,SEARCH_PATTERN2,SEARCH_PATTERN3,SEARCH_PATTERN4,SEARCH_PATTERN5 FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)
                If DT.Rows.Count = 1 Then
                    If Not IsDBNull(DT.Rows(0).Item(0)) Then
                        txtWindreamsuche.Text = ClassHelper.CheckDBNull(DT.Rows(0).Item(0), "String")
                        'Form-ID (Entity)
                        'Record -ID
                        Dim result
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(1), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern1.SelectedIndex = 1
                            Else
                                cmbSearchPattern1.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(2), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern2.SelectedIndex = 1
                            Else
                                cmbSearchPattern2.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(3), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern3.SelectedIndex = 1
                            Else
                                cmbSearchPattern3.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(4), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern4.SelectedIndex = 1
                            Else
                                cmbSearchPattern4.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(5), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern5.SelectedIndex = 1
                            Else
                                cmbSearchPattern5.SelectedIndex = 0
                            End If
                        End If
                        'cmbSearchPattern1.SelectedIndex = cmbSearchPattern1.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(1), "String"))
                        'cmbSearchPattern2.SelectedIndex = cmbSearchPattern2.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(2), "String"))
                        'cmbSearchPattern3.SelectedIndex = cmbSearchPattern3.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(3), "String"))
                        'cmbSearchPattern4.SelectedIndex = cmbSearchPattern4.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(4), "String"))
                        'cmbSearchPattern5.SelectedIndex = cmbSearchPattern5.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(5), "String"))
                    End If
                End If
            Else
                chkWDSearch_Active.Checked = False
            End If
        Catch ex As Exception
            MsgBox("Error in RecordSearch_Config:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub frmConstructorDesigner_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        formloaded = True
        Load_TreeViewEbene()
        Load_ConstructorUser()
        LANGUAGETextBox.Text = USER_LANGUAGE
    End Sub
    Sub Load_TreeViewEbene()
        Try
            Dim CONSTR_ID As Integer = CONSTRUCTOR_IDTextBox.Text

            Dim sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                            "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID IN (1,4) AND PARENT_ID = 0 AND CONSTRUCT_ID = {2} AND SCREEN_ID = {3} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, CONSTR_ID, CURRENT_SCREEN_ID)

            Dim DT As DataTable = ClassDatabase.Return_Datatable(sel_sql)
            If DT.Rows.Count >= 1 Then
                TreeViewDetails.Nodes.Clear()
                For Each rowEbene0 As DataRow In DT.Rows
                    Dim Node As TreeNode
                    Node = TreeViewDetails.Nodes.Add("E1: " & rowEbene0.Item("CAPTION").ToString)
                    Node.Tag = rowEbene0.Item("GUID")
                    'Jetzt die Ebene1Knoten einhängen
                    sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                            "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID in (2,5) AND PARENT_ID = {2} AND CONSTRUCT_ID = {3} AND SCREEN_ID = {4} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, rowEbene0.Item("FORM_ID"), CONSTR_ID, CURRENT_SCREEN_ID)

                    Dim dtEbene1 As DataTable = ClassDatabase.Return_Datatable(sel_sql)
                    If dtEbene1.Rows.Count > 0 Then
                        For Each rowEbene1 As DataRow In dtEbene1.Rows
                            Dim Node1 As TreeNode
                            Node1 = Node.Nodes.Add("E2: " & rowEbene1.Item("CAPTION").ToString)
                            Node1.Tag = rowEbene1.Item("GUID")
                            'Jetzt die Ebene2 Knoten einhängen
                            sel_sql = String.Format("SELECT *, [dbo].[FNPMO_GETOBJECTCAPTION]('{0}','FORMVIEW_TITLE' + CONVERT(VARCHAR(5), FORM_VIEW_ID), {1}) AS 'CAPTION' " & _
                           "from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID in (3,5) AND PARENT_ID = {2} AND CONSTRUCT_ID = {3} AND SCREEN_ID = {4} ORDER BY SEQUENCE", USER_LANGUAGE, CURRENT_SCREEN_ID, rowEbene1.Item("FORM_ID"), CONSTR_ID, CURRENT_SCREEN_ID)

                            Dim dtEbene2 As DataTable = ClassDatabase.Return_Datatable(sel_sql)
                            If dtEbene2.Rows.Count > 0 Then
                                For Each rowEbene2 As DataRow In dtEbene2.Rows
                                    Dim Node2 As TreeNode
                                    Node2 = Node1.Nodes.Add("E3: " & rowEbene2.Item("CAPTION").ToString)
                                    Node2.Tag = rowEbene2.Item("GUID")
                                Next
                            End If

                        Next

                    End If
                Next
                TreeViewDetails.ExpandAll()
                TreeViewDetails.Nodes(0).EnsureVisible()
            Else
                MsgBox("Achtung Konfigurationsfehler: Mehrere oder keine Level 0 Entität gefunden!" & "Korrigieren Sie bitte Ihre Eingaben", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MsgBox("Error in Load_TreeView:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub TBPMO_FORM_CONSTRUCTORBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBPMO_FORM_CONSTRUCTORBindingSource.CurrentChanged
        lblSave.Visible = False
        If formloaded = True Then
            Try
                If CONSTRUCTOR_IDTextBox.Text <> "" Then
                    LANGUAGETextBox.Text = USER_LANGUAGE
                    Load_TreeViewEbene()
                    Load_ConstructorUser()
                End If
            Catch ex As Exception
                MsgBox("Error Loading Construct-Data:" & vbNewLine & ex.Message)
            End Try
        End If
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeViewDetails.AfterSelect
        Try
            If Not TreeViewDetails.SelectedNode Is Nothing Then
                Dim construct_id As Integer = Integer.Parse(TreeViewDetails.SelectedNode.Tag)
                SELECTED_CONSTRUCTOR_DETAIL_ID = construct_id
                Me.tslblSaveDetail.Visible = False
                Load_Detail(construct_id)
                lblSave1.Visible = False
                Select Case XtraTabControl2.SelectedTabPageIndex
                    Case 0
                        Load_QuickDisplay()
                    Case 2
                        RecordSearch_Config()
                    Case 3
                        Load_UserSQL()
                    Case 4
                        Load_Client_Entity()
                        Load_ClientsforEntity()
                End Select

            End If
        Catch ex As Exception
            MsgBox("Error in AfterSelect:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub LEVEL1_SELECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LEVEL1_SELECTCheckBox.CheckedChanged
        Try
            If LEVEL1_SELECTCheckBox.Checked Then
                SQL_SELECT_EBENE1TextBox.Enabled = True
            Else
                SQL_SELECT_EBENE1TextBox.Enabled = False
            End If
            Dim view As String
            If SQL_SELECT_EBENE1TextBox.Text <> "" Then
                view = SQL_SELECT_EBENE1TextBox.Text.ToUpper.Replace("SELECT * FROM ", "")
                view = view.Trim
                Dim Sql = "SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('" & view & "')"
                If LogErrorsOnly = False Then ClassLogger.Add(">> " & Sql, False)
                Dim DT As DataTable = ClassDatabase.Return_Datatable(Sql)
                If DT IsNot Nothing Then
                    COLUMN_NAME1ComboBox.Items.Clear()
                    For Each row As DataRow In DT.Rows
                        COLUMN_NAME1ComboBox.Items.Add(row.Item(0))
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in LEVEL1_SELECTCheckBox:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub tsbtnSave_Click(sender As Object, e As EventArgs) Handles tsbtnSave.Click
        If XtraTabControl2.SelectedTabPageIndex = 1 Then
            Try
                Me.TBPMO_FORM_CONSTRUCTOR_DETAILBindingSource.EndEdit()
                If DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL.GetChanges Is Nothing = False Then
                    Me.CHANGED_WHOTextBox1.Text = Environment.UserName
                    Me.TBPMO_FORM_CONSTRUCTOR_DETAILBindingSource.EndEdit()
                    Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Update(DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL)
                    tslblSaveDetail.Text = "Constructor-Details erfolgreich gespeichert - " & Now
                    Me.tslblSaveDetail.Visible = True
                Else
                    Me.tslblSaveDetail.Visible = False
                End If

            Catch ex As Exception
                MsgBox("Error in Save Constructor-Details:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try
        End If

        If XtraTabControl2.SelectedTabPageIndex = 2 Then
            Try
                Try
                    Dim SP1, SP2, SP3, SP4, SP5
                    If cmbSearchPattern1.SelectedIndex <> -1 Then
                        SP1 = "@" & cmbSearchPattern1.Text
                    Else
                        SP1 = ""
                    End If
                    If cmbSearchPattern2.SelectedIndex <> -1 Then
                        SP2 = "@" & cmbSearchPattern2.Text
                    Else
                        SP2 = ""
                    End If
                    If cmbSearchPattern3.SelectedIndex <> -1 Then
                        SP3 = "@" & cmbSearchPattern3.Text
                    Else
                        SP3 = ""
                    End If
                    If cmbSearchPattern4.SelectedIndex <> -1 Then
                        SP4 = "@" & cmbSearchPattern4.Text
                    Else
                        SP4 = ""
                    End If
                    If cmbSearchPattern5.SelectedIndex <> -1 Then
                        SP5 = "@" & cmbSearchPattern5.Text
                    Else
                        SP5 = ""
                    End If
                    Dim sql = "UPDATE TBPMO_FORM_VIEW SET WINDREAM_SEARCH = '" & txtWindreamsuche.Text & "',SEARCH_PATTERN1 = '" & SP1 & "',SEARCH_PATTERN2= '" & SP2 & "',SEARCH_PATTERN3= '" & SP3 & _
                             "',SEARCH_PATTERN4 = '" & SP4 & "',SEARCH_PATTERN5 = '" & SP5 & "', CHANGED_WHO = '" & Environment.UserName & "' where FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID
                    If ClassDatabase.Execute_non_Query(sql) = True Then
                        lblSave1.Visible = True
                        lblSave1.Text = "Erfolgreich gespeichert - " & Now
                    End If
                Catch ex As Exception
                    MsgBox("Error in Save DocView-properties 2:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                    lblSave1.Visible = False
                End Try
            Catch ex As Exception
                MsgBox("Error in Save DocView-properties 1:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                lblSave1.Visible = False
            End Try
        End If

        If XtraTabControl2.SelectedTabPageIndex = 3 And TabControl1.SelectedIndex = 0 Then
            Try
                Me.TBPMO_CONSTRUCTOR_USER_SQLBindingSource.EndEdit()
                If DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL.GetChanges Is Nothing = False Then
                    Me.CHANGED_WHOTextBox2.Text = Environment.UserName
                    Me.TBPMO_CONSTRUCTOR_USER_SQLBindingSource.EndEdit()
                    Me.TBPMO_CONSTRUCTOR_USER_SQLTableAdapter.Update(DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL)
                    lblsaveSQLUser.Visible = True
                Else
                    lblsaveSQLUser.Visible = False
                End If
            Catch ex As Exception
                MsgBox("Error in Save User SQL:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End Try

        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not Me.txtWindreamsuche.Text = "" Then
            Me.OFDWindreamsuche.FileName = Me.WINDREAM_SEARCHTextBox.Text
        End If

        If Me.OFDWindreamsuche.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.WINDREAM_SEARCHTextBox.Text = Me.OFDWindreamsuche.FileName
        End If
    End Sub

    Private Sub btnWindreamsuche_Click(sender As Object, e As EventArgs) Handles btnWindreamsuche.Click
        If Not Me.txtWindreamsuche.Text = "" Then
            Me.OFDWindreamsuche.FileName = Me.txtWindreamsuche.Text
        End If

        If Me.OFDWindreamsuche.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtWindreamsuche.Text = Me.OFDWindreamsuche.FileName
        End If
    End Sub

    Private Sub chkWDSearch_Active_CheckedChanged(sender As Object, e As EventArgs) Handles chkWDSearch_Active.CheckedChanged
        Try
            Dim check = ClassDatabase.Execute_Scalar("SELECT DOCUMENT_VIEW FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)

            If chkWDSearch_Active.Checked Then
                grpbxWD_RecordSearch.Enabled = True
                If check = False Then
                    ClassDatabase.Execute_Scalar("UPDATE TBPMO_FORM_VIEW SET DOCUMENT_VIEW = 1 WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID, True)
                End If
            Else
                grpbxWD_RecordSearch.Enabled = False
                If check = True Then
                    ClassDatabase.Execute_Scalar("UPDATE TBPMO_FORM_VIEW SET DOCUMENT_VIEW = 1 WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID, True)
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Checked Change WD-Search:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub XtraTabControl2_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl2.SelectedPageChanged
        Select Case XtraTabControl2.SelectedTabPageIndex
            Case 0
                Load_QuickDisplay()
            Case 2
                RecordSearch_Config()
            Case 3
                Load_UserSQL()
            Case 4
                Load_Client_Entity()
                Load_ClientsforEntity()
        End Select
    End Sub
    Public Class MyListBoxItem
        Private _text As String
        Private _extraData As String

        Public Property Text As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property

        Public Property ExtraData As String
            Get
                Return _extraData
            End Get
            Set(ByVal value As String)
                _extraData = value
            End Set
        End Property

        Public Overrides Function ToString() As String
            Return Text
        End Function

    End Class
    Public Sub Load_Client_Entity()
        Try
            Dim sql = String.Format("SELECT GUID, CLIENT_NAME FROM TBDD_CLIENT WHERE GUID IN (SELECT CLIENT_ID FROM TBDD_CLIENT_USER WHERE USER_ID = {0})", USER_GUID)
            Dim DT1 As DataTable = ClassDatabase.Return_Datatable(sql)
            chklbxClient.Items.Clear()
            For Each row As DataRow In DT1.Rows
                chklbxClient.Items.Add(New MyListBoxItem() With {.Text = row.Item(1), .ExtraData = row.Item(0)})
            Next
           
        Catch ex As Exception
            MsgBox("UnexpectedError Load_Client_Entity:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Sub Load_ClientsforEntity()
        Try
            Dim ENTITY_ID = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM VWPMO_CONSTRUCTOR_FORMS WHERE GUID = " & SELECTED_CONSTRUCTOR_DETAIL_ID)
            Dim sql = String.Format("SELECT T.GUID, T1.CLIENT_NAME FROM TBPMO_CLIENT_ENTITY T, TBDD_CLIENT T1 WHERE T.CLIENT_ID = T1.GUID " & _
                        "AND T1.GUID IN (SELECT CLIENT_ID FROM TBDD_CLIENT_USER WHERE USER_ID = {0}) AND T.ENTITY_ID = {1}", USER_GUID, ENTITY_ID)


            Dim DT As DataTable = ClassDatabase.Return_Datatable(Sql)
            chklbxEntitiesforClient.Items.Clear()
            For Each row1 As DataRow In DT.Rows
                chklbxEntitiesforClient.Items.Add(New MyListBoxItem() With {.Text = row1.Item(1), .ExtraData = row1.Item(0)})
            Next
        Catch ex As Exception
            MsgBox("UnexpectedError Load_ClientsforEntity:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Public Sub Load_UserSQL()
        Try
            lbxUserSQL.Items.Clear()

            Dim sql = String.Format("SELECT DISTINCT T.GUID, T.USERNAME FROM TBDD_USER T, TBDD_CLIENT_USER T1 WHERE T.MODULE_RECORD_ORG = 1 AND T.GUID = T1.USER_ID AND T1.CLIENT_ID IN ({0}) ORDER BY T.USERNAME", USER_CLIENTS_COMMA_SEPERATED)
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql)
            lbxUserSQL.DataSource = DT
            lbxUserSQL.DisplayMember = DT.Columns(1).ColumnName
            lbxUserSQL.ValueMember = DT.Columns(0).ColumnName
        Catch ex As Exception
            MsgBox("UnexpectedError Load_UserSQL:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Private Sub btnAddControl_Click(sender As Object, e As EventArgs) Handles btnAddControl.Click
        Try
            Dim i = listBoxControls.SelectedValue
            If Not IsNothing(i) Then
                Dim insert = "INSERT INTO TBPMO_FORM_CONSTR_QUICK_DISPLAY (CONSTRUCT_ID, CONTROL_ID,ADDED_WHO) VALUES (" & SELECTED_CONSTRUCTOR_DETAIL_ID & ", " & i & ",'" & Environment.UserName & "')"
                If ClassDatabase.Execute_non_Query(insert, True) Then
                    Load_QuickDisplay()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Adding Control to Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btndeleteControl_Click(sender As Object, e As EventArgs) Handles btndeleteControl.Click
        Try
            Dim i = ListBoxConfiguredDisplay.SelectedValue
            If Not IsNothing(i) Then
                Dim delete = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY  WHERE GUID = " & i
                If ClassDatabase.Execute_non_Query(delete, True) Then
                    Load_QuickDisplay()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Deleting Control from Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub ListBoxConfiguredDisplay_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxConfiguredDisplay.SelectedIndexChanged
        lblChangeHeadline.Visible = False
        If ListBoxConfiguredDisplay.SelectedIndex <> -1 Then
            Try
                Dim txt As String = ListBoxConfiguredDisplay.Text
                Me.btnSetHeadline.Text = "Set '" & txt & "'" & vbNewLine & "as Headline"
                Me.btnSetHeadline.Visible = True
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub btnSetHeadline_Click(sender As Object, e As EventArgs) Handles btnSetHeadline.Click
        Dim upd = "UPDATE TBPMO_FORM_CONSTR_QUICK_DISPLAY SET HEADLINE = 1, CHANGED_WHO = '" & Environment.UserName & "' WHERE GUID = " & ListBoxConfiguredDisplay.SelectedValue
        If ClassDatabase.Execute_non_Query(upd, True) Then
            lblChangeHeadline.Visible = True
        End If
    End Sub

    Private Sub btnSetQuickViewPicture_Click(sender As Object, e As EventArgs) Handles btnSetQuickViewPicture.Click
        Dim DEL As String = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " AND CONTROL_ID in (SELECT GUID FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 6)"
        If ClassDatabase.Execute_non_Query(DEL, True) Then
            Try
                Dim i = listBoxControls.SelectedValue
                If Not IsNothing(i) Then
                    Dim insert = "INSERT INTO TBPMO_FORM_CONSTR_QUICK_DISPLAY (CONSTRUCT_ID, CONTROL_ID,PICTURE,ADDED_WHO) VALUES (" & SELECTED_CONSTRUCTOR_DETAIL_ID & ", " & i & ",1,'" & Environment.UserName & "')"
                    If ClassDatabase.Execute_non_Query(insert, True) Then
                        Load_QuickDisplay()
                        lblQuickViewAdded.Visible = True
                    Else
                        lblQuickViewAdded.Visible = False
                    End If
                End If
            Catch ex As Exception
                MsgBox("Error in Adding Control to Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                lblQuickViewAdded.Visible = False
            End Try
        End If
    End Sub

    Private Sub listBoxControls_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBoxControls.SelectedIndexChanged
        If listBoxControls.SelectedIndex <> -1 Then
            Try
                Dim i = listBoxControls.SelectedValue
                If i.ToString.Contains("DataRowV") Then Exit Sub
                If Not IsNothing(i) Then
                    Dim count = ClassDatabase.Execute_Scalar("SELECT COUNT(T1.GUID) FROM TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID = 6 and T1.GUID = " & i, True)
                    If count = 1 Then
                        Me.btnSetQuickViewPicture.Visible = True
                    Else
                        Me.btnSetQuickViewPicture.Visible = False
                    End If
                End If
                lblQuickViewAdded.Visible = False
            Catch ex As Exception
                MsgBox("Error in CHECK CONTROL IS PICTURE: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                Me.btnSetQuickViewPicture.Visible = False
            End Try
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim DEL As String = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " AND CONTROL_ID in (SELECT GUID FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 6)"
        If ClassDatabase.Execute_non_Query(DEL, True) Then
            Load_QuickDisplay()
        End If
    End Sub
    Private Function CreateTable(ByVal rowCount As Integer) As DataTable
        Dim tbl As New DataTable()
        tbl.Columns.Add("Name", GetType(String))
        tbl.Columns.Add("ID", GetType(Integer))
        tbl.Columns.Add("Image", GetType(Image))
        tbl.Columns.Add("Date", GetType(Date))
        For i As Integer = 0 To rowCount - 1
            tbl.Rows.Add(New Object() {String.Format("Name{0}", i), i, ImageCollection1.Images(i Mod 4), Date.Now.AddDays(i - 3)})
        Next i
        Return tbl
    End Function

    Private Sub LOAD_DIRECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LOAD_DIRECTCheckBox.CheckedChanged

    End Sub

    Private Sub btnAddUser2ConstructorREL_Click(sender As Object, e As EventArgs) Handles btnAddUser2ConstructorREL.Click
        Try
            For Each obj As Object In chklbxClient.CheckedItems
                Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
                Dim home As Integer = CInt(CheckBoxHome.Checked)
                Dim insert = "INSERT INTO TBPMO_CONSTRUCTOR_USER (CONSTRUCT_ID, USER_ID,ADDED_WHO,HOME_MENUE) VALUES (" & CONSTRUCTOR_IDTextBox.Text & ", " & item.ExtraData & ",'" & Environment.UserName & "'," & home & ")"
                If ClassDatabase.Execute_non_Query(insert, True) Then
                    Load_ConstructorUser()
                End If
            Next
        Catch ex As Exception
            MsgBox("Error while adding user to Constructor-Relation: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btndeleteUserConstructorRel_Click(sender As Object, e As EventArgs) Handles btndeleteUserConstructorRel.Click
        Try
            Dim i = ListBoxUsersFORConstructor.SelectedValue
            If Not IsNothing(i) Then
                Dim delete = "DELETE FROM TBPMO_CONSTRUCTOR_USER  WHERE GUID = " & i
                If ClassDatabase.Execute_non_Query(delete, True) Then
                    Load_ConstructorUser()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error while deleting user to Constructor-Relation: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub FormDesignerToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FormDesignerToolStripMenuItem.Click
        Try
            Dim SelectedNode As TreeNode = TryCast(TreeViewDetails.SelectedNode, TreeNode)
            If SelectedNode IsNot Nothing Then
                Dim sql = String.Format("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = {0}", SelectedNode.Tag)
                Dim FormId As Integer = ClassDatabase.Execute_Scalar(sql)
                If FormId > 0 Then
                    CURRENT_FORM_ID = FormId
                    OpenFormLevelDesigner()
                End If
            End If
        Catch ex As Exception
            MsgBox("Unexpected Error in Open Designer: " & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnChangeHOMESTANDARD_Click(sender As Object, e As EventArgs) Handles btnChangeHOMESTANDARD.Click
        Try
            Dim i = ListBoxUsersFORConstructor.SelectedValue
            If Not IsNothing(i) Then
                Dim sql = "SELECT HOME_MENUE FROM TBPMO_CONSTRUCTOR_USER  WHERE GUID = " & i
                Dim Home = ClassDatabase.Execute_Scalar(sql, True)
                If Home = True Then
                    Home = 0
                Else
                    Home = 1
                End If
                sql = "UPDATE TBPMO_CONSTRUCTOR_USER SET HOME_MENUE = " & Home & " WHERE GUID = " & i
                If ClassDatabase.Execute_non_Query(sql, True) Then
                    Load_ConstructorUser()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error while deleting user to Constructor-Relation: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub txtSearchUser_TextChanged(sender As Object, e As EventArgs) Handles txtSearchUser.TextChanged
        Dim i As Integer = lbxUserSQL.FindString(txtSearchUser.Text)
        lbxUserSQL.SelectedIndex = i
        If txtSearchUser.Text = "" Then
            lbxUserSQL.SelectedIndex = -1
        End If
    End Sub

    Private Sub lbxUserSQL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbxUserSQL.SelectedIndexChanged
        Try
            Me.TBPMO_CONSTRUCTOR_USER_SQLTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_CONSTRUCTOR_USER_SQLTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL, SELECTED_CONSTRUCTOR_DETAIL_ID, lbxUserSQL.SelectedValue)
        Catch ex As System.Exception
            If Not ex.Message.ToLower.Contains("atarowview") Then
                MsgBox("Error while loading User-Restrictions:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            End If
        End Try
        lblsaveSQLUser.Visible = False
    End Sub

    Private Sub btnAddUserSQL_Click(sender As Object, e As EventArgs) Handles btnAddUserSQL.Click
        If GUIDTextBox.Text = "" Then
            TBPMO_CONSTRUCTOR_USER_SQLBindingSource.AddNew()
        End If
    End Sub

    Private Sub TBPMO_CONSTRUCTOR_USER_SQLBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_CONSTRUCTOR_USER_SQLBindingSource.AddingNew
        Try
            DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL.ADDED_WHOColumn.DefaultValue = Environment.UserName
            DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL.CONSTR_DET_IDColumn.DefaultValue = SELECTED_CONSTRUCTOR_DETAIL_ID
            DD_DMSDataSet.TBPMO_CONSTRUCTOR_USER_SQL.USER_IDColumn.DefaultValue = lbxUserSQL.SelectedValue
        Catch ex As Exception
            MsgBox("Error while Adding User-Restrictions:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub SQL_COMMANDTextBox_TextChanged(sender As Object, e As EventArgs) Handles SQL_COMMANDTextBox.TextChanged
        If SQL_COMMANDTextBox.Text.Trim().Count = 0 Then
            dgvPlaceholders.Enabled = False
        Else
            dgvPlaceholders.Enabled = True
        End If

        CheckForPlaceholders()
    End Sub

    Private Sub CheckForPlaceholders()
        Dim count As Integer = 0
        Dim text As String = Me.SQL_COMMANDTextBox.Text
        Dim atPlaceholderRegex = New Regex(AtPlaceholderPattern, RegexOptions.IgnoreCase)
        Dim matches As MatchCollection = atPlaceholderRegex.Matches(text)

        dgvPlaceholders.Rows.Clear()

        For Each match As Match In matches
            Dim placeholder = ""
            Select Case match.Value.ToUpper
                Case "@USER_ID"
                    placeholder = lbxUserSQL.SelectedValue
            End Select
            dgvPlaceholders.Rows.Add({match.Value, placeholder})
        Next
    End Sub

    Private Sub btnTestSQL_Click(sender As Object, e As EventArgs) Handles btnTestSQL.Click
        Try
            Dim ENTITY_ID = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & SELECTED_CONSTRUCTOR_DETAIL_ID)
            Dim EntSQL = "SELECT T.* FROM VWTEMP_PMO_FORM" & ENTITY_ID.ToString & " T"
            Dim query As String = EntSQL & " " & SQL_COMMANDTextBox.Text

            For Each row As DataGridViewRow In dgvPlaceholders.Rows
                Dim placeholder As String = row.Cells(0).Value
                Dim replacement As String = row.Cells(1).Value

                ' Wenn Ersetzung ausgefüllt wurde, Platzhalter damit ersetzen
                If Not String.IsNullOrEmpty(replacement) Then
                    query = query.Replace(placeholder, replacement)
                Else
                    MsgBox("Please add a test-value for the placeholder '" & placeholder & "'!", MsgBoxStyle.Exclamation, "Missing value:")
                    Exit Sub
                End If
            Next
            Dim DT As DataTable = ClassDatabase.Return_Datatable(query)

            If DT IsNot Nothing Then
                dgvResult.DataSource = DT
            End If
        Catch ex As Exception
            MsgBox("Error in Testing User-Restriction SQL:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try


    End Sub

    Private Sub btnsaddUser2Client_Click(sender As Object, e As EventArgs) Handles btnsaddUser2Client.Click
        Dim ENTITY_ID = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & SELECTED_CONSTRUCTOR_DETAIL_ID)
        For Each obj As Object In chklbxClient.CheckedItems
            Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
            Dim sql = "select count(*) from TBPMO_CLIENT_ENTITY Where CLIENT_ID = " & item.ExtraData & " AND ENTITY_ID = " & ENTITY_ID
            If ClassDatabase.Execute_Scalar(sql) = 0 Then
                sql = String.Format("INSERT INTO TBPMO_CLIENT_ENTITY (CLIENT_ID, ENTITY_ID, ADDED_WHO) VALUES ({0}, {1}, '{2}')", item.ExtraData, ENTITY_ID, Environment.UserName)
                If ClassDatabase.Execute_non_Query(sql) = False Then
                    MsgBox("Error in Adding Entity to Client!", MsgBoxStyle.Critical)
                End If
            End If
        Next
        Load_ClientsforEntity()
    End Sub

    Private Sub btndeleteEntityfromClient_Click(sender As Object, e As EventArgs) Handles btndeleteEntityfromClient.Click
        For Each obj As Object In chklbxEntitiesforClient.CheckedItems
            Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
            Dim sql
            sql = String.Format("DELETE FROM TBPMO_CLIENT_ENTITY  WHERE GUID = {0}", item.ExtraData)
            If ClassDatabase.Execute_non_Query(sql, True) = False Then
                MsgBox(String.Format("Error in deleting Entity from Client", item.Text), MsgBoxStyle.Critical)
            End If
        Next
        Load_ClientsforEntity()
    End Sub
End Class