, , , , ,

Auto Complete Example in Asp.net C#

There are too many methods to make an auto complete text box in asp.net

You can use JQuery (Use jquery for Auto Complete), Ajax Auto complete extender and so on. If you will search on internet you will find so many examples which are using JQuery or Ajax Extender to show auto complete on websites.

I had searched on many sites I had tried so many examples to populate auto complete list but, unfortunately, I am not able to find an efficient auto complete example.

Finally after searching for many hours I had decided to write my own code.

My example is using an asp.net C# to show auto complete. Here, code goes
Download Code

Run and try typing some numbers or text. You will find an auto complete list on page
Read More
, , , , , ,

How to use Enterprise Library using C#


The Microsoft Enterprise Library is a collection of reusable software components (application blocks) designed to assist software developers with common enterprise development cross-cutting concerns (such as logging, validation, data access, exception handling, and many others). Application blocks are a type of guidance; they are provided as source code, test cases, and documentation that can be used "as is," extended, or modified by developers to use on complex, enterprise-level line-of-business development projects.

Some Features

While using enterprise library you have to concern about opening and closing connections.
This will automatically handle connection pooling and other important errors of connection and database.

I had used Enterprise Library 2.0 - January 2006.
To use Enterprise Library you required to add reference of following files:-

  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.EnterpriseLibrary.Configuration.Design.dll
  • Microsoft.Practices.EnterpriseLibrary.Data.dll
  • Microsoft.Practices.ObjectBuilder.dll
View code below which I had used to connect one of my tables to master database.

#region Using

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;


public class spt_monitor


public DataTable Read()




Database db = DatabaseFactory.CreateDatabase("masterConnection");

DbCommand dbCommand = db.GetSqlStringCommand("Select * from spt_monitor");

DataSet dataSet = db.ExecuteDataSet(dbCommand);

return dataSet.Tables[0];




return null;




This code is simply loading a dataset without much code.

Download Enterprise Library from following links:-

Download another tool which will generate code for Enterprise Library from crococode.com


You can also download code running code which is using master database as example.


Goals for Enterprise Library

Enterprise Library is a collection of application blocks intended for use by developers who build complex, enterprise-level applications.

Enterprise Library is used when building applications that are typically to be deployed widely and to interoperate with other applications and systems. In addition, they generally have strict security, reliability, and performance requirements.

The goals of Enterprise Library are the following:

  • Consistency. All Enterprise Library application blocks feature consistent design patterns and implementation approaches.
  • Extensibility. All application blocks include defined extensibility points that allow developers to customize the behavior of the application blocks by adding their own code.
  • Ease of use. Enterprise Library offers numerous usability improvements, including a graphical configuration tool, a simpler installation procedure, and clearer and more complete documentation and samples.
  • Integration. Enterprise Library application blocks are designed to work well together or individually.
Read More
, , ,

How to create base class in C#


One of the many benefits of object-oriented programming is that it allows for reuse of logic. For example, classes can be created that contain a base level of functionality. These base classes can then be extended through inheritance to create new classes that encompass the functionality of the base class along with any custom logic needed. The end result is that as a developer, once the base class has been created, you can extend and enhance the functionality of the base class with minimal effort.

Since the .NET Framework is built upon the principles of object-oriented programming (OOP), it's no surprise that ASP.NET borrows heavily from the tenets of OOP, one such tenet being inheritance. The base functionality for all ASP.NET pages is spelled out by the Page class in the System.Web.UI namespace. This class defines the essential properties, methods, and
events for an ASP.NET page, including such members as:
  • The intrinsic objects - Response, Request, Session, and so on,
  • Common properties - IsPostBack, IsValid, and others,
  • Methods used throughout the page lifecycle, such as SavePageViewState(),

ProcessRequest(), RaiseChangedEvents(), and others.

While all ASP.NET pages must be derived from the Page class, they need not be directly derived. That is, an ASP.NET page may extend a class that, itself, extends the Page class.

In fact, when using the code-behind model for creating ASP.NET pages the actual ASP.NET page
is derived from the code-behind class, with the code-behind class being derived from the Page class.

/***********Why I had used *****************************/
Let me describe why I had used base class.

