Saturday, 17 December 2016

A potentially dangerous Request.Form value was detected from the client

Cause of Error

.NET detected something in entered text which looked like an html statement. ASP.Net By default validates all input controls for potentially unsafe contents that can lead to Cross Site Scripting and SQL Injections. So ASP.Net is not allowing this content so it's blocking by this exception. By default it is recommended to allow this check to happen on each postback.

Solution
In some cases as per your requirement you can avoid this exception as below
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest = "false"

it will disable the request validation for current page. if you want to disable for whole application then you can put in web.config as below in system.Web section.
<pages validateRequest ="false" />


For .Net 4.0 or higher versions write below code under System.Web section.

<httpRuntime requestValidationMode = "2.0" />


Not all code paths return a value

If you return any thing from method instead of void then you must return the defined type value as described in return type.

Case 1. If you are using for , while loop or any other conditional statement then you should use return statement in all places like below.

public static bool isTwenty(int num)
        {
            for (int j = 1; j <= 20; j++)
            {
                if (num % j != 0)
                {
                    return false;
                }
                else if (num % j == 0 && num == 20)
                {
                    return true;
                }
            }
            return false;

        }

Case 2. If you are using Try Catch then Give return or throw in both try and catch Or give in finally only as below.

put return or throw in Try and catch both


public int display() {
            int x = 0;
            int y = 10;
            try
            {
                int z = y / x;
                return 0;
            }

            catch (DivideByZeroException)
            {
                Console.WriteLine("DivideByZeroException");
                //throw;
                return 1;
            }
            catch (Exception)
            {
                Console.WriteLine("general exception");
                //throw;
                return 2;
            }
            finally
            {
                Console.WriteLine("finally");
               // throw new Exception("bhanu");

            }

Put return or throw in finally only as below

public int display() {
            int x = 0;
            int y = 10;
            try
            {
                int z = y / x;
                //return 0;
            }

            catch (DivideByZeroException)
            {
                Console.WriteLine("DivideByZeroException");
                //throw;
                //return 1;
            }
            catch (Exception)
            {
                Console.WriteLine("general exception");
                //throw;
                //return 2;
            }
            finally
            {
                Console.WriteLine("finally");
                throw new Exception("bhanu");
            }
        }

Friday, 16 December 2016

If you get 2 calls in WCF for any method from jquery or client side.

Solution - 
When you get 2 calls in WCF service that means there is serialization problem in your code. it may be in Data Member or some other issue related to Serialization.

Failed to create Designer from any web Server Control

Solution - For this type of problem you should use same assembly version number in web.config file and add same version assembly in add reference  and use same version assembly in aspx page

<%@ Register assembly="Infragistics4.Web.v13.1, Version=13.1.20131.2157, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.Web.UI.GridControls" tagprefix="igtbl" %>

then version number defined above  13.1.20131.2157 should be same at below three places.

Web.config File
Add Reference
Aspx Page

Route 'controller/action' threw Exception System.InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

Solution

Set the MaxJsonLength property in web.config or controller as below.
<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2147483644"/>
    </webServices>
  </scripting>
</system.web.extensions>
If it does not work then try to write like below.
public ActionResult ControllerAction()
{
  var jsonResult = Json(veryLargeCollection, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}

If with above code still you are getting this error then use NewtonSoft.Json code.
public JsonResult ControllerAction()
        {
            var data= service.LoadApplicationUserEntitlements();
            var searializeddata = JsonConvert.SerializeObject(data);
            return Json(new { Success = true, Data = searializeddata }, JsonRequestBehavior.AllowGet);
        }

and in Script file use JSON.Parse method to get back object data as below.


In success function of call use

var data = JSON.Parse(data.Data);