Tuesday, May 31, 2011

How to avoid the error "Maximum Request Length Exceeded"

Scenario :


I had a requirement to develop a page which has to upload the file to the DB server. Uploading a file to the server is a very easy thing with the FileUpload control. By default the maximum size allowed to upload is 4MB and if you want you can increase or decrease in web.config settings.

Problem :

There won't be any problem, if you upload the file size of less than the specified size in web.config. If the size of the file is more than the limit specified, the runtime will be thrown with the message of "Maximum Request Length Exceeded". The thing is it won't be catched in button click of the File upload. Let us see how can we fix this. I found the solution in web and explaining for you.

Solution:  

I'm not sure this is the best way to do it. But this also a solution. We could catch this in Application_BeginRequest event of Global.asax. Normally whatever the client request for a page or file this application BeginRequest event will be fired and the control goes to the page events. Since its going for all the postback and initial load. We can calculate the content length of each post by the below lines,

            HttpContext context = ((HttpApplication)sender).Context;
            int PageContentLength = context.Request.ContentLength;

In addition with that, we need to calculate the actual size of the page and do some calculations. Look at the entire code below, which have to be added in Global.asax.

Event in Global.asax


The last line redirects the control to the same page with the url querystring. Based on the query string, we'll display the error message in Attachment page. Another thing here is as soon as you displayed the error message you need to remove the querystring value.

Conclusion:


This solution may not be better for performance wise since this event is keep calling for all the page requests as well file requests like .JS or .CSS. Let me know if you have better solution than this.

Happy Coding....




Thursday, May 26, 2011

Find from which page the master file is invoked

Scenario

We may sometimes need to know from which page, the master file is invoked. Based on the file, we'll do some operation not for the other pages. For example if the user name in session is empty, it has to redirect to the login page otherwise the requested page should be opened. But at the same time, if the user request a login page, it should not redirect to the same page again.

Solution


In the above said scenario, we have to identify from where the master page is being called. We have the below code to identify from which page its called,

this.Parent.TemplateControl.AppRelativeVirtualPath.ToString() 


If you put this code in master page file, you will get to know the page from where it's invoked. Its taking the parents relativevirtualpath.


Happy Coding...



Wednesday, May 25, 2011

Change the upload file size in ASP.NET application

Problem:


This is a scenario i faced when i worked in web application. Our application supports to upload the documents. When the user tries to upload the file more than 4MB, the browser shows error message and . Initially I did the work around, check the size of the file and notify the user to upload less than 4 MB document.

Solution

I found that thats not the correct solution. We may stop breaking the application and google the web and found a very simple solution to solve the problem.

We need to add the below line in web.config file under System.web tag
<httpRuntime maxRequestLength="5120"/>

The httpRuntime tag has the property called maxRequestLength, which is to be specified in KB. The above shows the 5 MB file can be uploaded.

If you are not specifying the httpRuntime in web.config..the default size is 4MB.

Conclusion:


With that above work around, user can able to upload more than 4 MB and we can fix the size.

Happy Coding... :)

Tuesday, January 25, 2011

JavaScript Error - Object Not Found

Problem:
When you refer JavaScript/css/images files, it will be referred properly and the code works fine in local machine. But you may get JavaScript error "Object Not Found" when deployed in web servers.


Solution:
Kindly use the  symbol to avoid this Object Not Found error to specify the root directory.


Sample Code:
The below code which generates "Object Not Found" JavaScript error,
<link href="/Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
Or 
<link href="../Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />


Working Code:
Use  symbol before the / to refer the root level as shown below,
<link href="~/Content/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
In addition, if you are working in MVC, use Url.Content to get absolute URL as shown below, 
<script src="<%= Url.Content("~/Scripts/jquery-1.4.4.js") %>" type="text/javascript">script>


Conclusion
Make a practice of referring any external files by using the ~ symbol in the root level to avoid Object Not Fund error.


Happy Coding


Thursday, December 23, 2010

"Object Expected" error in MVC JQuery autocomplete

Problem :-


I have been working in MVC and its very interesting to work with it. I just started working on JQuery. I have encountered this problem when implementing autocomplete to Text box in MVC with JQuery. The JavaScript error "Object Expected" will be coming when you reload the page. That means during first time load, there will be no problem. But if you click on paging and try to do autocomplete in text box. You could face this error in autocomplete statement.

Solution :-


Normally  we used to include the Jquery files by dragging to the page. When we drag the files, the following text will be added,

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript">

The blue color text shows like the Scripts directory is placed inside one folder from the root. But its not like that. Its placed directly under the root.

So change that script include to the below one.

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript">


This will solve your problem.


Conclusion :-


This may be the small issue but kill your time like anything. You won think the problem due to this script include tag. 


Happy Coding.. :)


Friday, November 26, 2010

Assign default text in Html.TextBoxFor


To assign textbox value in MVC view, use the following syntax.

<%= Html.TextBoxFor(x => x.EmployeeName, new { @Value = "Enter Employee Name"}) %>

Here the @Value case is must.

Wednesday, October 6, 2010

MVC Cascading dropdown, Jquery Grid and Resolving circular references

1. Look at the below link by Stephen Walther to work on Cascading dropdowns

http://stephenwalther.com/blog/archive/2008/09/07/asp-net-mvc-tip-41-creating-cascading-dropdown-lists-with-ajax.aspx

You may face this circular reference error due to referring one class in another class for foreign key relation,

can use the below link to solve that problem

http://blogs.telerik.com/atanaskorchev/posts/10-01-25/resolving_circular_references_when_binding_the_mvc_grid.aspx

2. jquery provides the Grid with paging and sorting. check with the below link

http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx

Thanks to respective authors..