Sunday, December 31, 2006

Error "System.Web.HttpException: Maximum request length exceeded"

I have come across this error multiple times at work. This problem occurs because the default value for the maxRequestLength parameter in the section of the Machine.config or Web.Config file is 4096 (4 megabytes). As a result, files that are larger than this value are not uploaded by default.

To resolve this problem, use one of the following methods:

  • In the Machine.config file, change the maxRequestLength attribute of the <httpRuntime> configuration section to a larger value. This change affects the whole computer.
  • In the Web.config file, override the value of maxRequestLength for the application. For example, the following entry in Web.config allows files that are less than or equal to 1 GB to be uploaded:
                         <httpRuntime maxRequestLength="1048576" />

Max value for maxRequestLength attribute is "1048576" (1 GB) for .NET Framework 1.0 or 1.1 and "2097151" (2 GB) for .NET Framework 2.0.

Note: During the upload process of large files, built-in ASP.NET loads the whole file in memory before the user can save the file to the disk. Therefore, the process may recycle because of the memoryLimit attribute of the processModel tag in the Machine.config file. More info you can find in Microsoft KB article: http://support.microsoft.com/default.aspx?scid=kb;EN-US;295626

Sunday, December 17, 2006

Export GridView Data in MS-Excel

In the following example, I will demonstrate how to export GridView Data into MS-Excel using VB.NET. It uses the same business object to select data from database. I have simply formatted the GridView control from previous example.

Page Name: ExportGridViewToExcel.Aspx

------------------------------------

Note: You will be required to add - EnableEventValidation = "False" at Page Directive in order to Export to work.

-------------------------------------

Code Snippet
  1. <%@ Page Language="VB" AutoEventWireup="false" EnableEventValidation = "False" CodeFile="ExportGridViewToExcel.aspx.vb"
  2.         Inherits="ExportGridViewToExcel" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml" >
  7. <head runat="server">
  8.     <title>Export Customers Data</title>
  9. </head>
  10. <body>
  11.     <form id="frmCustomer" runat="server">
  12.     <div  style="text-align:center">   
  13.         <asp:Button ID="btnExportToExcel" runat="server" Text="Export to Excel" /><br />  
  14.         <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label> <br />
  15.         <asp:GridView ID="gdvCustomer" DataKeyNames="CustID" runat="server" AllowPaging="True" DataSourceID="odsCustomer"  
  16.             AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
  17.             BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical"
  18.             RowStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
  19.             <Columns>                
  20.                 <asp:BoundField DataField="CustID" HeaderText="Cust ID"  />                                    
  21.                 <asp:BoundField DataField="CustName" HeaderText="Name" SortExpression="CustName" />                    
  22.                 <asp:BoundField DataField="CustAddress" HeaderText="Address" SortExpression="CustAddress"/>                
  23.             </Columns>
  24.             <EmptyDataTemplate>                
  25.                 No Customer Record Found.                
  26.             </EmptyDataTemplate>            
  27.             <FooterStyle BackColor="#CCCC99" />
  28.             <RowStyle BackColor="#F7F7DE" />
  29.             <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
  30.             <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
  31.             <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
  32.             <AlternatingRowStyle BackColor="White" />
  33.         </asp:GridView>
  34.                
  35.           <asp:ObjectDataSource ID="odsCustomer" runat="server"
  36.               SelectMethod="GetCustomers"
  37.             TypeName="Vishwa.Example.Business.CustomerFac" >
  38.          </asp:ObjectDataSource>
  39.         </div>
  40.     </form>
  41. </body>
  42. </html>
 

Code Behind : ExportGridViewToExcel.Aspx.vb

