How to catch exception on saving foreign key field value? - asp.net-mvc-3

While trying to add value into foreign key field, if same data doesn't exist in primary key field it throws exception like "An error occured while updating entries." However, i want to catch the same exception and display it in some window with message like "Value doesn't exist, please enter different value." So how can i catch this error in asp.net MVC.

not really familiar with MVC, I use WinForms, but why not try to find out the error code you get. Just put your code in a try catch block and if an exception is thrown check if the error code matches your error code and if it does use a MessageBox or something like that to show your message.
Hope this helps

Related

Unexpected exception while validating invocable action requests

When we try to change the pricebook to add products in opportunity; we are not able to do it because of one validation rule(that is we can not edit opportunity if this opportunity does not contain opportunity products )
I wrote the validation rule:
NOT(OR(ISNEW(),IF(ISCHANGED(Pricebook2Id),true,HasOpportunityLineItem) ))
when the validation rule should trigger, it triggers and showing error message as expected, but when it should not trigger it is showing some different error( not validation rule error) like:
"Can't update Price Book. Unexpected exception while validating invocable action requests"
Can anyone suggest anything?
jhumur
I think the appropriate object should be PricebookEntry rather than Pricebook2.

In spring batch, How can pass parameter from ItemWriter to SkipListener?

In my spring batch job, if while itemWriter is writing an error occurred, it is skipped. And ItemSkipListener received it and log item to file.
My item has one list data and i want know what element in list make error occurred.
But in ItemSkipListener, i can only know item and throwable. (cannot know what element in list)
So i think itemWriter pass list index to ItemSkipListener.
How can i do that?
thanks.
I found the solution my self.
When exception occurred in writer, catch the exception and make new exception with index of element and etc. and throw it. then in skipListener receive the throwable and can get the data.

Add a global error in Spring MVC from a non-controller location

Somewhere in my Spring MVC app I need to add a global error. I no longer have access to any BindingResult, I only have my Request. My error is not the result of any binding issues, it's a global condition. Then I need to attach my new Errors object to the "errors" Req. attribute, so the JSP can display it.
But how to do this? I can't do
Errors errors = new Errors();
errors.reject(....);
request.setAttribute("errors", errors);
because Errors is an interface. The implementations all have to do with 'Binding',like BindException, AbstractBindingResult, MapBindingResult, etc. These all have nothing to do with my case, I don't have a binding field error, it's a general error msg.
Any tips?
Our resolution to this problem: The proper way to handle EXCEPTIONS (rather than VALIDATION errors) is to keep track of our own, custom Request Attribute that we display ourselves in our error area on the JSP.
BindingResult/Errors/etc. are all Binding-related, we can't hack it to handle general exception errors.
We just add a custom Request Attr. called "exceptions" and then our Error section checks for this additonal attribute list to display.
SpringMVC doesn't provide its own 'generic' Error collection.

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

I am developing an MVC5 internet application and have a question in regards to user input having HTML data.
I understand that if I want to have HTML code in a model, I can include the [AllowHtml] data annotation and then sanitize the objects field.
My question is this, for any object field that does not have the [AllowHtml] data annotation, where the user enters some HTML code, is it possible to cater to this error rather than have the Error.cshtml display the error?
Ideally, I would like to display a validation message in the view before the Error.cshtml displays and logs the error.
Is this possible? How can I cater to the error before the Error.cshtml displays and logs the error.
Thanks in advance.
UPDATE
I have a function as follows in the Global.asax file:
protected void Application_Error(object sender, EventArgs e)
This function catches my errors such as when the user goes to a page that does not exist, however, the http error in question goes directly to the error.cshtml file.
How can I edit my code so that the Application_Error function catches this error?
I am using Elmah for logging and have customErrors mode="On"
It's not that easy to write a validator that checks if a textbox doesn't contain HTML. This is because HTML is not defined by certain characters, but instead by a combination of them. A text containing <, '>' or even <script> isn't necessarily HTML.
You should take the approach of the allowed values. If a textbox should contain only number, then validate it like so.
By overriding Application_Error in Global.asax you can catch this exception and redirect the user to a more meaningful error page
protected void Application_Error()
{
Exception lastError = Server.GetLastError();
if (lastError is HttpRequestValidationException)
{
//redirect to a static page and show proper error message
}
}
If you're using Elmah things are even simpler. Elmah is designed to work with ASP.Net error handling.
You need to remove the default global HandleErrorAttribute from App_Start\FilterConfig (or Global.asax), and then set up an error page in your Web.config:
<customErrors mode="On" defaultRedirect="~/error/" />
In case you run into trouble please check this article, it explains everything very well
http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx

How to get MessageBuilder status in Protocol Buffers?

When using Message.Builder.build() an exception is thrown when a required field is not set. Is there way to find out if the exception will be thrown? i.e. something like an iSReadyToBuild? There is a buildPartial method but it does not say whether the build was complete or partial.
The method you are looking for is called "isInitialized()".

Resources