DateTime is a very important datatype and data in terms of realtime application. Some of the application that run timely manner and some of the application that generate data (log file) with time.
To generate such report you need to required store information about date and time.
In .NET there is datatype called DateTime. This contain all information about datetime but sometime problem with this too.
For example:
DateTime dt = DateTime.Now; // 6/6/2009 09:35:00 PM.
This is used to get current current time of system. If you store this time in file or in a database and later you retrieve then you get that time. So at last “6/6/2009 09:35:00 PM” this is going to store in a file.
Problem occur when you retrieve this later to display information and by some reason you make changes to current time zone of system or sometime timezone of application. So then also you get “6/6/2009 09:35:00 PM.” as data , which is not right.
So best way to store date is UTC format.
DateTime dt = DateTime.UtcNow and after retrieval convert it to LocalTime but for that also you have to confirm that stored DateTime is in Utc format.
DateTime structure contains methods called ToBinary() and FromBinary().
long binaryDate = DateTime.Now.ToBinary() ;
Now store ‘binaryDate’ value in database or text file. So later when you get this value.
DateTime dt = DateTime.FromBinary(binaryDate). This will generate correct Date and Time even after timezone changed. This is because of binaryDate( Long ) value contain information about TimeZone too. So it automatically adjust as per system timezone.
Apart from this DateTime structure contain one field called ‘Kind’. which contain current DateTime format kind and that is Utc , Local or Unspecified. Many of case when you store simple way DateTime and later you retrieve kind is unspecified. But when you store binary date and later you retrieve you get either Utc or Local.
Please give your comment or any new idea about storing datetime.
Think Better To Build Best.
Blog by Jinal Patel.
Microsoft .NET Technology,SharePoint 2007,SharePoint 2010,ASP.net MVC
Saturday, June 6, 2009
DateTime operation in .NET Application
Usage of aspnet_compiler.exe and aspnet_merge.exe
Whenever asp.net website or webapplication publish,aspnet_compiler.exe is used for compilation.
Example :
WebSite : c:\myProject\TestSite
1 . Go to Visual Studio Tool from program menu and start Visual studio command prompt.
or
If you do not have visual studio installed on your machine and just .NET Framework 2.0 installed on your machine then also you can use aspnet_compiler tool.
- Start > run and type cmd.
- set path=%windir%\Microsoft.NET\Framework\v2.0.50727
- aspnet_compiler –v / –p c:\myproject\testsite c:\testcompile
It will compile c:\myproject\testsite and put published site in c:\testcompile.
- When you compile site using above command site is not updatable after publish.
- If you want to site updatable after publish add –u option to it.
- It is good to add –f option in order to override existing published site.
- Fore more help just type aspnet_compiler /?
Now let discuss about aspnet_merge.exe tool. This tool is introduce later and it is used to merge all assemblies that is located in bin directory of published site. When you published website project many assemblies are in bin directories depending upon your option.
- In aspnet_compiler if you add –fixednames option then it will generate single assembles for each page otherwise it is generated per directory (this is default for asp.net website project).
- Main disadvantage of different assembles either way is problem with reflection. You are not able to get control and page information located in another assemblies without assemblies loading programmatically.
- aspnet_merge.exe tool merge all aspnet generated assemblies to single assemblies.
Example :
1. set path=%programfiles%\Microsoft SDKs\Windows\v6.0A\bin
2. aspnet_merge c:\testcompile
This will create single DLL ( assembly ) in bin directory.
3. To set generated assembly name use this.
aspnet_merge –o Single.dll c:\testcompile.
Hope this will help you.
Please give your comment and advice on this.
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.
- 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.
- 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.
2. After right click on Default SMTP virtual server go for property
menu item. It will display as show in following image.
3. Now go in Access Tab and click on relay. Please select as shown in following image and click ok.
4. After that go for delivery tab. ( Display as follows)
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.
7. Now go for outbound connection button. It will display as follows. Please specify TCP port as 587 for gmail otherwise 25 as default.
8. Click on Advanced . Configure as follows . In case of other SMTP , you have to specify that smtp server.
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
Tuesday, April 21, 2009
GMail SMTP Setting To Send Email From ASP.net Application
First of all , It is required to configured GMail.
1. Create Gmail account or use existing if you already have it.
2. Log in into Gmail.
3. Go Settings.
4. Go Forwarding and Enable IMAP/POP3
5. In that go to last section and enable IMAP.
Now in your Web Application / Web Site project , open web.config. Add following to web.config.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.gmail.com" port="587" userName="username@gmail.com" password="password" />
</smtp>
</mailSettings>
</system.net>
GMail IMap Service runnig at port 587 so you must required to configured that. Also Gmail required SSL authentication so it is required to enable ssl for SMTPClient.
MailMessage message = new MailMessage();
message.From = new MailAddress(from@gmail.com);
message.To.Add("to@gmail.com");
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
Yuo can not enable SSL in web.config so you must required to configure it explicitly.