Wednesday, November 23, 2011

Linq : Datetime comparison issues..

If you have taken a datetime field in the database and you want to compare it with another date using Linq then the below code will not work fine !!

WRONG :
var result = (from c in context.Clients
where c.RFS_OfficeUse.AptWithIntakeCoordDate < DateTime.Now
select c).ToList();
return result;

RIGHT :
var result = (from c in context.Clients
where c.RFS_OfficeUse.AptWithIntakeCoordDate < DateTime.SpecifyKind( DateTime.Now,DateTimeKind.Utc)
select c).ToList();
While creating the schema of database the Ado.net entity model will convert datetime database fields to UTC format automatically, so you need to first convert your datetime to UTC format.

Use MS Access for Asp.net Personalization

Asp.net provides default option for personalization, that is using Sql server.

What if sql server is not installed on a server ? Here you can use the Asp.net personalization using MS Access.

Just follow the simple steps :

Download the Sample Access Providers provided by Microsoft.
This is a .vsi file and you could run it to get installed. But rather then doing that, you simply rename the extension from vsi to zip and unzip the file.
you can see access.csproject and ASPNetDB.mdb files.
Now open the access.csproject project and build it, it should create a dll named "SampleAccessProviders.dll"
Open your website where you need to implement MS Access personalization.
Now add reference of 'SampleAccessProviders.dll' from the location you built it.
Copy ASPNetDB.mdb file under App_Data folder.
Now only some configuration settings are left!
Just open your website web.config
you need to the following settings:
First of all the connectionstring to the access database:
<connectionStrings>
<add name="AccessDbFile" connectionString="~/App_Data/ASPNetDB.mdb"
providerName="System.Data.OleDb"/>
connectionStrings>
Also change the authentication mode  Forms  this:
<authentication mode="Forms">
<forms loginUrl="mylogin.aspx" defaultUrl="Login.aspx"/>
authentication> 
After that you can simply copy in the providers that you need.

<membership defaultProvider="AccessMembershipProvider">
<providers>         <clear/>      
<add name="AccessMembershipProvider"              
type="Samples.AccessProviders.AccessMembershipProvider, SampleAccessProviders"
connectionStringName="AccessDbFile"        
enablePasswordRetrieval="false"              enablePasswordReset="false" 
requiresUniqueEmail="false"          
requiresQuestionAndAnswer="false"              minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"              
applicationName="SampleSite"              hashAlgorithmType="SHA1"      
passwordFormat="Hashed"/>     providers> membership>