Tech blog

Solving problems

About
Contact

Archive for the ‘ASP.NET’ tag

System.Web.HttpException: Request is not available in this context

leave a comment

Use HttpContext.Current.Request instead of Request

i.e.

HttpContext.Current.Request.UserHostAddress

not

Request.UserHostAddress

VN:F [1.9.3_1094]
Rating: 2.5/5 (2 votes cast)
VN:F [1.9.3_1094]
Rating: +2 (from 2 votes)

Comparison: ASP .NET WebForms vs MVC

leave a comment

Comparison: ASP .NET WebForms vs MVC

WebForms MVC
  • Requires .NET 2.0
  • Event model – button click triggers an event in code-behind
  • ViewState – server-based forms for easier management
  • Existing third party controls
  • Requires .NET 3.5 and MVC library
  • Requires IIS 6.0 or later for URL rewritting – btw, max IIS version for WinXP is 5.1
  • Backward compatible – developer can include WebForms pages into MVC website and vice-versa
  • Dividing an application into the model, the view, and the controller – data, design and code are separated
  • For large teams of developers and designers (parallel work)
  • No ViewState means smaller output and cleaner html
  • Portable among other languages (like php) – similar file hierarchy, object names, different syntax
  • 30-50% faster than Web Forms, 100-800 requests per second
  • Native URL rewritting
  • Skinnable, suitable for websites that are viewed in computer browsers and mobile browsers
  • Unit testing
  • VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: +3 (from 3 votes)

    Written by Avivo

    January 30th, 2010 at 11:41 pm

    IF WRN: Assembly binding logging is turned OFF or It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level

    leave a comment

    We had some problems with subdirectories under our root website.

    We wanted to add different examples under our website root (organized into folders) like this:

    • www.our-website.com/example1
    • www.our-website.com/example2

    And we always got these errors.

    At root of our website www.our-website.com we didn’t have any web.config and we didn’t want any. Every subfolder has its own web.config and it didn’t worked under IIS7

    Solution:

    Click on each folder under IIS and then right mouse click Convert To Application and it worked.

    VN:F [1.9.3_1094]
    Rating: 3.0/5 (1 vote cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Saving high quality JPG in C# or ASP.NET

    leave a comment

    Just use this piece of code or read at this MSDN article:

    private void VaryQualityLevel()
    {
    	// Get a bitmap.
    	Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    	ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
    
    	// Create an Encoder object based on the GUID
    	// for the Quality parameter category.
    	System.Drawing.Imaging.Encoder myEncoder =
    	System.Drawing.Imaging.Encoder.Quality;
    
    	// Create an EncoderParameters object.
    	// An EncoderParameters object has an array of EncoderParameter
    	// objects. In this case, there is only one
    	// EncoderParameter object in the array.
    	EncoderParameters myEncoderParameters = new EncoderParameters(1);
    
    	EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters);
    
    	myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters);
    
    	// Save the bitmap as a JPG file with zero quality level compression.
    	myEncoderParameter = new EncoderParameter(myEncoder, 0L);
    	myEncoderParameters.Param[0] = myEncoderParameter;
    	bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters);
    
    }
    
    ...
    
    private ImageCodecInfo GetEncoder(ImageFormat format)
    {
    	ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
    
    	foreach (ImageCodecInfo codec in codecs)
    	{
    	if (codec.FormatID == format.Guid)
    	{
    	return codec;
    	}
    	}
    	return null;
    }
    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    November 13th, 2009 at 4:17 pm

    Check if image (url) exists in ASP.NET

    leave a comment

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("url");
    request.Method = "HEAD";
    
    bool exists;
    try
    {
        request.GetResponse();
        exists = true;
    }
    catch
    {
       exists = false;
    }
    
    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    August 14th, 2009 at 11:07 am

    Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 0

    3 comments

    If you get an error:

    Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 0

    just add this code in HEAD section of your page/masterpage:

    <script language="javascript" type="text/javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);
    function endRequest(sender, args)
    {
    	// Check to see if there's an error on this request.
    	if (args.get_error() != undefined)
    	{
    	  //$get('Error').style.visibility = "visible";
    	  // Let the framework know that the error is handled,
    	  // so it doesn't throw the JavaScript alert.
    	  args.set_errorHandled(true);
    	}
    }
    </script>
    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    June 16th, 2009 at 7:48 pm

    Storing and retrieving images in MS SQL Server

    leave a comment

    This is a tutorial from very nice Blog:

    Hi, I saw many developers are asking this questions in different forums. Recently I saw this question on MSDN forum and I thought let me write a blog on this. I know many of us found this too easy however for new bees its bit hard.

    In this article, I had used SQL Server 2005 as back end and C# as front end. SQL Server has “Image” data type to store the image. In Oracle and some other database you can use a data type which is used to store binary value (may be BLOB). I have created a simple aspx which has File upload control and a button. When user selects a file to upload, I am checking it for valid image type and converting it to array of Bytes. Then I will store that byte array into database. Below is the code,

    if (objFileUpload.PostedFile !=null)
    {
    if (objFileUpload.PostedFile.ContentLength > 0)
    {
    HttpPostedFile objHttpPostedFile = objFileUpload.PostedFile;
    if (CheckValidFileType(objHttpPostedFile.FileName))
    {
    int intContentlength = objHttpPostedFile.ContentLength;
    byte[] bytImage =new Byte[intContentlength];
    objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength);
    }
    }
    }

    Read Uploaded file (here Image) in Byte Array

    Pass this Byte array to you DAL and use it for storing image in database. I am using Enterprise Library as DAL so my code will look like,

    Database db =DatabaseFactory.CreateDatabase();
    string sqlCommand ="StoredProcedureName";
    DbCommand dbCommandWrapper = db.GetStoredProcCommand(sqlCommand);
    db.AddInParameter(dbCommandWrapper,"@Image",DbType.Binary,bytImage );
    try
    {
    db.ExecuteNonQuery(dbCommandWrapper);
    }
    catch {throw; }

    Insert Image in to database

    This is how you can store the Image in database. Retrieving the image is the same process. Write a SP which will return your image. Store this value in a Byte Array. Once you get the image in Byte array, you just have to write it on form as shown below,

    Byte[] bytImage =Byte array retrieved from database.
    if (bytImage !=null)
    {
    Response.ContentType = "image/jpeg";
    Response.Expires = 0;
    Response.Buffer =true;
    Response.Clear();
    Response.BinaryWrite(bytImage);
    Response.End();
    }

    Code to display Byte array as Image on form.

    To use this at multiple places in your application, you create a page to which you can pass ID of Image and it will retrieve image from database and create image. To do this copy paste above code in aspx.cs file. Now on every page you require to show this image take on that page and set its ImageURL property to the path of newly created user control. See the code below,

    Image control on any aspx page (Lets say Sample.aspx).

    string strURL ="~/ViewImage.aspx?ID= 1";
    ViewImage.ImageUrl = strURL;

    set Image URL for image control on code behind (Sample.aspx.cs)

    You can see the image will be displayed in your page where you had put Image tag.
    Happy Programing.

    VN:F [1.9.3_1094]
    Rating: 5.0/5 (1 vote cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    April 16th, 2009 at 9:22 am

    Silverlight 2 Plugin not working

    2 comments

    Well, maybe you will expirience a problem that Silverlight 2 plugin is not working eventhough you have the latest version 2.0.30825.0

    It is showing MIME type: application/x-silverlight-2-b2

    This can be if you have old Silverlight 2 Beta version 2.0.10125.0 in bin directory of your website/application.

    Just ovewrite it with latest version and it will be ok.

    Magic!

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    February 17th, 2009 at 9:43 pm