2016-05-12 13:55:04 +02:00

507 lines
25 KiB
VB.net

Imports ERPConnect
Imports System.Data.SqlClient
Public Class frmSAP_FuBa
Private Sub TBPMO_SAP_FUBABindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_SAP_FUBABindingNavigatorSaveItem.Click
Try
Me.TBPMO_SAP_FUBABindingSource.EndEdit()
If DD_ECMAdmin.TBPMO_SAP_FUBA.GetChanges Is Nothing = False Then
Me.CHANGED_WHOTextBox.Text = Environment.UserName
Me.TBPMO_SAP_FUBABindingSource.EndEdit()
Me.TBPMO_SAP_FUBATableAdapter.Update(DD_ECMAdmin.TBPMO_SAP_FUBA)
lblSave.Visible = True
Else
lblSave.Visible = False
End If
Catch ex As Exception
MsgBox("Error in Save Fuba:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub frmSAP_FuBa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_ECMAdmin.TBPMO_SAP_FUBA". Sie können sie bei Bedarf verschieben oder entfernen.
Refresh_Fubas()
End Sub
Sub Refresh_Fubas()
Try
Me.TBPMO_SAP_FUBATableAdapter.Connection.ConnectionString = MyConnectionString
Me.TBPMO_SAP_FUBATableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_SAP_FUBA)
If MethodTextBox.Text <> "" Then
If MethodTextBox.Text = "TABLE" Then
rbTable.Checked = True
Else
rBFuba.Checked = True
End If
End If
Catch ex As Exception
MsgBox("Error in Load Fubas:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub btnERPConnect_Click(sender As Object, e As EventArgs) Handles btnERPConnect.Click
Dim con As New R3Connection
Try
Me.Cursor = Cursors.WaitCursor
If LogErrorsOnly = False Then ClassLogger.Add("", False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP Connect started:", False)
ERPConnect.LIC.SetLic("W86DWC992C")
con.Host = HostTextBox.Text
con.SystemNumber = SystemNumberTextBox.Text
con.UserName = UserNameTextBox.Text
con.Password = PasswordTextBox.Text
con.Client = ClientTextBox.Text
con.Language = LanguageTextBox.Text
If LogErrorsOnly = False Then ClassLogger.Add(">> HOST: " & HostTextBox.Text & " |SystemNumber: " & SystemNumberTextBox.Text & " |UserName: " & UserNameTextBox.Text & " |Password: " & PasswordTextBox.Text & " |Client: " & ClientTextBox.Text & " |Language: " & LanguageTextBox.Text, False)
con.Open(False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP-Connection erfolgreich aufgebaut!", False)
If LogErrorsOnly = False Then ClassLogger.Add("", False)
lblConnectionopen.Visible = True
Me.Cursor = Cursors.Default
Catch ex As Exception
MsgBox("Unexpected Error in SAP Connect: " & ex.Message, MsgBoxStyle.Critical)
lblConnectionopen.Visible = False
Me.Cursor = Cursors.Default
lblResult.Visible = False
GridControl2.DataSource = Nothing
lblSave.Visible = False
Me.Cursor = Cursors.Default
Exit Sub
End Try
End Sub
Private Sub TBPMO_SAP_FUBABindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_SAP_FUBABindingSource.AddingNew
DD_ECMAdmin.TBPMO_SAP_FUBA.ADDED_WHOColumn.DefaultValue = Environment.UserName
End Sub
Private Sub TBPMO_SAP_FUBABindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBPMO_SAP_FUBABindingSource.CurrentChanged
lblSave.Visible = False
GridControl2.DataSource = Nothing
lblConnectionopen.Visible = False
If MethodTextBox.Text <> "" Then
If MethodTextBox.Text = "TABLE" Then
rbTable.Checked = True
Else
rBFuba.Checked = True
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnInsertData.Click
If TempTableNameTextBox.Text <> "" Then
Me.Cursor = Cursors.WaitCursor
Dim con As New R3Connection
Try
If LogErrorsOnly = False Then ClassLogger.Add("", False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP Connect started:", False)
ERPConnect.LIC.SetLic("W86DWC992C")
con.Host = HostTextBox.Text
con.SystemNumber = SystemNumberTextBox.Text
con.UserName = UserNameTextBox.Text
con.Password = PasswordTextBox.Text
con.Client = ClientTextBox.Text
con.Language = LanguageTextBox.Text
If LogErrorsOnly = False Then ClassLogger.Add(">> HOST: " & HostTextBox.Text & " |SystemNumber: " & SystemNumberTextBox.Text & " |UserName: " & UserNameTextBox.Text & " |Password: " & PasswordTextBox.Text & " |Client: " & ClientTextBox.Text & " |Language: " & LanguageTextBox.Text, False)
con.Open(False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP-Connection erfolgreich aufgebaut!", False)
If LogErrorsOnly = False Then ClassLogger.Add("", False)
Catch ex As Exception
MsgBox("Unexpected Error in SAP Connect: " & ex.Message, MsgBoxStyle.Critical)
Me.Cursor = Cursors.Default
Exit Sub
End Try
Dim resulttable As DataTable
Try
Dim table As New ERPConnect.Utils.ReadTable(con)
'Dim cWhereClause = "SAP_OBJECT EQ '<ObjectType>' AND AR_DATE GE '<AR_DATE>'"
'Dim sSearchDate1 = "20160101"
'cWhereClause = Replace(cWhereClause, "<ObjectType>", Objekttyp.Text)
'cWhereClause = Replace(cWhereClause, "<AR_DATE>", sSearchDate1)
If WHERE_CLAUSETextBox.Text <> "" Then
table.AddCriteria(WHERE_CLAUSETextBox.Text)
End If
'ClassLogger.Add(">> cWhereClause: " & cWhereClause, False)
'table.AddField("MATNR")
'table.AddField("MAKTX")
' table.AddCriteria("SPRAS = 'DE'")
table.TableName = TableNameTextBox.Text
If txtLimitRows.Text <> "" Then
table.RowCount = CInt(txtLimitRows.Text)
End If
table.Run()
resulttable = table.Result
If LogErrorsOnly = False Then ClassLogger.Add(">> resulttable created!", False)
If LogErrorsOnly = False Then ClassLogger.Add("", False)
Catch ex As Exception
Me.Cursor = Cursors.Default
MsgBox("Unexpected Error in Get SAP Table Result: " & ex.Message, MsgBoxStyle.Critical)
Exit Sub
End Try
Dim i = 0
Dim tablename = "TPPMO_SAPFUBA_ " & TempTableNameTextBox.Text
Dim createtablesql = "CREATE TABLE TPPMO_SAPFUBA_ " & TempTableNameTextBox.Text & " ("
For Each col As System.Data.DataColumn In resulttable.Columns
If i = 0 Then
createtablesql += "[" & col.ColumnName & "] [varchar](100)"
Else
createtablesql += ", [" & col.ColumnName & "] [varchar](100)"
End If
i += 1
Next
createtablesql += ")"
Dim check = String.Format("SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='{0}'", tablename)
If ClassDatabase.Execute_Scalar(check) = 1 Then
ClassDatabase.Execute_non_Query("DROP TABLE " & tablename)
End If
'Die Tabelle anlegen
If LogErrorsOnly = False Then ClassLogger.Add(">> Create Table: " & createtablesql, False)
If ClassDatabase.Execute_non_Query(createtablesql) = False Then
Me.Cursor = Cursors.Default
MsgBox("Error while creating the temporary table. Please check the logfile!", MsgBoxStyle.Critical)
Exit Sub
End If
Try
Dim SQLconnect As New SqlClient.SqlConnection
SQLconnect.ConnectionString = MyConnectionString
SQLconnect.Open()
' Note that the column positions in the source DataTable
' match the column positions in the destination table,
' so there is no need to map columns.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(SQLconnect)
bulkCopy.DestinationTableName = tablename
Try
' Write from the source to the destination.
bulkCopy.WriteToServer(resulttable)
Catch ex As Exception
MsgBox("Unexpected Error in BulkCopy SQL: " & ex.Message, MsgBoxStyle.Critical)
End Try
End Using
SQLconnect.Close()
Me.Cursor = Cursors.Default
If LogErrorsOnly = False Then ClassLogger.Add(">> Create table from SAP FuBa was successfull " & resulttable.Rows.Count & " Rows were inserted to table '" & tablename & "'!", False)
MsgBox("Create table was successfull! " & vbNewLine & resulttable.Rows.Count & " Rows were inserted to table '" & tablename & "'!", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox("Unexpected Error in SQL bulkCopy: " & ex.Message, MsgBoxStyle.Critical)
Me.Cursor = Cursors.Default
End Try
End If
End Sub
Private Sub rbTable_CheckedChanged(sender As Object, e As EventArgs) Handles rbTable.CheckedChanged
If rbTable.Checked Then
MethodTextBox.Text = "TABLE"
lblMethodname.Text = "Table Name"
Else
lblMethodname.Text = "FuBa Name"
MethodTextBox.Text = "FUBA"
End If
End Sub
Private Sub MethodTextBox_TextChanged(sender As Object, e As EventArgs) Handles MethodTextBox.TextChanged
If MethodTextBox.Text = "TABLE" Then
rbTable.Checked = True
End If
End Sub
Private Sub btnGetResult_Click(sender As Object, e As EventArgs) Handles btnGetResult.Click
Dim SAPCon As New R3Connection
Try
Me.Cursor = Cursors.WaitCursor
If LogErrorsOnly = False Then ClassLogger.Add("", False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP Connect started:", False)
ERPConnect.LIC.SetLic("W86DWC992C")
SAPCon.Host = HostTextBox.Text
SAPCon.SystemNumber = SystemNumberTextBox.Text
SAPCon.UserName = UserNameTextBox.Text
SAPCon.Password = PasswordTextBox.Text
SAPCon.Client = ClientTextBox.Text
SAPCon.Language = LanguageTextBox.Text
If LogErrorsOnly = False Then ClassLogger.Add(">> HOST: " & HostTextBox.Text & " |SystemNumber: " & SystemNumberTextBox.Text & " |UserName: " & UserNameTextBox.Text & " |Password: " & PasswordTextBox.Text & " |Client: " & ClientTextBox.Text & " |Language: " & LanguageTextBox.Text, False)
SAPCon.Open(False)
If LogErrorsOnly = False Then ClassLogger.Add(">> SAP-Connection created and connected!", False)
If LogErrorsOnly = False Then ClassLogger.Add("", False)
lblConnectionopen.Visible = True
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in SAP Connect: " & ex.Message, False)
MsgBox("Unexpected Error in SAP Connect: " & ex.Message, MsgBoxStyle.Critical)
lblConnectionopen.Visible = False
Me.Cursor = Cursors.Default
lblResult.Visible = False
GridControl2.DataSource = Nothing
lblSave.Visible = False
Exit Sub
End Try
Dim SAPResulttable As DataTable = New DataTable
If rbTable.Checked = True Then
Try
If LogErrorsOnly = False Then ClassLogger.Add(">> Get data from SAP Table", False)
Dim table As New ERPConnect.Utils.ReadTable(SAPCon)
'Dim cWhereClause = "SAP_OBJECT EQ '<ObjectType>' AND AR_DATE GE '<AR_DATE>'"
'Dim sSearchDate1 = "20160101"
'cWhereClause = Replace(cWhereClause, "<ObjectType>", Objekttyp.Text)
'cWhereClause = Replace(cWhereClause, "<AR_DATE>", sSearchDate1)
If WHERE_CLAUSETextBox.Text <> "" Then
table.AddCriteria(WHERE_CLAUSETextBox.Text)
End If
'ClassLogger.Add(">> cWhereClause: " & cWhereClause, False)
'table.AddField("MATNR")
'table.AddField("MAKTX")
' table.AddCriteria("SPRAS = 'DE'")
table.TableName = TableNameTextBox.Text
If txtLimitRows.Text <> "" Then
table.RowCount = CInt(txtLimitRows.Text)
End If
If LogErrorsOnly = False Then ClassLogger.Add(">>Table.Run executed", False)
table.Run()
SAPResulttable = table.Result
Try
GridControl2.DataSource = SAPResulttable
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in GridControl2 >> DataTable 1: " & ex.Message, False)
MsgBox("Unexpected Error in GridControl2 >> DataTable: " & ex.Message, MsgBoxStyle.Critical)
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
End Try
Dim i As Integer
'For Each row As DataRow In resulttable.Rows
' ClassLogger.Add(">> " & row.Item(0).ToString & "|" & row.Item(1).ToString & "|" & row.Item(2).ToString)
'Next
SAPCon.Close()
If txtLimitRows.Text <> "" Then
lblResult.Text = "Result of SAP Table (limited to " & txtLimitRows.Text & " rows)"
Else
lblResult.Text = "Result of SAP Table (unlimited) >> " & SAPResulttable.Rows.Count & " rows retreived"
End If
lblResult.Visible = True
Me.Cursor = Cursors.Default
lblSave.Visible = False
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in SAP Table Result: " & ex.Message, False)
MsgBox("Unexpected Error in SAP Table Result: " & ex.Message, MsgBoxStyle.Critical)
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
End Try
End If
If rBFuba.Checked = True Then
If LogErrorsOnly = False Then ClassLogger.Add(" >> Get data from FuBa-Function", False)
Dim func As RFCFunction
Try
func = SAPCon.CreateFunction(TableNameTextBox.Text)
If LogErrorsOnly = False Then ClassLogger.Add(" >> func.Execute", False)
func.Execute()
If LogErrorsOnly = False Then ClassLogger.Add(" >> function was executed", False)
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in Create SAP Function: " & ex.Message, False)
MsgBox("Error in Create SAP Function: " & ex.Message, MsgBoxStyle.Critical)
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
Exit Sub
End Try
Dim FUNCT_ROW_COUNT As Integer = 1
Dim RowMaskresult As String
Try
If LogErrorsOnly = False Then ClassLogger.Add(" >> Checking FunctionExecute", False)
'Den ersten Wert ausgeben lassen um die Spalten zu splitten
For Each row As RFCStructure In func.Tables("T_INDEX").Rows
Try
If row.Item(1).ToString.Contains(FuBa_SplitCharacterTextBox.Text) Then
RowMaskresult = row.Item(1).ToString
If LogErrorsOnly = False Then ClassLogger.Add(" >> RowMask result: " & RowMaskresult, False)
FUNCT_ROW_COUNT = 2
End If
Catch ex As Exception
RowMaskresult = "ErrorInGetSeperatedResult: " & ex.Message
End Try
If FUNCT_ROW_COUNT = 2 Then Exit For
Next
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in Checking FunctionExecute: " & ex.Message, False)
MsgBox("Error in Checking FunctionExecute: " & ex.Message, MsgBoxStyle.Critical)
SAPCon.Close()
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
Exit Sub
End Try
FUNCT_ROW_COUNT = 1
Dim ColCount As Integer
If FuBa_SplitCharacterTextBox.Text <> "" And RowMaskresult.Contains(FuBa_SplitCharacterTextBox.Text) Then
Try
If LogErrorsOnly = False Then ClassLogger.Add(" >> Define Datatable with SplitCharacter method", False)
Dim split() = RowMaskresult.Split(FuBa_SplitCharacterTextBox.Text)
If split.Length > 0 Then
ColCount = split.Length - 1
If LogErrorsOnly = False Then
ClassLogger.Add(" >> ColCount: " & ColCount.ToString, False)
ClassLogger.Add(" >> Split-Results: ", False)
For Each s As String In split
ClassLogger.Add(" # " & s, False)
Next
End If
For x = 0 To ColCount
' Declare DataColumn and DataRow variables.
Dim column As DataColumn
column = New DataColumn()
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "Column " & x.ToString
SAPResulttable.Columns.Add(column)
Next
If LogErrorsOnly = False Then ClassLogger.Add(" >> Datatable Columns after Split created", False)
' Create new DataRow objects and add to DataTable.
'Jetzt die Zeilen der Function durchlaufen
For Each row As RFCStructure In func.Tables("T_INDEX").Rows
Dim new_row As DataRow
If txtLimitRows.Text <> "" Then
If CInt(txtLimitRows.Text) = FUNCT_ROW_COUNT Then
Exit For
End If
End If
new_row = SAPResulttable.NewRow()
Dim Rowresult As String = row.Item(1).ToString
If Rowresult.Contains(FuBa_SplitCharacterTextBox.Text) Then
Dim RowSplit() = RowMaskresult.Split(FuBa_SplitCharacterTextBox.Text)
Dim col_index = 0
'Jetzt die Spaltenwerte in die NewRow eintragen
For x = 0 To ColCount
new_row(x) = RowSplit(x)
Next
SAPResulttable.Rows.Add(new_row)
End If
Next
End If
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in Create Datatable with split-method: " & ex.Message, False)
MsgBox("Unexpected Error in Create Datatable with split-method: " & ex.Message, MsgBoxStyle.Critical)
SAPCon.Close()
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
Exit Sub
End Try
Else
If LogErrorsOnly = False Then ClassLogger.Add(" >> Define Datatable WITHOUT SplitCharacter method", False)
' Declare DataColumn and DataRow variables.
Dim column As DataColumn
' Create new DataColumn, set DataType, ColumnName and add to DataTable.
column = New DataColumn()
column.DataType = System.Type.GetType("System.Int32")
column.ColumnName = "id"
SAPResulttable.Columns.Add(column)
' Create second column.
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "FuBa-Result"
SAPResulttable.Columns.Add(column)
If LogErrorsOnly = False Then ClassLogger.Add(" >> Datatable Columns created", False)
' Create new DataRow objects and add to DataTable.
Try
For Each row As RFCStructure In func.Tables("T_INDEX").Rows
Dim new_row As DataRow
If LogErrorsOnly = False And FUNCT_ROW_COUNT = 1 Then
ClassLogger.Add(" >> The first 30 rows will be logged", False)
End If
If txtLimitRows.Text <> "" Then
If CInt(txtLimitRows.Text) = FUNCT_ROW_COUNT Then
Exit For
End If
End If
new_row = SAPResulttable.NewRow()
Dim Result As String
Try
Result = row.Item(1).ToString
Catch ex As Exception
Result = "2 Empty"
End Try
new_row("id") = FUNCT_ROW_COUNT
new_row("FuBa-Result") = Result
If LogErrorsOnly = False And FUNCT_ROW_COUNT <= 30 Then
ClassLogger.Add(" >> Result Row: " & Result, False)
End If
FUNCT_ROW_COUNT += 1
SAPResulttable.Rows.Add(new_row)
Next
Catch ex As Exception
ClassLogger.Add(">> Unexpected Error in Create SAP Function-Create Datatable: " & ex.Message, False)
MsgBox("Error in Create Datatable: " & ex.Message, MsgBoxStyle.Critical)
SAPCon.Close()
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
Exit Sub
End Try
End If
'Jetzt dem Grid die Datasource zuordnen
Try
If SAPResulttable.Rows.Count > 0 Then
GridControl2.DataSource = SAPResulttable
End If
SAPCon.Close()
If txtLimitRows.Text <> "" Then
lblResult.Text = "Result of FuBa (limited to " & txtLimitRows.Text & " rows)"
Else
lblResult.Text = "Result of FuBa (unlimited) >> " & SAPResulttable.Rows.Count & " rows retreived"
End If
lblResult.Visible = True
Me.Cursor = Cursors.Default
lblSave.Visible = False
Catch ex As Exception
SAPCon.Close()
lblResult.Visible = False
Me.Cursor = Cursors.Default
lblSave.Visible = False
GridControl2.DataSource = Nothing
ClassLogger.Add(">> Unexpected Error in GridControl2 >> DataTable 2: " & ex.Message, False)
MsgBox("Error in GridControl2 >> DataTable: " & ex.Message, MsgBoxStyle.Critical)
End Try
End If
End Sub
Private Sub rBFuba_CheckedChanged(sender As Object, e As EventArgs) Handles rBFuba.CheckedChanged
If rBFuba.Checked = True Then
Me.FuBa_SplitCharacterTextBox.Visible = True
lblSplitchr.Visible = True
Else
Me.FuBa_SplitCharacterTextBox.Visible = False
lblSplitchr.Visible = False
End If
End Sub
End Class