Files
AVISO/Aviso/cEasyBinding.vb
2019-10-28 09:30:39 +01:00

96 lines
3.9 KiB
VB.net

Imports System.Data.SqlClient
Imports VERAG_PROG_ALLGEMEIN
Public Class cEasyBinding
Public 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 = "AVISO")
Select Case conn
Case "AVISO" : connection = cDatenbankAVISO.GetNewOpenConnection(True)
Case "FMZOLL" : connection = cDatenbankAVISO.GetNewOpenConnectionFMZOLL(True)
End Select
End Sub
Public Sub initBinding(sql, table)
Try
bindingSource.CancelEdit()
bindingSource = New BindingSource
bindingdataTable.Clear()
bindingdataTable = New DataTable(table)
bindingdataTable.TableName = table
bindingdataSet.Clear()
Try
Me.bindingDataAdapter = New SqlDataAdapter(sql, connection)
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