jQuery.validate – Setting default ignore rules

This post has been moved here.

Advertisements

JavaScript Errors Properties & Throwing Exceptions

Quick bit of information on JavaScript errors and throwing errors. (This post uses error and exception as synonyms… not sure what the correct term is in JavaScript)

I was trying to figure out how to get details from an exception thrown in JavaScript and it seems the structure is like this:

{
  message : “the error message”,
  fileName : “file:///c:/User/me/Desktop/Stuff.js”,
  lineNumber : 57,
  stack : “bunch of stuff here”
}

(Used json2.js to stringify the error message as Firebug didn’t seem to be showing it… ?)

When you throw your own exceptions you can of course throw anything including a string like this:

throw “This is a string”;

… and when you analyse what you caught it will be a simple string with that value.

try {
    throw "This is a string";
} catch(error) {
    alert(error);
}

The result will be an alert box with the value "This is a string" displayed.

Just found out here that you can do this:

throw new Error(reason);

Type of conditional expression cannot be determined because there is no implicit conversion between ” and ‘System.DateTime’

I had the error contained in the title where I was trying to assign a DateTime to an object property that could be nullable. If the value is not null I had to parse the value as a DateTime… The code is as following:

results.Add(new MyObject() {
    ID = new Guid(row["MyObjectID"].ToString()),
    Title = row["Title"].ToString(),
    DateDisabled = row.IsNull("DateDisabled") ? null

     : DateTime.Parse(row["DateDisabled"].ToString()),
});

The easiest way to solve this is as follows:

results.Add(new MyObject() {
    ID = new Guid(row["MyObjectID"].ToString()),
    Title = row["Title"].ToString(),
    DateDisabled = row.IsNull("DateDisabled") ? (DateTime?)null
     : DateTime.Parse(row["DateDisabled"].ToString()),
});

A potentially dangerous Request.Form value was detected from the client in ASP.NET WebForms and MVC

This error turns up when you enter input into a text field that contains what looks like markup. For example:

APotentiallyDangerouseRequestFormValue

When you submit you normally would get something like this… ( which unfortunately I copied from the wrong place originally :S )

Server Error in ‘/MyApplication’ Application.


A potentially dangerous Request.Form value was detected from the client (remarks="<code></code>5678,<c…").

Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case.

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (remarks="<code></code>5678,<c…").

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (remarks="<code></code>5678,<c...").]
   System.Web.HttpRequest.ValidateString(String s, String valueName, String collectionName) +8723114
   System.Web.HttpRequest.ValidateNameValueCollection(NameValueCollection nvc, String collectionName) +111
   System.Web.HttpRequest.get_Form() +129
   System.Web.HttpRequestWrapper.get_Form() +11
   System.Web.Mvc.ValueProviderDictionary.PopulateDictionary() +145
   System.Web.Mvc.ValueProviderDictionary..ctor(ControllerContext controllerContext) +74
   System.Web.Mvc.ControllerBase.get_ValueProvider() +31
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +53
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +109
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:2.0.50727.4016; ASP.NET Version:2.0.50727.4016

 

… which is nice by default, but in some situations where you want people to be able to enter markup of some kind you want to be able to disable this. There are several ways.

WebForms – Per Page

This is a matter of adding the ValidateRequest property to the page directive per page:

<%@ Page Language="c#" … ValidateRequest="false"%>

WebForms -Globally

To turn off validation (which is not recommended unless you need to and know the consequences) is doen by editing the Web.config file’s pages element and adding the validateRequest attribute as shown below:

<system.web> 
  : 
  <pages validateRequest="false" /> 
  : 
</system.web>

MVC – Action

To prevent this error in MVC you can do it per action by applying the ValidateInputAttribute to the action method.

[AcceptVerbs(HttpVerbs.Post)]
[ValidateInput(false)]
public ActionResult EditMyEntity(string newValue)
{  :
}

Note that I also have an entry on the MVC attribute here which discusses this attribute a little more. I think it needs updating though 🙂

Microsoft Visual C++ Runtime Error, Explorer.exe

I’m running Vista Business and getting the following error only upon initial boot in the morning. If i restart it seems to be fine.

Microsoft Visual C++ Runtime Library

Runtime Error!
Program: C:\Windows\Explorer.EXE
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

There are few possibly causes that I’ve come across for this.

  • People have said that the Google Toolbar causes this issue.. (I’ve uninstalled this to see how it goes.. Update: It didn’t work for me. Might for you though!)
  • Others have said that the Free Download Manager can cause it.
  • One person said that they ran OneCare to clean the registry. See the reply at the bottom. Only a few lines long. (Therefore another option would be to try a registry cleaner)

As far as I know I’ve not got the Free Downoad Manager installed. I do have the google toolbar though.

Will try and keep this updated as I figure it out or find more possibly solutions.

“Cannot Resolve the collation conflict between” error in Sql Server 2005

I’ve come across this issue a few times. You try and compare two columns on different tables where the collation has not been defined consistently for whatever reason, and you get an error like this

Msg 468, Level 16, State 9, Line 21
Cannot resolve the collation conflict between "Latin1_General_CI_AS"
    and "SQL_Latin1_General_CP1_CI_AI" in the equal to operation.

I’ve got an example below that you can run to cause the error. Notice that the Dogs.Name column is set to a collation of “SQL_Latin1_General_CP1_CI_AI” and the OtherListOfDogs.DogName column is set to a collation of “Latin1_General_CI_AS”.

DECLARE @Dogs TABLE
(
    DogID int
    ,Age int
    ,[Name] NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AI
)

DECLARE @OtherListOfDogs TABLE
(
    DogName NVARCHAR(100) COLLATE Latin1_General_CI_AS
)

SELECT  *
FROM    @Dogs d
JOIN    @OtherListOfDogs olod
    ON  d.[Name] = olod.DogName

The two columns are compared in the select statement. The collations are different and they are incompatible, thus the error.

To allow the code to work (notice I said work, not necessarily “fix”) you can add a collate statement to one of the columns (or both) like in the following:

SELECT  *
FROM    @Dogs d
JOIN    @OtherListOfDogs olod
    ON  d.[Name] COLLATE Latin1_General_CI_AS = olod.DogName

So the Dogs.Name column will be compared to the other tables column with the same collation… And no error. :o)

Login failed for user …

I was just comparing two backups against another database. I had restored one database and compared it with Sql Compare from Redgate. All good! Then I restored another database to compare against the same database. No go….

“Login failed for user bla“.

Error ID: was 18456.

Solution:

Silly me! When i restored the second database, I had given it a name different to the first one!

Lesson:

If anything goes wrong “Check the servername, username, password, AND database name!” or in other words check all the connection string/credentials completely!!!