Imports System.Data.SqlClient Imports Gemeinsames Imports System.ComponentModel Imports System.Windows.Forms Public Class cEasyBinding Public WithEvents bindingDataAdapter As New SqlDataAdapter Public WithEvents bindingdataTable As New DataTable Public bindingdataSet As New DataSet Public WithEvents bindingSource As New BindingSource Dim connection As SqlConnection Dim initialized = False Public updateImmediately = False Sub New(Optional conn As String = "SDL") Select Case conn Case "SDL" : connection = cSqlDb.GetNewOpenConnection(False) Case "FMZOLL" : connection = cSqlDb.GetNewOpenConnectionFMZOLL(False) Case "ADMIN" : connection = cSqlDb.GetNewOpenConnectionLogin() Case "AVISO" : connection = cSqlDb.GetNewOpenConnectionAVISO() End Select End Sub Public Sub initBinding(sql, table, Optional List = Nothing) Try bindingSource.CancelEdit() bindingSource = New BindingSource bindingdataTable.Clear() bindingdataTable = New DataTable(table) bindingdataTable.TableName = table bindingdataSet.Clear() Try If List IsNot Nothing Then Dim cmd As New SqlCommand(sql, connection) For Each i In List 'MsgBox(i.Text.trim & " " & IIf(i.Value Is Nothing, DBNull.Value, i.Value)) cmd.Parameters.AddWithValue("@" & i.Scalarvariable.trim, IIf(i.Value Is Nothing, DBNull.Value, i.Value)) Next Me.bindingDataAdapter = New SqlDataAdapter(cmd) Else Me.bindingDataAdapter = New SqlDataAdapter(sql, connection) End If Catch ex As SqlException MessageBox.Show("Der Connection-String kann nicht verarbeitet werden. Wenden Sie sich an den Programmbetreuer.") End Try Me.bindingDataAdapter.FillSchema(bindingdataSet, SchemaType.Source, table) Me.bindingDataAdapter.Fill(bindingdataSet, table) Me.bindingdataTable = Me.bindingdataSet.Tables(table) Me.bindingSource.DataSource = Me.bindingdataTable Me.bindingdataTable.Locale = System.Globalization.CultureInfo.InvariantCulture initialized = True Catch ex As Exception MsgBox("BindingERR01: " & ex.Message) End Try End Sub Sub setARITHABORTOn() Try Using cmd As New SqlCommand("SET ARITHABORT ON", connection) cmd.ExecuteNonQuery() End Using Catch ex As Exception MsgBox("BindingERR02: " & "Fehler in setARITHABORTOn: " & ex.Message) End Try End Sub Public Function updateBinding(Optional tablename As String = "", Optional ContinueUpdateOnError As Boolean = False) As Boolean If Not initialized Then Return True Try bindingSource.EndEdit() Dim builder As New SqlCommandBuilder(bindingDataAdapter) bindingDataAdapter.Update(bindingdataSet, bindingdataTable.TableName) Me.bindingdataSet.AcceptChanges() Return True Catch ex As Exception MsgBox("Es ist ein SQL-Fehler beim Updaten der Daten aufgetreten:" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & "Die Werte werden zurückgesetzt!", MsgBoxStyle.OkCancel, "Datenbank-Fehler") Me.bindingSource.ResumeBinding() End Try Return False End Function Sub binddata(o As Object, bindingParam As String, bindingSource As BindingSource, dataName As String, Optional bindingNullValue As String = "", Optional formatString As String = "") o.DataBindings.Clear() o.DataBindings.Add(New Binding(bindingParam, bindingSource, dataName, True, DataSourceUpdateMode.OnPropertyChanged, bindingNullValue, formatString)) End Sub Private Sub bindingSource_DataError(sender As Object, e As BindingManagerDataErrorEventArgs) Handles bindingSource.DataError ' If the data source raises an exception when a cell value is ' commited, display an error message. If e.Exception IsNot Nothing Then ' AndAlso e. = DataGridViewDataErrorContexts.Commit Then MessageBox.Show("Daten-Fehler: " & vbNewLine & vbNewLine & e.Exception.Message) End If End Sub 'Private Sub bindingdataTable_ColumnChanged(sender As Object, e As DataColumnChangeEventArgs) Handles bindingdataTable.ColumnChanged ' If updateImmediately Then updateBinding() 'End Sub End Class