Imports ERPConnect Imports System.Data.SqlClient Public Class frmSAP_FuBa Dim m_CountTo As Integer = 0 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 If rbTable.Checked Then MethodTextBox.Text = "TABLE" lblMethodname.Text = "Table Name" Else lblMethodname.Text = "FuBa Name" MethodTextBox.Text = "FUBA" 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 Me.Cursor = Cursors.WaitCursor If ClassSAP.ConnectionInit(HostTextBox.Text, SystemNumberTextBox.Text, UserNameTextBox.Text, PasswordTextBox.Text, ClientTextBox.Text, LanguageTextBox.Text) = True Then lblConnectionopen.Visible = True Else lblConnectionopen.Visible = False End If Me.Cursor = Cursors.Default '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 Dim SAPCon As New R3Connection Me.Cursor = Cursors.WaitCursor If ClassSAP.ConnectionInit(HostTextBox.Text, SystemNumberTextBox.Text, UserNameTextBox.Text, PasswordTextBox.Text, ClientTextBox.Text, LanguageTextBox.Text) = True Then lblConnectionopen.Visible = True Else lblConnectionopen.Visible = False Me.Cursor = Cursors.Default lblResult.Visible = False GridControl2.DataSource = Nothing lblSave.Visible = False Exit Sub End If Dim SAPResulttable As DataTable = New DataTable If rbTable.Checked = True Then End If If rBFuba.Checked = True Then Dim result = ClassSAP.FuBa_Returndatatable_Seperated(TableNameTextBox.Text, FuBa_SplitCharacterTextBox.Text, txtLimitRows.Text) If Not IsNothing(result) Then SAPResulttable = result End If End If If SAPResulttable Is Nothing Then SAPCon.Close() lblResult.Visible = False Me.Cursor = Cursors.Default lblSave.Visible = False GridControl2.DataSource = Nothing ClassLogger.Add(">> SAPResulttable is nothing", False) Exit Sub End If '###### Dim i = 0 Dim tablename = "[TBPMO_SAPFUBA_" & TempTableNameTextBox.Text & "]" Dim createtablesql = "CREATE TABLE TBPMO_SAPFUBA_" & TempTableNameTextBox.Text & " (" For Each col As System.Data.DataColumn In SAPResulttable.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 If LogErrorsOnly = False Then ClassLogger.Add(">> TempTable " & tablename & " exists and will now be deleted!", False) ClassDatabase.Execute_non_Query_withConn("DROP TABLE " & tablename, 1) End If ''Die Tabelle anlegen If LogErrorsOnly = False Then ClassLogger.Add(">> Create Table: " & createtablesql, False) ClassDatabase.Execute_non_Query_withConn(createtablesql, 1) Dim del = "delete from " & tablename ClassDatabase.Execute_non_Query_withConn(del, 1) 'If ClassDatabase.Execute_non_Query_withConn(createtablesql, 1) = False Then ' Me.Cursor = Cursors.Default ' MsgBox("Error while creating the temporary table. Please check the logfile!", MsgBoxStyle.Critical) ' Exit Sub 'End If Dim ins = "Insert into " & tablename & "(" Dim DT As DataTable = ClassDatabase.Return_Datatable("SELECT * FROM " & tablename) If Not IsNothing(DT) Then Dim anz As Integer = 0 For Each col As DataColumn In DT.Columns If anz = 0 Then ins = ins & col.ColumnName Else ins = ins & ", " & col.ColumnName End If anz += 1 Next ins = ins & ") VALUES (" End If Try 'For Each row As DataRow In SAPResulttable.Rows ' For x = 0 To CURRENT_COL_COUNT ' If x = 0 Then ' ins = ins & "'" & row.Item(x).ToString & "', " ' Else ' ins = ins & "'" & row.Item(x).ToString & "'" ' End If ' ins = ins & ")" ' Next ' ClassDatabase.Execute_non_Query_withConn(ins, 1) 'Next Dim OwnerCS As String OwnerCS = ClassDatabase.GetConnectionString(1) Dim SQLconnect As New SqlClient.SqlConnection SQLconnect.ConnectionString = OwnerCS 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(SAPResulttable) 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 " & SAPResulttable.Rows.Count & " Rows were inserted to table '" & tablename & "'!", False) MsgBox("Create table was successfull! " & vbNewLine & SAPResulttable.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 Dim sw As New Stopwatch sw.Start() m_CountTo = 100 Me.Cursor = Cursors.WaitCursor If ClassSAP.ConnectionInit(HostTextBox.Text, SystemNumberTextBox.Text, UserNameTextBox.Text, PasswordTextBox.Text, ClientTextBox.Text, LanguageTextBox.Text) = True Then lblConnectionopen.Visible = True Else lblConnectionopen.Visible = False Me.Cursor = Cursors.Default lblResult.Visible = False GridControl2.DataSource = Nothing lblSave.Visible = False sw.Stop() Exit Sub End If 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 '' AND AR_DATE GE ''" 'Dim sSearchDate1 = "20160101" 'cWhereClause = Replace(cWhereClause, "", Objekttyp.Text) 'cWhereClause = Replace(cWhereClause, "", 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 sw.Stop() 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 Dim result = ClassSAP.FuBa_Returndatatable_Seperated(TableNameTextBox.Text, FuBa_SplitCharacterTextBox.Text, txtLimitRows.Text) If Not IsNothing(result) Then SAPResulttable = result End If '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 ' If LogErrorsOnly = False Then ClassLogger.Add(" >> RowCount limited to" & txtLimitRows.Text & " rows. Exit SAP Function", False) ' 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() = Rowresult.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 ' FUNCT_ROW_COUNT += 1 ' 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 If SAPResulttable Is Nothing Then SAPCon.Close() lblResult.Visible = False Me.Cursor = Cursors.Default lblSave.Visible = False GridControl2.DataSource = Nothing sw.Stop() ClassLogger.Add(">> SAPResulttable is nothing", False) End If 'Jetzt dem Grid die Datasource zuordnen Try If SAPResulttable.Rows.Count > 0 Then GridControl2.DataSource = SAPResulttable End If SAPCon.Close() Dim elapsed As Double elapsed = sw.Elapsed.TotalSeconds sw.Stop() If txtLimitRows.Text <> "" And SAPResulttable.Rows.Count >= CInt(txtLimitRows.Text) Then lblResult.Text = "Result of FuBa (limited to " & txtLimitRows.Text & " rows) - Duration: " & Format(elapsed, "0.00") & " seconds" Else lblResult.Text = "Result of FuBa (unlimited) >> " & SAPResulttable.Rows.Count & " rows retrieved - Duration: " & Format(elapsed, "0.00") & " seconds" 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 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