?
Solved

c#, ignore the error in catch (as in try catch)

Posted on 2014-11-15
3
Medium Priority
?
181 Views
Last Modified: 2014-11-15
In a list box, when I click on an item, this code puts the selected item on the screen. One of the items reads <All> which put the program in panic and produces the error below:

Question: How can I find the error number for this instance and pacify the error?
try
{
   Response.Write(lstCity.SelectedItem.Text); 
}
  catch (Exception ex)
{
  // *********find the error number when lstCity.SelectedItem.Text == <All> and pacify the error
}

Open in new window

Error message when <All> is selected:
Server Error in '/' Application.

A potentially dangerous Request.Form value was detected from the client (lstCity="<All>").

Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874. 

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (lstCity="<All>").

Source Error: 


[No relevant source lines]

Source File: c:\Users\Mike\AppData\Local\Temp\Temporary ASP.NET Files\vs\0db659aa\1e56896f\App_Web_1ohlj355.0.cs    Line: 0 

Stack Trace: 


[HttpRequestValidationException (0x80004005): A potentially dangerous Request.Form value was detected from the client (lstCity="<All>").]
   System.Web.HttpRequest.ValidateString(String value, String collectionKey, RequestValidationSource requestCollection) +9693725
   System.Web.<>c__DisplayClass5.<ValidateHttpValueCollection>b__3(String key, String value) +18
   System.Web.HttpValueCollection.EnsureKeyValidated(String key) +9693093
   System.Web.HttpValueCollection.GetValues(String name) +17
   System.Web.UI.WebControls.ListBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +51
   System.Web.UI.WebControls.ListBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +15
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +303
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6704
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +245
   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) in c:\Users\Mike\AppData\Local\Temp\Temporary ASP.NET Files\vs\0db659aa\1e56896f\App_Web_1ohlj355.0.cs:0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237

Open in new window

0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 1600 total points
ID: 40444879
You could rewrite it like this:
try
{
	Response.Write(lstCity.SelectedItem.Text); 
}
catch (HttpRequestValidationException ex)
{
	if (ex.Message.Contains("lstCity = <All>"))
		Response.Write(lstCity.SelectedItem.Text);
	else
	{
		// This is really an exception, do some logging
	}
}
catch (Exception ex)
{
	// Do something logging.
}

Open in new window


But I would recommend that you choose a different item to represent <ALL>, like -ALL-, or just All Cities.

-saige-
0
 
LVL 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 400 total points
ID: 40444892
AS suggested by Saige you need a different string to represent <ALL>, this string  is not valid because it contains "<" and ">" .
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40444902
I used -All-
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question