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

Continue reading

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”);

Continue reading

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.

Here we will use XML Serialization & Deserialization for converting our object to XML and XML to Object.

Continue reading

Run your ASP.net Application without IIS. (using Cassini)

It is easy to run your asp.net web application without installing any web server. For running your application without IIS you have to use Cassini Desktop Adapter.

    Features of Cassini Desktop Adapter

* Standalone Web Applications – now on the Desktop
* 100% Embedded Web server
* No Installation or GAC Required
* No Administrator Privileges Required
* No XML / Configuration Files
* Cassini On-Demand

Continue reading

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);
                }
            }
            
 Continue reading 

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 have used gmail as a SMTP Server
for gmail account
SMTP Server: smtp.gmail.com
SMTP port : 465 or 587
your account Name:###
your password:###

Continue reading

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];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (t item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

Converting DataView to DataTable in C#

Consider following code for converting DataView to DataTable in C#

DataTable dt = dv.ToTable();// dv = Data View

Consider following example

public DataTable GetFilterData(DataTable dt)
{

if (dt != null)
{
DataView dataView = dt.DefaultView;
dataView.RowFilter = filterCondition; //some condition like": ColumnName like '%this%'; 
dt = dataView.ToTable();
}
return dt;
}

Preventing / Blocking Page Review after Logout with Forms Authentication

After logging out we can prevent getting previous page or information by doing following things.

Add Code to Page Load event:
—————————–

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

Or We can use following code
Continue reading