i
Web Application Fundamentals
Using Internet Information Services
Web Forms
ASP.NET MVC
Web Services
ASP.NET Features
Request/Response Programming
HttpRequest Class
HTTP Collections
HttpResponse Class
Redirection
HttpUtility Class
Using Visual Web Developer
Visual Studio Forms Designer
Using Components
Shadow Copying
Using the Global.asax File
Data Binding
Different Types Of Caching
How to use Object cache
Optimizing Your ASP.NET Application
What Is Caching [Storage]
Forms Authentication
Windows Authentication
ASP.NET Security Fundamentals
Data Source Controls
SqlDataSource
SiteMapDataSource
The AccessDataSource Control
ObjectDataSource
Connection String Storage
GridView
DetailsView
FormView
ListView
DataPager
Entity Data Model
EntityDataSource
Remote Method Calls
Ajax Control Toolkit
Ajax Client Library
Rich Client Applications
Ajax [Asynchronous JavaScript and XML]
ScriptManager
UpdatePanel
Model-View-Controller Pattern
ASP.NET MVC versus Web Forms
ASP.NET MVC Projects in Visual Studio
Action Methods in MVC
Routing in MVC
Strongly-Typed Views [Add images here from the visual studio]
Model Binding
Validation
ADO.NET [ActiveX Data Object]
ASP.NET Data Providers
Connections
Commands
DataReaders and Connected Access
DataSets and Disconnected Access
What happens if you are doing some important transactions and suddenly our application breaks, then it will be a very bad impression to the end-user. Our application should work smoothly in any situation. If any transaction fails, then there should be the proper way to handle this, and more information should be logged in the log file. At the same time, the user needs to see some custom message that the last operation is not completed or failed.
This handling the error should be consistent for great user experience as well as for the important transaction. ASP.NET has provided a great feature to handle that kind of situation. "Exception" class is the base class of every exception.
Exception Handling can be done in one of the levels given below:
Application-level
Page-level
Code-level
Application Level Error Handling:
We can handle default errors at the application level either by modifying our application's web.config or by modifying the Application_Error handler in the Global.asax file.
Modifing web.config file:
Modifing Global.asax file: void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError();
if (ex is HttpUnhandledException) { Server.Transfer("LogError.aspx?handler=Application_Error%20-%20Global.asax", true); } }
|
Page-Level Error Event Handling:
page-level error handler helps us to log the unhandled exception. We can also take the user to a page that can display helpful information.
private void Page_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError();
// Handle specific exception. if (ex is HttpUnhandledException) { ErrorMsgTextBox.Text = "" } // Clear the error from the server. Server.ClearError(); }
|
Code Level Error Handling:
We can achieve this by using a try-catch-finally block. We can do this for each and every function of the application. This will help us to identify the specific exception because, in the exception stack trace, we can have all the details regarding the error. Details like file name, function name, line number, etc.
Syntax:
try { // code that may raise exceptions } catch(Exception ex) { // handle exception } finally { // final cleanup code }
|
Example :
try { using (StreamReader sr = File.OpenText("details.txt")) { Console.WriteLine($"Read this file is {sr.ReadLine()}"); } } catch (FileNotFoundException ex) { Console.WriteLine($"The file was not found: '{ex}'"); } finally { if (sr != null) { sr.Close(); } }
|
In the above example, if the file is not found, then the application will throw the exception, then that can be achieved by the catch block. As soon as code throws the exception, the execution stops that point only, and the catch block starts execution.
Finally, the block is used to release the managed resources in both scenarios. Both the scenario means it will execute when there is no exception, and when there is an exception.
Don't miss out!