Code Snippet
  1. Option Explicit On
  2. Option Strict On
  3. Imports System.IO
  4. ' Author : Vishwa Mohan
  5. ' Date : 12/15/2006
  6. ' Class : ExportCustomer
  7. ' Purpose: To Export Customers Record into Excel
  8. Partial Class ExportGridViewToExcel
  9.     Inherits System.Web.UI.Page
  10.     Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
  11.         If gdvCustomer.Rows.Count + 1 < 65536 Then
  12.             Me.lblMessage.Text = ""
  13.             Me.gdvCustomer.AllowPaging = False
  14.             'If you are binding GridView under Code Behind
  15.             'Me.gdvCustomer.DataSource = GetCustomer
  16.  
  17.             Me.gdvCustomer.DataBind()
  18.             Dim tw As New StringWriter()
  19.             Dim hw As New System.Web.UI.HtmlTextWriter(tw)
  20.             Dim frm As HtmlForm = New HtmlForm()
  21.             Page.Response.ContentType = "application/vnd.ms-excel"
  22.             Page.Response.AddHeader("content-disposition", "attachment;Customer.xls")
  23.             Page.Response.Charset = ""
  24.             Page.EnableViewState = False
  25.             frm.Attributes("runat") = "server"
  26.             Controls.Add(frm)
  27.  
  28.             frm.Controls.Add(gdvCustomer)
  29.             frm.RenderControl(hw)
  30.             Response.Write(tw.ToString())
  31.             Response.End()
  32.             Me.gdvCustomer.AllowPaging = True
  33.  
  34.             'If you are binding GridView under Code Behind
  35.             'Me.gdvCustomer.DataBind()
  36.         Else
  37.             Me.lblMessage.Text = "Too many rows - Export to Excel not possible"
  38.         End If
  39.     End Sub
  40. End Class

 

Page at the run Time

Saturday, December 02, 2006

The Four Wives

Once upon a time, there lived a king who had four wives. He adored his fourth queen so much that he always used to gift her with loads of ornaments and other such adornments. She was inseparable from the king and a perpetual accompaniment wherever he went.
  
He also loved his third wife a lot - he guarded her richness and grandeur against the evil eyes of then neighboring kings and others.

He loved his second wife so much that he shared all his thoughts especially with her. But unfortunately he never loved his first queen - he ignored her very presence and enjoyed with the other queens.

Days passed by and one day it so happened that the king fell terribly sick and the best of the physicians in the kingdom and the world could find a cure. It was certain that he was staring at his end and he was on his final days.

He calls his fourth queen over and sadly says "Look, I am on my deathbed and will leave this world anytime now. When I was hale and hearty, I used to take you everywhere that I went. Will you accompany me when I leave to the other world?" To his utter shock, the Queen refused to do so and made it abundantly clear that it was unreasonable for the king to expect this and she would not like to give up her life for him. Though shocked, he did not lose hope; he calls his third queen and poses the same question to her. The shock was greater, she refuses to accompany him and moreover she replied that she would remarry after his death and continue to lead a happy life. The king was crestfallen at this outrageous reply from his dearest queens but again rests his hopes on the second queen. He looks at her and she pretends to be sympathetic to him and replies, "O dear, when you die, I cannot accompany you" and justifies this decision saying, "Your last rites will be performed as per the scriptures and I will take the responsibility to ensure this"

The king realized the reality at this fag end of his life. He realized the bitter truth that no one would accompany him nor would be available during his final days. While he was brooding over this fact, his first wife says, "O sweetheart, I am with you wherever you go. Don't you worry that you will have to make this last journey alone" The king was surprised and then realized that she was the true wife who loved him for what he was - not his riches not even his health. But his regret was that it was too late to make amends and shower her with his love.

Yes...like this king, we all have four wives.

The fourth wife is our body - a physical commodity that burns into ashes or disintegrates back to the earth at the end. The third wife is the wealth that we earn which would be shared and become property of others after our death. The Second wife is the relatives and friends who can only remain with us till the cemetery or crematorium. The First wife is the one which remains in our heart, the Divine or God, which remains with us in all our births and rebirths. It saves us from grief at the later stages if we adopt our spiritual practices and be devoted to the divine during our younger years.