Thursday, May 28, 2009

BuildManager To Get Detail About Dynamic Compiled Assembly

Here i am going to explain how one can use BuildManager to get detail about dynamic generated Assembly.
In ASP.net 2.0 or ASP.net 3.5 , there are two type of projects. One is ASP.net Web Application and Another one is ASP.net WebSite Project. Significant difference between these two is how they compiled or published.

1. When “ASP.net WebApplication“ project published it create only Single DLL file in Bin for whole application and DLL name is also predetermined. Also when any new item added to the Project ( Either Web Form or Web User Control ) it comes under one namespace.

2. When “ASP.net WebSite“ project published then it create many DLL file in bin directory. No of DLL depend on condition you choose when publish “ASP.net WebSite” project.

image
- When only first checkbox is checked then it create assembly per directory. In following image you can see one sample solution. So it create one assembly for Top Level directory and their page and control and one assembly for TestControls directory.

       image

- When second checkbox is selected then it creates separate assembly for each page and each controls.

Now main thing “BuildManager” comes into picture. 

1. If you have “ASP.net WebApplication” project then you can access of another type in project via namespace. For Example you have two page "Default.aspx” and “Default2.aspx” and you want to just create instance of “Default.aspx” via its type in “Default2.aspx”.

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        public string GetMessage()
        {
              return “Hello”;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            WebApplication3._Default dForm = new _Default();
            Response.Write(dForm.GetMessage());
         }
    }
}
so it is very easy for “ASP.net WebApplication” project.

2.  For “ASP.net Web site“ type of project this is not a case. This type of project does not allow you to access another simple way as above. For that you have to use BuildManager Class.

public partial class _Default : System.Web.UI.Page
{   
protected void Page_Load(object sender, EventArgs e)
{   
// You can not access Default2 class directly.    
// Get Type using BuildManager.
// Here i specified virtual path.

Type compiled_o =  System.Web.Compilation.BuildManager.GetCompiledType("~/Default2.aspx");                
//Detail about assembly. Fully qualified name which contain dynamic assembly name too.
Response.Write(compiled_o.FullName);
// Create instance of Type.
object obj =  Activator.CreateInstance(compiled_o);
// GetData is a Method to call.
System.Reflection.MethodInfo info = compiled_o.GetMethod("GetMessage"); 
// Now Call method . Here Method GetData is a instance method so we need to call
// Invoke method with obj. null as no argument required.
object data = info.Invoke(obj, null);
// Check that data contain value.
if (data != null)
{
  Response.Write(data.ToString());

}
}
public partial class Default2 : System.Web.UI.Page
{
    public string GetMessage()
    {
              return “Hello”;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

BuildManager class has static method GetCompiledType, which required virtual path as a input so you can even give virtual path of user control too.

Please give your comment on this.

Saturday, May 16, 2009

SMTP Setting to send outbound email

Here i am going to explain about steps required to configure SMTP (Local SMTP) to send outbound email.

Steps.
1. Go to run and Type inetmgr. After inetmgr ( Internet information services manager ) open. Right click on Default SMTP virtual server ( As shown in figure 1) . If this is not displayed in your inetmgr then you need to first install SMTP services using Add/ Remove component.
 1

2. After right click on Default SMTP virtual server go for property 
menu item. It will display as show in following image.
image
3. Now go in Access Tab and click on relay. Please select as shown in following image and click ok.
 2
4. After that go for delivery tab. ( Display as follows)
 image

5. Click on outbound security . In that tab select basic authentication. Here i am using by gmail account to send outbound email. AS gmail required TLS (security to transfer credential) , you have to select TLS encryption checkbox too.Then click on ok.
5
7. Now go for outbound connection button. It will display as follows. Please specify TCP port as 587 for gmail otherwise 25 as default.
3

8. Click on Advanced . Configure as follows . In case of other SMTP , you have to specify that smtp server.

4

Click ok and then finally click on apply. Now restart smtp server using inetmgr.
Note : Here i took Gmail smtp setting . For that you need to configure Gmail account from Gmail setting and click on IMAP enable.

Thursday, May 14, 2009

DataTable / DataSet and Linq

1. Querying DataTable using Linq 
    Here i am going to explain how you can use linq to get data from DataTable.
    You need to use AsEnumerable() method of DataTable object to query datatable using linq.
Example :  
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
DataRow dr = dt.NewRow();
dr["ID"] = "1";
dr["Name"] = "Test1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "2";
dr["Name"] = "Test2";
dt.Rows.Add(dr);

var result = from user in dt.AsEnumerable()
                     where user.Field<string>("ID") == "1"
                     select user;


2. Join Two DataTable using Linq.
     Example : Here i take two datatable.

        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        DataRow dr = dt.NewRow();
        dr["ID"] = "1";
        dr["Name"] = "Test1";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ID"] = "2";
        dr["Name"] = "Test2";
        dt.Rows.Add(dr);

        DataTable dt1 = new DataTable();
        dt1.Columns.Add("ID");
        dt1.Columns.Add("Product");
        dr = dt1.NewRow();
        dr["ID"] = "1";
        dr["Product"] = "Test-Product";
        dt1.Rows.Add(dr);

Linq Query:

var c = from p in dt.AsEnumerable()
join d in dt1.AsEnumerable() on p.Field<string>("ID") equals d.Field<string>("ID")
select new { ID = p.Field<string>("ID"), Name = p.Field<string>("Name"), Product = d.Field<string>("Product") };

3. Except and Intersect operation between two DataTable using Linq .

        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        DataRow dr = dt.NewRow();
        dr["ID"] = "1";
        dr["Name"] = "Test1";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["ID"] = "2";
        dr["Name"] = "Test2";
        dt.Rows.Add(dr);

        DataTable dt1 = new DataTable();
        dt1.Columns.Add("ID");
        dt1.Columns.Add("Name");
        dr = dt1.NewRow();
        dr["ID"] = "1";
        dr["Name"] = "Test1";
        dt1.Rows.Add(dr);

        var  exceptresult = dt.AsEnumerable().Except(dt1.AsEnumerable(), System.Data.DataRowComparer.Default);

         var intersectresult  = dt.AsEnumerable().Intersect(dt1.AsEnumerable(), System.Data.DataRowComparer.Default);

For both of above operation you need to specify DataRowComparer . System.Data.DataRowComparer is a static sealed class and its static property Default contain comparer reference.  You must have to specify comparer for this operation on Reference datatype.

Next blog i will give you CustomDataRow Comparer to make it more efficient.

Important Tool For .NET

1. Managed Stack Explorer 
    This tool is used to monitor all managed process and their threads.
    You can download that tool at following location DownLoad

2. Process Explorer
    This tool is same as Taskmanager but it will give you more detail than taskmanager. Even this tool show you process in parent child relation so it is easy to manager.
     You can find more detail and download location at following link Process Explorer.

3. CLRProfiler
    This tool is specially designed for CLR ( Common language runtime ) . This tool give you more detail about Heap graph of your application.
For .Net Framework 1.1
For .NET Framework 2.0