Public Class frmGhostMode
    Private Sub frmGhostMode_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim oSQL = "SELECT [SQL_COMMAND] FROM TBDD_SQL_COMMANDS WITH (NOLOCK) where TITLE = 'GHOST_SELECT'"
        Dim DT_USER = DatabaseFallback.GetDatatableECM(oSQL)
        ' 
        If IsNothing(DT_USER) Then
            LOGGER.Info("GHOST Select 1 was nothing, now trying 2nd..")
            oSQL = "SELECT CONFIG_VALUE FROM TBIDB_BASE WITH (NOLOCK) WHERE CONFIG_NAME = 'GHOST_SELECT'"
            If Not IsNothing(oSQL) Then
                oSQL = DatabaseFallback.GetScalarValueIDB(oSQL)
                DT_USER = DatabaseFallback.GetDatatableIDB(oSQL)
            Else
                MsgBox("No SQL for Ghostmode via IDB. Check Your configuration.", MsgBoxStyle.Exclamation)
                Exit Sub
            End If
        End If

        Try
            If Not IsNothing(DT_USER) Then
                oSQL = DT_USER.Rows(0).Item(0)
                DT_USER = DatabaseFallback.GetDatatableECM(oSQL)
                GridControl1.DataSource = DT_USER
                LOGGER.Info($"Received [{DT_USER.Rows.Count}] Ghost-Users to select!")
            End If


        Catch ex As Exception
            MsgBox("Error Load_Users:" & vbNewLine & ex.Message)
        End Try
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick

        Set_GhostUser()
    End Sub

    Private Sub GridView1_DoubleClick(sender As Object, e As EventArgs) Handles GridView1.DoubleClick
        Set_GhostUser()
    End Sub

    Sub Set_GhostUser()
        Dim oFocusedUserName
        Try
            oFocusedUserName = GridView1.GetFocusedRowCellValue(GridView1.Columns("UserName"))
            If IsNothing(oFocusedUserName) Then
                oFocusedUserName = GridView1.GetFocusedRowCellValue(GridView1.Columns("USERNAME"))
            End If
            If IsNothing(oFocusedUserName) Then
                oFocusedUserName = GridView1.GetFocusedRowCellValue(GridView1.Columns("USER_NAME"))
            End If
            If IsNothing(oFocusedUserName) Then
                oFocusedUserName = GridView1.GetFocusedRowCellValue(GridView1.Columns("USER"))
            End If
        Catch ex As Exception
            oFocusedUserName = GridView1.GetFocusedRowCellValue(GridView1.Columns("UserName"))
            If IsNothing(oFocusedUserName) Then
                MsgBox(ex.Message, MsgBoxStyle.Critical)
                Exit Sub
            End If
        End Try
        If oFocusedUserName <> String.Empty Then
            Dim result As MsgBoxResult = MsgBox("Do You really want to activate the Ghost-Mode?", MsgBoxStyle.YesNo, "")
            'wenn Speichern ja
            If result = MsgBoxResult.Yes Then
                USER_GHOST_MODE_USRNAME = oFocusedUserName
                USER_GHOST_MODE_ACTIVE = True
                Me.Close()
            End If
        Else
            MsgBox("Please choose an user for ghostmode!", MsgBoxStyle.Information)
        End If
    End Sub
End Class