I have a website of around 100 pages; most of the pages are only available after login.

To restrict unauthorized access to pages generally we check for session values.

I have a session variable which contains a value, if that variable is null I have to assume this is an unauthorized user.

Now, what I had done to resolve the problem I had made a base class which will contains my code of checking session variables and redirect to login page. Then I had simply inherited page from the page base class to reduce my code and increase efficiently of website

download code which I had written to use base class

In fact, oftentimes it makes sense to create a base class for a particular ASP.NET Web application that extends the Page class and have all code-behind classes derive from this class, rather than directly from the Page class. This universal base class can contain its own properties, methods, or events that are common to all pages in the particular ASP.NET application, or it can extend the functionality of existing methods or properties.

Regardless of whether the code-behind model or inline script model is used, when the .aspx page is visited, the ASP.NET engine translates the markup section into a class and then compiles that class. If the code-behind model is being used, then the auto-generated class is derived from the page's corresponding code-behind class. On the other hand, if the inline script model is being used then the auto-generated class is derived directly from the Page class. The following diagram illustrates the resulting type graph used by both models.

Regardless of whether the code-behind model or inline script model is used, when the .aspx page is visited, the ASP.NET engine translates the markup section into a class and then compiles that class. If the code-behind model is being used, then the auto-generated class is derived from the page's corresponding code-behind class. On the other hand, if the inline script model is being used then the auto-generated class is derived directly from the Page class. The following diagram illustrates the resulting type graph used by both models.

The Basics of a Base Class
As the type graph examined above shows, the auto-generated ASP.NET page class must extend the Page class, but can do so indirectly, through a code-behind class, for example. But there's no reason why there can only be one level of encapsulation between the auto-generated class and the essential Page class. You can optionally create a base class from which all pages will derive from. When creating a base class, you'll typically have this base class extend the Page class. If you are using the code-behind model your code-behind classes will need to be modified to inherit from the new base class as opposed to the Page class. If you are using the inline script block model, you'll need to use the @Page directive's Inherits keyword to specify the type name of the class you want the auto-generated class to extend. With a custom base class the type graph for an ASP.NET page morphs into the one shown below:

As you can see in the type graph, the base class must derive from the Page class. ASP.NET page's that want to utilize the functionality of the base class will derive from this base class rather than from the Page class directly.

When creating a base class you might decide to add additional properties or methods, which are as simple as adding the appropriate properties and methods to the base class itself. Additionally, you'll likely need to extend the functionality of the Page class, adding logic at particular points in the page's lifecycle, such as common code that should run in response to the Page's Load event. To accomplish this, you'll want to override the appropriate method: OnInit to respond to the Init event; OnLoad to respond to the Load event; and OnPreRender to respond to the PreRender event.
Read More
, , , , , , ,

How to send e-mail using C# with Attachment

When I am working in core asp mail sending is one of the biggest task.

For mail sending I have to check for third party on server. I have to according to third party avalibale on different server.

But, now say thanks to Microsoft who had made that task easy.

Sending a email using ASP.NET 2.0 and C# 2.0 is actually very simple.

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email.

You have to write this code to your page.

public static void Send(string SMTPServerName, string SMTPUserName, string SMTPPassWord, string MailFrom, string MailTo, string CC, string BCC, string Subject, string Body, bool BodyHTML, string Attachment)
MailMessage message = new MailMessage(MailFrom, MailTo, Subject, Body);

//Adding multiple To Addresses
foreach (string sTo in MailTo.Split(",".ToCharArray()))
if (MailTo != sTo)

//Adding multiple CC Addresses
if (CC.Length > 0)
foreach (string sCC in CC.Split(",".ToCharArray()))

//Adding multiple BCC Addresses
if (BCC.Length > 0)
foreach (string sBCC in BCC.Split(",".ToCharArray()))
//Adding multiple BCC Addresses
if (Attachment.Length > 0)
foreach (string sAttachment in Attachment.Split(",".ToCharArray()))
Attachment attachment = new Attachment(sAttachment);
SmtpClient emailClient = new SmtpClient(SMTPServerName);
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(SMTPUserName, SMTPPassWord);
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
message.IsBodyHtml = BodyHTML;

Or you can also download code from.
Read More