ArgumentException: Unterminated string passed in - asp.net

I recently encountered this error in one of the pages in my site.
[ArgumentException: Unterminated string passed in. (26): {"selectedIndexes":["0"],"]
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeString()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeMemberName()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32\ depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32\ depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String\ input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer\ serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String\ input)
at Telerik.Web.UI.RadCompositeDataBoundControl.LoadPostData(String postDataKey,\ NameValueCollection postCollection)
at Telerik.Web.UI.RadCompositeDataBoundControl.System.Web.UI.IPostBackDataHandler.LoadPostData(String\ postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean\ fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
[HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown.]
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint,\ Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.pages_appeal_appealaction_aspx.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
What could be the reason for this exception ? Any ideas how to solve it ??

Yep. This error happens when the ASP.NET system receives a request, routes it to your handler and ModelBinder takes over to map values passed in to parameters in your handler. When one of those parms is a string and that string is started but not terminated, it throws the System.ArgumentException from the System.String.DeserializeString() method.
You have two options.
Fix the string data on the browser in JavaScript before you send it. (duh).
Create a custom Model Binder to handle the exception gracefully and send back a 500-series HTTP error or better yet, fix the data in a predictable way. That fixing is not always possible like in a sound file, image, movie or other base64-encoded blob.
Here's how to do #2 - Creating a custom model binder.

Related

A generic error occurred in GDI+ - On specific server only

I have the following piece of code that turns an image into a byte array to store in the database.
Public Function Image2Bytes(ByRef aImage As Drawing.Image) As Byte()
Dim MemStream As New MemoryStream()
aImage.Save(MemStream, Drawing.Imaging.ImageFormat.Png)
Return MemStream.ToArray()
End Function
This works fine on our local server as well as some production servers. However, on some shared hosting servers, this gives a GDI+ error as described below:
Error: Exception of type 'System.Web.HttpUnhandledException' was thrown. > A generic error occurred in GDI+.:
Top Stack Trace:
System.Web.UI.Page.HandleError(Exception e)
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
System.Web.UI.Page.ProcessRequest()
System.Web.UI.Page.ProcessRequest(HttpContext context)
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Inner Stack Trace:
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
Common.Image2Bytes(Image& aImage)
EntityClass.Validate()
Logo.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I've seen a lot of "Generic GDI+" errors here, but none of them solve this issue. For each solution I need to upload to a specific production server to test, as locally, there is no error.
Any ideas?

Value cannot be null. Error on opening webpage in .net

I have a web application that runs fine on a local dev server(the one that comes with ,net) but when uploaded to iis8 it comes up with a null exception. Not sure where to start.
The app is runing version 4 of the framework.
Value cannot be null.
Parameter name: type
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: type
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:
[ArgumentNullException: Value cannot be null.
Parameter name: type]
System.Activator.CreateInstance(Type type, Boolean nonPublic) +10703382
System.Activator.CreateInstance(Type type) +6
System.Web.Profile.ProfileBase.CreateMyInstance(String username, Boolean isAuthenticated) +77
System.Web.Profile.ProfileBase.Create(String username, Boolean isAuthenticated) +256
System.Web.HttpContext.get_Profile() +113
MB.ASTechnology.UI.BasePage.InitializeCulture() +36
ASP.default_aspx.__BuildControlTree(default_aspx __ctrl) +91
ASP.default_aspx.FrameworkInitialize() +44
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +48
System.Web.UI.Page.ProcessRequest() +72
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +58
ASP.default_aspx.ProcessRequest(HttpContext context) +4
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

What does a stacktrace mean when there are two of them?

I have an MVC application and it has essentially two stacktraces, im wondering why there are two of them though?..
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67
[InvalidOperationException: An error occurred when trying to create a controller of type 'PteDotNet.Web.Controllers.BlogController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +232
System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8970356
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
One is a stacktrace of an inner exception.
It looks like you first got a MissingMethodException, with reason
No parameterless constructor defined for this object.
That caused an InvalidOperationException, with reason
An error occurred when trying to create a controller of type 'PteDotNet.Web.Controllers.BlogController'. Make sure that the controller has a parameterless public constructor.
So apparently your BlogController needs a parameterless constructor.
I'am not completely sure, but It looks like the Root Cause:
The MissingMethodException is fired first, then the InvalidOperationException gets fired, because of the first Exceptin
The InvalidOperationException is the exception thrown, but it is thrown as a result of a MissingMethodException and this exception is included as the InnerException of the InvalidOperationException. The InvalidOperationException explains what went wrong but looking at the InnerException you can see where the failure was triggered in the first place.
When you evaluate ToString() on an exception with an inner exception you get stack traces of all nested inner exceptions to help you understand the problem. A common mistake is to use ex.Message + ex.StackTrace for diagnostics purposes but you loose information about inner exceptions. Unless you want to hide this valuable information you should always use ex.ToString() when logging exceptions.
In the MVC framework there is code similar to this:
try {
// Will throw MissingMethodException if there is no parameterless constructor.
var controller = Activator.CreateInstance(type);
...
}
catch (MissingMethodException ex) {
var message = String.Format("An error occurred when trying to create a controller of type '{0}'. Make sure that the controller has a parameterless public constructor.", type);
throw new InvalidOperationException(message, ex);
}
So instead of getting a MissingMethodException which might confuse you, you get an InvalidOperationException with a nice explanation. This is an example of best practice when it comes to exception handling.

Instance validation error calling web service

I'm getting an instance validation error in my proxy class i use to call a web service. This proxy class was auto-generated by XSD2Code. To me the error doesn't match the code it's pointing to.
Exception Details:
System.InvalidOperationException:
Instance validation error:
'LessThanOrEqualTo' is not a valid
value for ComparisonOperatorType.
Source Error:
Line 12866: cswgetCapabilitiesResponse csw.cswgetCapabilities(cswgetCapabilitiesRequest request)
Line 12867: {
Line 12868: return base.Channel.cswgetCapabilities(request);
Line 12869: }
Line 12870:
Source File:
d:\webapp\App_Code\ogc.csw.proxy.cs
Line: 12868
Stack Trace:
[InvalidOperationException: Instance validation error: 'LessThanOrEqualTo' is not a valid value for ComparisonOperatorType.]
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read156_ComparisonOperatorType(String s) +582
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read157_ComparisonOperatorsType(Boolean isNullable, Boolean checkType) +742
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read162_Scalar_CapabilitiesType(Boolean isNullable, Boolean checkType) +746
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read165_Filter_Capabilities(Boolean isNullable, Boolean checkType) +746
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read166_CapabilitiesType(Boolean isNullable, Boolean checkType) +1430
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadercsw.Read280_Item() +281
Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer11.Deserialize(XmlSerializationReader reader) +43
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) +579
[InvalidOperationException: There is an error in XML document (284, 13).]
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) +1375
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle) +163
System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest) +606
[CommunicationException: Error in deserializing body of reply message for operation 'cswgetCapabilities'.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +4767763
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +1725
csw.cswgetCapabilities(cswgetCapabilitiesRequest request) +0
cswClient.csw.cswgetCapabilities(cswgetCapabilitiesRequest request) in d:\webapp\App_Code\ogc.csw.proxy.cs:12868
cswClient.cswgetCapabilities(GetCapabilitiesType1 GetCapabilities) in d:\webapp\App_Code\ogc.csw.proxy.cs:12875
tests_csw.Unnamed1_Click(Object sender, EventArgs e) in d:\webapp\tests\csw.aspx.cs:22
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691
Is "LessThanOrEqualTo" meant to be a valid value? I think this message is saying that it tried to deserialize "LessThanOrEqualTo" as a value for the ComparisonOperatorType element (or attribute), but that the string "LessThanOrEqualTo" is not valid for the type of the element.

Handle "Cannot access a closed resource set"

I have a website with several languages in a database. From the database I use ResXResourceWriter to create my .resx files. This is working really good but sometimes I get this exception:
MESSAGE: Cannot access a closed resource set.
SOURCE: mscorlib
FORM:
QUERYSTRING:
TARGETSITE: System.Object GetObject(System.String, Boolean, Boolean)
STACKTRACE: at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString)
at System.Resources.RuntimeResourceSet.GetString(String key, Boolean ignoreCase)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at System.Data.Linq.DataContext.GetMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters)
at System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters)
at Business.DatabaseModelDataContext.Web_GetMostPlayedEvents(String cultureCode)
at Presentation.Default.Page_Load(Object sender, EventArgs e)
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I don't know why this is happening or how to solve it. Does anyone know anything about this?
Thanks,
Philip
I suspect you closed your DataSource prematurely or didn't close it at all when your program routine is about to end?

Resources