Click here to Skip to main content
15,885,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 14-Apr-18 9:08am

1 solution

C#
Dim strSelectSql As String = "SELECT * FROM " & TableName & " where " & WhereField & "=" & ID

C#
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 & "')"

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900