Tuesday, July 7, 2009

How to remove error "'Sys' is undefined" ASP.NET

When anyone create a normal "ASP.NET Web Site" and not an "ASP.NET Ajax Enabled Web Site" and try to use AJAX Controls this error occurs.

There are two key tags, that must exist on web.config:

under <assemblies> (possibly you already have this one)
<add assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

under <httpHandlers> (this is the new one)
<add verb="GET" path="ScriptResource.axd" Type="Microsoft.Web.Handlers.ScriptResourceHandler" validate="false"/>

If you use AJAX controls always try to create AJAX Enabled website.

If you already created Normal website and wish to use AJAX Controls you can copy following tag alongwith codes from WebConfig file from any AJAX Enabled website.

Copy assembly under assemblies tag;
copy httpHandlers tag and code under it; and
copy code under httpModules alongwith httpModules tag.



Tuesday, May 5, 2009

Where does cookies are stored in local Hard drive ?



This is one of the interesting things to find out the cookies in your local drive. First of all, From "Explorer Folder Option ", Select, show hidden files and folder.


Fig 1.2 : Show Hidden files and Folder Settings


Now Browse into document & settings of the current user and open the cookies folder. Now looks the picture.

Fig 1.3 : Reading Cooking info in local System

Friday, May 1, 2009

How to Read data from cookies ?

Now , its times to retrieve data from cookies. Ok, before reading cookies, first of all we need to check whether a cookies was found or not. "Its always good practice to check cookie before read it, because is browser is disable cookies.



How to create cookies ?

For working with cookies we need to use namespace System.web














Now , have a look, on the code , that how can we create a cookies and add it with web response .3










The cookies which has been created will persist , until browser has been closed. we can persist the cookies. But how? Just after few point I have discussed it.

Advantages & Disadvantages of of Cookies

Advantages
Following are main advantages of using cookies in web application:
  • It's very simple to use and implement.
  • Browser's taking care send data.
  • For multiple sites cookies, Browser automatically arranges them.
Main disadvantages of cookies are:
  • Its store data in a simple text format. so it's not secure at all.
  • There is a size limit of cookies data ( 4096 bytes / 4KB).
  • Number if cookies also limited. Most Browser provides limits of storing cookies is 20. If new cookies came, it will discard the old one. Some of browser support up to 300.
  • We need to configure browser. It will not work on a high security configuration of browser. [I have explained about this in details.]

How Cookies are started ?



When client request to the server, server send the cookies into client . The same cookies can be referred for subsequent request. As for example, if codeproject.com stores session id as a cookies, when any client hits first times on the server, server generates the session id and send it as a cookies to client. [As given in Fig 1.0]




Fig 1.0 : Initial state of cookie creation






Now for all other subsequent from the same client it uses the session-id from cookies, just like the picture below:




Fig 1.1 : Subsequent request for other pages

Browser and web server are responsible for exchange cookies information. For different sites, browser keeps cookies differently. If any pages need information from cookies, when that URL is being hit, first its search for local system for cookies information then its moved to server with that information.




What are Cookies ?

Cookies are the small files that are created on the client's system or client browser memory (if temporary). Its use for State management that I have already discuss on my view state article. So we can store small piece of information in a client system and we can use it when we needed. Most interesting thing is that Its works transparently with the user. It can be easily used any where of you web application. Cookies store information in a plain text format. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences , Password Remember Option etc. Cookies has many advantages and disadvantages. I will comes to this points , but first have a look how cookies are started.

Thursday, April 30, 2009

FileUpload control in ASP.NET web form using c#

FileUpload is very simple control allows user to select file and upload.
You can easily handle that with very little coding and ease. I have given the full code behind for this functionality.
First create a aspx page with 3 controls
FileUpload: rename it to FileUpload1
Label: rename it to lblMsg
Button: rename it to btnAdd
Copy this code in your code behind file inside the class declaration. As you can see we are simply calling theSaveUploadedFile() function to save the uploaded file. you can check for the particular extension within the function right now we are checking for only 3 extensions. You can change it in sAllowedExt string.
Here is the code behind stuff:

String UPLOAD_PATH = "";
int MAXSIZE = 5 * 1024 * 1024; // max 5 MB
String sAllowedExt = ",.doc,.pdf,.xls,"; // make sure whatever extension you enter, string ends with comma
protected void Page_Load(object sender, EventArgs e)
{
btnUpload.Attributes.Add("onclick", "return checkFile();");
UPLOAD_PATH =
Server.MapPath("~/UploadedDocs") + "/";
}
private void setMsg(String sMsg)
{
lblMsg.Text = sMsg;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
try
{
SaveUploadedFile();
}
catch (Exception ex)
{
//Response.Write("Error: " + ex.Message);
setMsg("File could not be uploaded successfully.");
}
}

private void SaveUploadedFile()
{
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String validExt = ;
if (validExt.IndexOf("," + fileExtension + ",") != -1)
{
if (FileUpload1.FileBytes.Length <= MAXSIZE)
{
FileUpload1.PostedFile.SaveAs(UPLOAD_PATH + FileUpload1.FileName);
setMsg("File uploaded successfully.");
}
else
{
setMsg("File size exceeded the permitted limit.");
}
}
else
{
setMsg("This file type is not accepted.");
}
}
}
Once the user uploads the file, it's availabe using FileUpload.PostedFile.SaveAs() method. You can then directly save that file to particular folder with new name. We are checking FileUpload1.HasFileto make sure we only process if a file is uploaded.

Wednesday, April 29, 2009

ASP.NET event firing twice

First Solution:
This seems to be a very common problem where by your event handler method gets run twice.
It is caused by VS.NET inserting 2 wireup of the event handler:
  • once in the aspx (HTML) eg:
  • and once in the VS.NET generated section (InitializeComponent) eg:
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);

The easiest solution, imo, is to simply remove the "OnClick..." HTML markup from the .aspx page.
e.g: change this: < asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" >
To this: < asp:Button ID="btnTest" runat="server" Text="Test" >
Rebuild, and bingo! - 1 event per click.

Second Solution:

I had the same problem, but none of the solutions above solved my problem..I was using HTML controls on my aspx page like < button runat="server" id="btnNext" type="submit" onserverclick="btnNext_Click" > The solution for me was to change the type="submit" to type="button".

Large file uploads in ASP.NET

Uploading files via the FileUpload control gets tricky with big files. The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded."

Increasing the Maximum Upload Size

The 4MB default is set in machine.config, but you can override it in you web.config. For instance, to expand the upload limit to 20MB, you'd do this:

<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480"/>
</system.web>

Since the maximum request size limit is there to protect your site, it's best to expand the file-size limit for specific directories rather than your entire application. That's possible since the web.config allows for cascading overrides. You can add a web.config file to your folder which just contains the above, or you can use the tag in your main web.config to achieve the same effect:

<location path="Upload">
<system.web>
<httpRuntime executionTimeout="110" maxRequestLength="20000" />
</system.web>
</location>
Heaven - it is ok but try to earn money at hellishdollars.com !