async upload control in update panel ( web forms)

There is an issue with upload controls when used with update panels. Moreover they are not just supported.

The quick fix is:

Put this in your page load  Page.Form.Attributes.Add(“enctype”, “multipart/form-data”);

Put a trigger to your update panel : <Triggers>
                <asp:PostBackTrigger ControlID=”btnSubmit” />

Put your upload control : <asp:AsyncFileUpload ID=”fuImage” runat=”server” UploaderStyle=”Modern” Style=”margin-left: 100px;”
                                                onchange=”return ImageValidate()” />



Speed Up Your LINQ Statements with “Let”

This post being last post of the year 2011. Have a great going for year 2012! Wish all of you a happy prosperous new year 🙂

Using Linq Let keyword :

Old way :
var cheapCategories = from cat in Categories
where cat.Products.Average(p => p.Price) p.Price) };

New Way :
var cheapCategories = from cat in Categories
let AvgPrice = cat.Products.Average(p => p.Price)
where AvgPrice < 100
select new { cat.Name, AvgPrice };
Its code is pretty straight forward to the point!

GridView – Pass multiple values to Command Argument

<asp:TemplateField HeaderText="Start">
<asp:Button ID="btnTest" runat="Server" CommandName="Test" Text="Select" 
CommandArgument='<%#Eval("carid") + ","+Eval("year") %>' />                      

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   if (e.CommandName == "Test")
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
         Label1.Text= commandArgs[0];
         Label2.Text = commandArgs[1];

HTTP handler & HTTP module

An ASP.NET HTTP handler is the process (frequently referred to as the “endpoint”) that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.

An HTTP module is an assembly that is called on every request that is made to your application. HTTP modules are called as part of the ASP.NET request pipeline and have access to life-cycle events throughout the request. HTTP modules let you examine incoming and outgoing requests and take action based on the request.


Typical uses for custom HTTP handlers include the following:

  • RSS feeds   To create an RSS feed for a Web site, you can create a handler that emits RSS-formatted XML. You can then bind a file name extension such as .rss to the custom handler. When users send a request to your site that ends in .rss, ASP.NET calls your handler to process the request.
  • Image server   If you want a Web application to serve images in a variety of sizes, you can write a custom handler to resize images and then send them to the user as the handler’s response.

Typical uses for HTTP modules include the following:

  • Security   Because you can examine incoming requests, an HTTP module can perform custom authentication or other security checks before the requested page, XML Web service, or handler is called. In Internet Information Services (IIS) 7.0 running in Integrated mode, you can extend forms authentication to all content types in an application.
  • Statistics and logging   Because HTTP modules are called on every request, you can gather request statistics and log information in a centralized module, instead of in individual pages.
  • Custom headers or footers   Because you can modify the outgoing response, you can insert content such as custom header information into every page or XML Web service response.

Shortcuts i use in Visual Studio 2010

  • Hold Alt while selecting = column select mode (don’t need to continue holding).
  • ctrl+K, ctrl+C = comment out a section
  • ctrl+K, ctrl+U = uncomment a section
  • ctrl+M, ctrl+O = collapses all regions (functions, etc.)
  • ctrl+M, ctrl+L = shows all regions (functions, etc.)
  • ctrl+. (dot) = Show smart menu (whenever you see that weird blue underline, it brings up a menu of options related)
  • ctrl+N = New file dialog
  • Shift+Alt+Enter = Full screen mode (I only use this when I’m trying to show code to people on a projector or something)
  • ctrl+tab = goes between child windows and brings up the nice “active files” window so you can pick the file you want (at least in 2010 anyway).
  • F5 = debug (also in break mode causes the execution to continue, but if you don’t know that, that’s rather worrisome)
  • Ctrl+F5 = compiles and runs the app (no debug)
  • Shift+F5 = kills debugging session
  • F4 = displays the properties window
  • ctrl+K,ctrl+D = formats the code in the window
  • Shift+del = deletes a row
  • F12 = Go to definition

Error : Unable to load the specified metadata resource.

Recently while working on a generic Learning Management System product i came across this error – ” Unable to load the specified metadata resource.

Stack Trace would read like : “[MetadataException: Unable to load the specified metadata resource.] System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +2510308 System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +180 System.Data.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection`1 uriRegistry, MetadataArtifactAssemblyResolver resolver) +8564268 System.Data.Metadata.Edm.MetadataCache.SplitPaths(String paths) +271 System.Data.Common.Utils.<>c__DisplayClass2.<Evaluate>b__0() +19 System.Data.Common.Utils.Result.GetValue() +100 System.Data.Common.Utils.Memoizer`2.Evaluate(TArg arg) +181 System.Data.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections) +292 System.Data.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection() +29 System.Data.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor) +205 System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName) +27 eLMSDAL.eLMSEntities..ctor() in F:\Users\Machine\Documents\Visual Studio 2010\Projects\eLMS\eLMSDAL\eLMSModel.Designer.cs:34 eLMSDAL.UserDAL.UserInsert(String FirstName, String MiddleName, String LastName, DateTime DOB, String Address1, String Address2, String City1, String City2, String State1, String State2, String Country1, String Country2, String ZipCode1, String ZipCode2, String Email, String Password, String Mobile, String Resume, Boolean Status, DateTime CreatedDateTime, DateTime UpdatedDateTime, String UserRole) in F:\Users\Machine\Documents\Visual Studio 2010\Projects\eLMS\eLMSDAL\UserDAL.cs:13 eLMSBLL.UserBLL.Save(ArrayList& validationErrors) in F:\Users\Machine\Documents\Visual Studio 2010\Projects\eLMS\eLMSBLL\UserBLL.cs:42 UserRegistration.btnSubmit_Click(Object sender, EventArgs e) in e:\MyWork\eLMS\UserRegistration.aspx.cs:44 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563″
After wrecking my head for about 1 hour, i got the solution.

Please make sure you update the app.config file in (probably in Data Access Layer) and web.config (Probably be in Presentation layer)

App.config and web.config must work in harmony.

hope this help someone!


How to handle Empty Data in ASP.NET DataList and GridView

Go to your aspx web form and try this

<asp:DataList ID="dlContacts" runat="server">

<!-- Add your Item Template Here -->

<asp:Label ID="lblEmpty" Text="no result" runat="server"

For GridView:

  <table border="0px" cellpadding="5" cellspacing="5">
       Error Message here!
 <EmptyDataRowStyle CssClass="empty" ForeColor="Red" />