A curry of Dependency Inversion Principle (DIP), Inversion of Control (IoC), Dependency Injection (DI) and IoC Container

A curry of Dependency Inversion Principle (DIP), Inversion of Control (IoC), Dependency Injection (DI) and IoC Container using C#. Dependency injection using C#. Inversion of control using C#. IoC using C#. Download sample – 77 KB Introduction Background Dependency Inversion Principle (DIP) Let’s try to understand DIP with some example Problems we will face if

Read More

Delegate in C#

Delegate in C# I am not going to write formal definition here because you will find lots of definition of delegate by googling. Most of the time, new developer faces problem with delegate. They think actually what it is and why they use it instead of method. In simple word: What is Delegate? Delegate is

Read More

Creating generic method or extension for searching through all the columns in DataTable in C#

Few days ago I worked on a project where database table has more than 80 columns. My client asked to create a search so that it can search through all the columns. So I have decided to create an extension method which will search through all the columns in DataTable. Creating generic method or extension

Read More

Executing *.sql file form C#

Sometimes we need to run *.sql script from our application for installing data base. SqlCommand is not right for running installation script because installation script consists of DDL and GO command. Here we use smo library for executing *.sql script. For doing above operation we need to add following references Microsoft.SqlServer.ConnectionInfo Microsoft.SqlServer.Smo Took me a

Read More

My Short Note

Use Asp.net Session from Class Library (or Class Library Project) Add First we need to add System.Web as a reference on our project. System.Web.HttpContext.Current.Session[“MyVariable”]; System.Web.HttpContext.Current.Session(“MyVariable”); How to write connection string in web.config file At first we need to add following namespace using System.Collections.Generic; using System.Web.Configuration; Now use following code to write on web.config file Configuration

Read More

Convert your Data Transfer Object to XML (XML Serialization and Deserialization)

Introduction When we need to transfer our object through internet or need to write in a file then it is necessary to convert it to XML. We can convert object to XML using XML Serialization. In this article we will discuss about how to convert object (It may be entity/ Data Container/ DTO) to XML.

Read More

Clearing all TextBoxes text from a Form using C#

When we develop any application form, then there will be a new button where we need to perform clear operation. We can clear all the TextBox texts from a Form or a Control like panel, tabe pages using following code foreach (Control c in this.Controls) { if (c is TextBox) { c.Text = string.Empty; }

Read More

Select Distinct Record from DataTable

If it is necessary to select distinct record from Data Table, then we can use following method. public DataTable SelectDistinct(DataTable SourceTable, string FieldName) { DataTable dt = new DataTable(); dt = SourceTable.Clone(); object LastValue = null; foreach (DataRow dr in SourceTable.Select(“”, FieldName)) { if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName]))) { LastValue = dr[FieldName]; dt.ImportRow(dr);

Read More

Sending Email using C# (with HTML body & Attachment )

Introduction This article is provide a basic idea how we can send Emails from a C# Windows Application. Here I have described how we can send mail with Attachment and HTML body. Description If we want to send mail then we need to know following things 1. SMTP Server address 2. Port No Here I

Read More

Converting List to DataTable in C#

There is no built in method for converting list to data table in C#. So we can implement following method for converting List to DataTable in C#. public DataTable ToDataTable(IList data) { PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(t)); DataTable table = new DataTable(); for (int i = 0; i < props.Count; i++) { PropertyDescriptor prop = props[i];

Read More