Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C# 4.0
Article

Implementing WinForms Audit Trail using Binding Source

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Jun 2010CPOL2 min read 19.4K   174   4   3
Using BindSource control to implement Windows Forms applications Audit trial in .NET

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

Introduction

Implementing Audit Trail using Binding Source is one of the methods of auditing user interactions with Windows Forms in .NET application.

Such a method can help development with any methodology using third parties.

See Implementing Audit Trail using Entity Framework.

Background

A developer who is involved in Windows Forms applications may get stuck deciding which method to use or how to audit the application. The simplest way is to audit the database using triggers. But in fact, this method has many sensitive views, one of them is that if you save the original changed record into audit table; you have to get the actual values of related table. i.e., if the Employee table has a field of cityID, that cityid has value of 15 which is Cairo, if another user updates the city table and sets city of Cairo into Alex, then the first user audit will show the changes from Paris into Alex not into Cairo.

The proposed method is to develop a way to save the current readable values to user at the time of changing.

Using BindingSource control which is most used by any developer, you can audit the current working table prior to actual values represented to user's screen.

Using BindingComplete event of binding source, you track user interaction with the screen, finally when saving changes, you can also save the instance of the audit object.

Using the Code

To use this method, you may:

  • Create a new Windows form
  • Add and define your BindingSource object in form
  • Add BindingComplete event to above bindingSource object to track user changes as follows:
C#
// reading from database
if (e.BindingCompleteContext == BindingCompleteContext.ControlUpdate && 
	e.Binding.IsBinding)
{
if (! auditDictionary.ContainsKey(e.Binding.BindingMemberInfo.BindingField))
{
string OldValue = ReadDisplayValue(e.Binding.Control);
AuditTrial audit = new AuditTrial(e.Binding.BindingMemberInfo.BindingField, OldValue);
}
}

if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && 
	e.Binding.IsBinding)
{
if (auditDictionary.ContainsKey(e.Binding.BindingMemberInfo.BindingField))
auditDictionary[e.Binding.BindingMemberInfo.BindingField].NewValue = 
	ReadDisplayValue(e.Binding.Control);
}

The above method writes values into auditDictionary automatically adding a field with its values.

Finally, you may save the object of auditDictionary into database as Audit entry.

Points of Interest

If you are using such methods, I recommend setting this code into your base forms for your applications, as it is in generic form and can be applied into any data Provider.

License

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


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions

 
GeneralNo an article... Pin
Dave Kreskowiak29-Jun-10 2:55
mveDave Kreskowiak29-Jun-10 2:55 
GeneralComments Pin
Md. Marufuzzaman29-Jun-10 1:52
professionalMd. Marufuzzaman29-Jun-10 1:52 
GeneralNot an article Pin
R. Giskard Reventlov28-Jun-10 22:15
R. Giskard Reventlov28-Jun-10 22:15 
Would be better as blog post.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum

me, me, me

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.