15 September 2010

File Not Found Exception in Application_Error of Global.asax

I got this nasty error of "File Not Found." in Application_Error of Global.asax file on every web page request of my web application in ASP.NET. I try to check what is causing this File Not Found exception on every single request. Initially I try to catch the exception and check what is stack trace is saying but that is of not much help as there wasn't any indication what is causing it. Then I found the solution. Here it is for those who are trying to resolve it. Save some of your productive hours.

Step 1 : Set break point at Application_Error of Global.asax .

Step 2 : Start debugging your application and when the request hit the Application_Error break point type

 ((HttpApplication)sender).Context.Request.Url  

into Watch window of visual studio.

This will give you the information on which resource is missing and why this specific exception is generated on every single request.

Happy Coding !!!

23 May 2010

Show Session Timeout countdown on ASP.NET page

Hi,

I came across a really nice feature that you can use in ASP.NET to show session timeout to users. This provides rich user experience. I used javascript to create this facility. Hope this will help someone looking for the solution.

 var timeout = '<%= Session.Timeout * 60 * 1000 %>';  
 var timer = setInterval(function() { timeout -= 1000; document.getElementById('countDown').innerHTML = time(timeout); if (timeout == 0) { clearInterval(timer); alert('Your session has expired!') } }, 1000);  
 function two(x) { return ((x > 9) ? "" : "0") + x }  
 function time(ms) {  
 var t = '';  
 var sec = Math.floor(ms / 1000);  
 ms = ms % 1000  
 var min = Math.floor(sec / 60);  
 sec = sec % 60;  
 t = two(sec);  
 var hr = Math.floor(min / 60);  
 min = min % 60;  
 t = hr+":"+two(min) + ":" + t;  
 return "You session will timeout in " + t ;  
 }  


add a span named countDown to the page where you want to display this session timeout message. If you want it to display on all the pages once user is logged in then put it in Master page and voila !!! Your session timeout message will be there to warn user.

 <span id="countDown">  
  </span>  

14 April 2010

Creating Custom Membership Provider for Login Control in ASP.NET

You may have came across the situation where you want to use .NET's membership provider facility but don't want to use tables and stored procedures generated by aspnet_regsql.exe . You want to use your own simple Users table in your own database with just few fields like UserID, Password and Role. It may seems like a big task to create your own Membership Provider to use your own login logic but it is not that hard. Just follow the steps below :

1 > Create your own Membership Provider Class and inherit it from base MembershipProvider class. If you are using VB.NET the needed methods are added automatically. If you are using C# just right click the MembershipProvider class and add the properties and methods . Don't forget to import System.Configuration.Provider namespace.

The IDE will generate all the methods and properties and you don't have to implement all of them.

The only method that you need to implement is ValidateUser(string username, string password)

You can leave all the other methods throw an exception unless you explicitly want the facility provided by non implemented method.


public class MyCustomMemershipProvider : MembershipProvider  
 {  
 public int TryLogin(string id, string pass) // My own login method  
 {  
 int roleId = 0;  
 DatabaseUtilityHelper databaseHelper = new DatabaseUtilityHelper();  
 SqlConnection con = databaseHelper.GetBBCDatabaseConnection();  
 SqlParameter[] agentParams = new SqlParameter[] {  
 new SqlParameter("@UserId",id),  
 new SqlParameter("@Password",pass)};  
 con.Open();  
 SqlDataReader reader = DatabaseUtility.ExecuteReader(con, "login_user", CommandType.StoredProcedure, agentParams);  
 if (reader.Read())  
 {  
 roleId = Int32.Parse(reader.GetSqlValue(1).ToString());  
 }  
 else  
 {  
 roleId = 0;  
 }  
 con.Close();  
 return roleId;  
 }  
 public override string ApplicationName  
 {  
 get  
 {  
 throw new NotImplementedException();  
 }  
 set  
 {  
 throw new NotImplementedException();  
 }  
 }  
 .....  
 ..... // Generated Code  
 ....  
 ....  
 public override bool ValidateUser(string username, string password)  
 {  
 int result = TryLogin(username, password);  
 if (result > 0)  
 {  
 return true;  
 }  
 else  
 {  
 return false;  
 }  
 } 


2 > Add CustomeMemberShip Provider to your web.config




 <membership defaultProvider="MyCustomMemershipProvider">  
 <providers>  
 <clear/>  
 <add  
 name="MyCustomMemershipProvider"  
 type="BBCApplication.BusinessLogic.MyCustomMemershipProvider"/>  
 </providers>  
 </membership>  


3.> Add Membership Provider for your login control on Login.aspx page.

In the Properties window of the Login control set MembershipProvider to your MyCustomeMembershipProvider class.

That is it.
Off you go to your own login logic with custom membership provider.