Hi
You have done good job for audit example.
But its working only on gridview.
I want same but work into my single form. INSERT/UPDATE/DELTE.
Please help.
Regards
Manish
What I have tried:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration
Imports System.Collections.Generic
Imports System.Collections.Specialized
Public Class CurrentFields
'Private Shared ReadOnly _connectionString As String
Private _colName As String
Private _colValue As String
'Public Shared Sub New()
Public _connectionString As String = WebConfigurationManager.ConnectionStrings("POManagerConnectionString").ConnectionString
'End Sub
Public Property CurrColName As String
Get
Return _colName
End Get
Set(ByVal value As String)
_colName = value
End Set
End Property
Public Property CurrColValue As String
Get
Return _colValue
End Get
Set(ByVal value As String)
_colValue = value
End Set
End Property
Public Function GetCurrentFields(ByVal WhereField As String, ByVal ID As Integer, ByVal TableName As String) As List(Of CurrentFields)
Dim results As List(Of CurrentFields) = New List(Of CurrentFields)()
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = Nothing
Dim dad As SqlDataAdapter = Nothing
Dim strSelectSql As String = "SELECT * FROM " & TableName & " where " & WhereField & "=" & ID
' strSelectSql += " WHERE " & WhereField = ID
Try
Dim conn As SqlConnection = New SqlConnection(_connectionString)
dad = New SqlDataAdapter(strSelectSql, conn)
dad.Fill(ds)
dt = ds.Tables(0)
For Each row As DataRow In dt.Rows
For Each column As DataColumn In dt.Columns
Dim currFlds As CurrentFields = New CurrentFields()
currFlds.CurrColName = column.ColumnName
currFlds.CurrColValue = row(column).ToString()
results.Add(currFlds)
Next
Next
Catch
End Try
Return results
End Function
End Class
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Public Class InsertAuditData
Public Shared Sub insertAuditChanges(ByVal TableName As String, ByVal Operation As String, ByVal PerformedBy As String, ByVal FieldName As String, ByVal OldValue As String, ByVal NewValue As String)
Dim strConn As String = WebConfigurationManager.ConnectionStrings("POManagerConnectionString").ConnectionString
Using conn As SqlConnection = New SqlConnection(strConn)
Dim strUpdate As String = "INSERT INTO AUDIT_Table (PageName, Operation, OccurredAt, PerformedBy, FieldName, OldValue, NewValue) VALUES "
strUpdate += "('" & TableName & "', '" & Operation & "', '" + DateTime.Now & "' ,'" & PerformedBy & "' , '" & FieldName & "', '" & OldValue & "', '" & NewValue & "')"
Dim cmd As SqlCommand = New SqlCommand(strUpdate, conn)
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Sub
End Class
When i save data then i put this code but something wrong with myDE and e.NewValues because its not gridview ??? It's working fine with gridview.
' Start Audit
' ----------------------------------------------------------------------------------------
Dim ID As String = txtPOID.Text
Dim currFlds As CurrentFields = New CurrentFields()
currFieldList = currFlds.GetCurrentFields("POID", ID, "PurchaseOrder")
Session("CurrData") = currFieldList
currFieldList = CType(Session("CurrData"), List(Of CurrentFields))
'Dim myDE As DictionaryEntry
'myDE.Value = currFlds.CurrColValue
For Each myDE As DictionaryEntry In e.NewValues
Dim i As Integer = 0
Dim key As String = myDE.Key.ToString()
For Each currFld As CurrentFields In currFieldList
If currFld.CurrColName = key Then
Exit For
End If
i += 1
Next
If myDE.Value IsNot Nothing Then
Dim newVal As String = myDE.Value.ToString()
If currFieldList(i).CurrColValue <> newVal Then
InsertAuditData.insertAuditChanges("Edit Purchase Order", "UPDATE", Session("Username"), key, currFieldList(i).CurrColValue, newVal)
End If
Else
If currFieldList(i).CurrColValue <> "" Then
InsertAuditData.insertAuditChanges("Edit Purchase Order", "UPDATE", Session("Username"), currFieldList(i).CurrColName, currFieldList(i).CurrColValue, "")
End If
End If
Next