Click here to Skip to main content
Licence 
First Posted 23 May 2005
Views 20,282
Bookmarked 12 times

Useful classes for using with ASP/C#.NET applications

By | 23 May 2005 | Article
Some useful classes you can use when creating ASP/C# applications.

Introduction

Included in the source files are four useful classes for everyone to use. They are as follows:

  • ConfigObj class -- class to manage configuration details for the application.
  • MsSql class -- a class dedicated to handle MS-SQL connections. It currently allows both simple SQL and transactions.
  • Errorlogger class -- a class dedicated to do errorlogging.
  • OleDB class -- a class dedicated to handle OleDB connections.

An example to illustrate the use of these classes are also included.

Details

I will now explain what each class does in details.

ConfigObj class

This class is a manager for configuration details. It allows both program updating of the values of the keys and auto-updating of the keys stored in the configuration files. An example of its use is as follows:

//declares the new object
configobj.configobj config=new configobj.configobj(); 
config.addkey("config1"); //adds in the configuration name of the key
config.addkey("config2");

In the app.config or web.config, the setting will be as such:

<configuration>
 <appSettings>
  <add key="config1" value="value1"/>
  <add key="config2" value="value2"/> 
 </appSettings>
</configuration>

To use it, simply use the getkey function to return the value of the key.

Note that, when adding key, the name of the key must be the same as what it is in the configuration file.

The neat part of this class is that, if you are programming big applications with more than two programmers, using this class allows cleaner and neater codes for maintenance and ease of use. Enhancements are currently being done on this class to allow greater flexibility and usability.

MsSql class

This class is basically a class dedicated to access MS-SQL databases. It further enhances the current Microsoft MS SQL methods by adding in error capturing. An example of its use is as such:

//adds the configuration key into the configobj. 
//The connection is found in the .config file
config.addkey("mssql");
mssql.Sql sql=new Sql();//declare simple sql class.
sql.Constr=config.getkey("mssql"); //gets the connection string
sql.ExecuteNonQuery("Insert into the table " +
            "(field1test,field2test) values ("+"'1','2')");

mssql.sqltransobj transsql=new sqltransobj(); //declare transactions
transsql.Constr=config.getkey("mssql"); //gets the connection string
transsql.begin();
transsql.addtransaction("Insert into the table " +
           "(field1test,field2test) values ("+"'1','2')");
transsql.commit();
transsql.close();

The simple SQL class caters for error capturing and graceful exiting when it encounters an error, either in the connection or the SQL being sent. The transaction class, built on top of it, also has same functionality.

Enhancement is currently being done to include stored procedures handling.

Errorlogger Class

A class dedicated to do errorlogging. Nothing special though. All error messages thrown in are formatted and stored in a text file for viewing. An example of its use is as follows:

try
{
    ..do something
}
catch (Exception e)
{
     // capture the error
     ErrorLog.writetolog(e.Message+"from where ",e.StackTrace); 
}

Although simple, it proves to be a valuable class for any decent developers who require error handling. This class is used throughout the three other classes that is included also.

Enhancements is done on this class to make it send emails to a set of predefined users on encountering an error.

OleDB Class

This class is a further enhancement of the Microsoft OleDB class, similar concept to the MS-SQL class. What it provides basically are the four most used methods with error handling built into it.

The plus point of this class is that, any database connections required to use this class need only to supply the necessary parameters to make a new class that is easy to use. The source code given illustrates this clearly with the foxpro class built on top of it. And to use this class, developers simply has to do this:

configobj.configobj config=new configobj.configobj();
config.addkey("thetable");
config.addkey("initpath");
OleDB.VFPDB testconnect=new VFPDB();
testconnect.Source=config.getkey("thetable");
testconnect.executeNonQuery("insert into thetable " +
         "(field1test,field2test) values ("+"'1','2')");

Further enhancements on this class are also in progress.

Looking at the big picture

You might have realised after looking at the codes that, there are actually two base classes (errorlogger and configobj) with two other classes (mssql and oledb) built with these base classes as dependencies. The two base classes can however still make use of the other two classes without much difficulty, say like if the error messages are to be kept in the database, the class can be modified to include the mssql class to do so easily. That is the actual intention when building the nature of these classes, that is, each class is dedicated to doing only one task, and that if it requires any sub-services to enhance its functionality, it calls another class for service with messages only, and not by manipulating that object. Personally, I believe by following this concept of only using messages to communicate objects with objects, it greatly reduces enhancements and maintenance efforts and increases flexibility.

Final Word

These codes are not totally bug-free, please feel free to email me the bugs that are found. Also, I might also have explained some concepts wrongly. Last but not least, all suggestions are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kerry Teo

Web Developer

Singapore Singapore

Member

Worked as a Software Engineer, done ASP.Net with C#,ASP, Ms-Sql and crystal reporting. Currently working as an Analyst Programmer, dealing with C# windows and console applications, Ms-Sql and crystal reports. Playing with C# stuff during my free-time. Recently getting very interested and my hands dirty in .Net mobile applications. I have being brushing up my skills on documentation like UML and validation and verification. Now currently also doing Masters in knowledge engineering, doing things like Data mining, genetic algorithm, fuzzy logic

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhe download zip file not available Pinmembermohankan23:02 24 May '05  
GeneralRe: he download zip file not available Pinmemberbabeliii21:08 25 May '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 23 May 2005
Article Copyright 2005 by Kerry Teo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid