Showing posts with label Validations. Show all posts
Showing posts with label Validations. Show all posts
, , , , , , , , , , , ,

How to set max limt of File Upload in Asp.net

Many times I faced the same problem with all my applications, that is Max size of file to be uploaded to server.

By default, the maximum size limit of a file to be uploaded to a server using the ASP.NET FileUpload control is 4MB. You cannot upload anything that is larger than this limit.

If you wants to upload a file more the size of 4 MB which is by default, you have to make some changes in the application's web.config:

<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>

Below is the small description of the parameters attributes.

maxRequestLength - Attribute limits the file upload size for ASP.NET application. This limit can be used to prevent denial of service attacks (DOS) caused by users posting large files to the server. The size specified is in kilobytes. As mentioned earlier, the default is "4096" (4 MB). Max value is "1048576" (1 GB) for .NET Framework 1.0/1.1 and "2097151" (2 GB) for .NET Framework 2.0.

executionTimeout - Attribute indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by the application. The executionTimeout value should always be longer than the amount of time that the upload process can take.
Share:
Read More
, , , , , , , ,

How to pass server values to Javascript using Eval()

In my application I have a target to show message if user does not have permission to navigate to another page. I can do this by using server code but it will make a round trip to server and make a bad user experience so, I decided to use java script.

Following is the function of Java Script

<script language="javascript" type="text/javascript">
function func_Message(obj)
{
if (parseFloat(obj)>0)
{
return true;
}
else
{
alert("Please assign a value before viewing the details");
return false;
}
}
</script>


And following is the code of Link Button from where I am passing value to Java Script Function

<asp:LinkButton ID="lnk_Details" runat="server" Text="Details" CommandArgument='<%# Eval("festi_id") +"~" + Eval("user_id") %>'
CommandName="ViewDetails" OnClientClick='<%# "return func_Message(" + Eval("user_id") + ");"%>'></asp:LinkButton>
Share:
Read More
, , , , , ,

Disbale JIT in .net / Application level Exception handling in .net

downloadDownload Code Example

I was developing windows application for a well known company of World. This application was in C#.net 2005.

As per requirement I have to show user defined message whenever an Exception occurs in the application.

The answer is very simply write a simple Try{},Catch{} statement to handle exception.
Suppose I have a code of 1000 line unwanted code and error handling creates extra thread while executing the application. I had search on the internet and found a solution to disable the JIT debugger. It works but still it is showing the application information related to functions etc. used in the code.

Now, a major issue had risen from client, i.e. he don’t want to show JIT information to user he wants to show his own message whenever any Exception occurs.

Offf…. Now that’s the task to which needs to done. I cannot use Try{},Catch{} as this decrease the application performance. Finally I came to the solution. I had wired an event with application to handle application error. Here is the code.

static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show("Illegal operation or command.", "Illegal operation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
Now, run your code and click on the button, it will show are exception while you are running the application within the .net environment.

Now browser the directory and Run you EXE file and again click in the button, now it will not show exception rather show the message which you wants to display.

This is a good approach it will show exception and source in while debugging and show and user defined message in EXE



Share:
Read More
, , , , ,

Client side validations in C# using javascript

Client-side validation uses the same error display mechanism as server-side checking.

Validating input data on web pages is usually a function performed by the server.
The web page allows the user to enter data, and when the Submit button is pressed, the browser wraps up the data into an HTTP request and sends it to the server.

The server checks each data field to make sure it is valid, and if any problems are found, a new form along with error messages is sent back to the browser.
Wouldn't it be much more useful if problems could be detected in the browser before a server request is made? This approach would provide two primary advantages.

It would lighten the load on the server, and, more importantly, it would notify the user of a problem with a data field almost immediately after he or she entered the bad data.
This supports the truism that errors are cheapest to fix the closer the detection is to the original creation of the error.

For example, if there is a problem with a zip code field and the user is notified just after he enters the bad zip code, then he is still thinking about zip code and can easily make the correction.
If the user isn't notified until the server response comes back, he's already stopped thinking about zip code—his mind has moved on to other concerns.
This problem of context switching is especially difficult when the server returns errors for many different fields.

Now, Let me explain how to check client side validations using Java script


To test this code make a new website. Add a new page with name "Default" and Paste the code provided below.

You can also download code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Client Side Validations.</title>

<script language="javascript" type="text/javascript">
function validate()
{
if (document.getElementById("<%=txtName.ClientID%>").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
{
alert("Email id can not be blank");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
var matchArray = emailid.match(emailPat);
if (matchArray == null)
{
alert("Your email address seems incorrect. Please try again.");
document.getElementById("<%=txtEmail.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")
{
alert("Web URL can not be blank");
document.getElementById("<%=txtWebURL.ClientID %>").value="http://"
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"
var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;
var matchURL=tempURL.match(Url);
if(matchURL==null)
{
alert("Web URL does not look valid");
document.getElementById("<%=txtWebURL.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtZIP.ClientID%>").value=="")
{
alert("Zip Code is not valid");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
var digits="0123456789";
var temp;
for (var i=0;i<document.getElementById("<%=txtZIP.ClientID %>").value.length;i++)
{
temp=document.getElementById("<%=txtZIP.ClientID%>").value.substring(i,i+1);
if (digits.indexOf(temp)==-1)
{
alert("Please enter correct zip code");
document.getElementById("<%=txtZIP.ClientID%>").focus();
return false;
}
}
return true;
}
</script>

</head>
<body>
<form id="form1" runat="server">
Testing Client Side Validations.<br />
<br />
<br />
<div>
Name :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
Email :
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
Web URL :
<asp:TextBox ID="txtWebURL" runat="server"></asp:TextBox><br />
Zip :
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox><br />
<br />

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>


Run you application and try to test the validations. You will find all the validations are raised before raising the server side event if submit button
Share:
Read More