Database Exception Handling

by Admin 4. September 2012 06:40

 

The structure for handling sql exceptions will be as per the diagram below

 

                                            

 Database (SQL) exception handling approach

This part covers the exception handling from the stored procedures

 

Here are the steps of how we are going to implement exception handling in our IPF database

 

1.     Create a table named ErrorTracer. This table holds all the logged error details.

 

Here is the schema for this table

CREATE TABLE ErrorTracer

(

  iErrorID INT PRIMARY KEY IDENTITY(1,1),

  vErrorNumber INT,

  vErrorState INT,

  vErrorSeverity INT,

  vErrorLine INT,

  vErrorProc VARCHAR(MAX),

  vErrorMsg VARCHAR(MAX),

  vUserName VARCHAR(MAX),

  vHostName VARCHAR(MAX),

  dErrorDate DATETIME DEFAULT GETDATE()

)

 2.     Now we have got a stored procedure named usp_LogErrorDetails to handle / log all the SQL errors.

 

3.     The script for the table creation and the stored procedure is embedded below.

Double click to view the file contents

ExceptionHandling.sql (2.46 kb)

 

4.      A sample procedure named usp_ExceptionHandlingSample to validate the stored procedure script is embedded below.

Double click to view the file contents

ExceptionHandlingSample.sql (819.00 bytes)


 5.     The sample procedure pseudo code is given below.

BEGIN TRY

      SELECT 1/0

END TRY

BEGIN CATCH

     EXEC usp_LogErrorDetails

END CATCH

 

6.     This basically raises a divide by zero error which is captured by our usp_LogErrorDetails procedure, which internally inserts a record with all the error details in the ErrorTracer table

 

 

7. The error details from the ErrorTracer table for this sample would be as below

Column Name

Value logged

iErrorID

1

vErrorNumber

8134

vErrorState

1

vErrorSeverity

16

vErrorLine

15

vErrorProc

usp_ExceptionHandlingSample

vErrorMsg

Divide by zero error encountered.

vUserName

CORPORATE\VenkatRangala

vHostName

UKLON1BL0003

dErrorDate

51:25.9

 

 

 

 

 

 

 

 

  Hope this helps!

 

Tags:

SQL Server

MVC4 MVC3 side by side installation error

by Admin 15. July 2012 11:56

If you install MVC4 on an existing MVC3 s/w . The MVC3 applications will not compile and end up with the following error:

The type 'System.Web.Mvc.ModelClientValidationRule' exists in both 'c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll' and 'c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.WebPages.dll' 

It would appear this has been moved to a different assembly and since both assemblies are in the GAC it does not know which to use.

FIX 

Edit the csproj:

ProjectName.csproj

Change the following 

<Reference Include="System.Web.WebPages"/> 

to

<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL "/>

Hope this helps!

Tags:

ASP.NET MVC

Internet Information Services (IIS) 7.5 Express Download

by Admin 28. June 2012 03:15

Internet Information Services (IIS) 7.5 Express Download Here

Tags:

IIS

Text in C# Razor syntax

by Admin 27. June 2012 03:12

Text in C# Razor syntax

Eg:

@foreach(var item in Model)

{

<text> here is the sample text </text>

Other option is 

@: here is the sample text 

}

Tags:

ASP.NET MVC

Html encoding & decoding

by Admin 27. June 2012 02:49

@item.Body --- Razor syntax has got html encode by default.

If you want to display the raw text without encoding use 

@Html.Raw(item.Body)

Tags:

ASP.NET MVC

MVC Call a controller Action from another controller action

by Admin 15. June 2012 02:07

 

Use Controller.RedirectToAction

Tags:

ASP.NET MVC

Repository Pattern benefits in MVC3

by Admin 19. May 2012 00:11

The repository pattern has the following benefits:

  • It provides a substitution point for the unit tests. You can easily test business logic without a database and other external dependencies.

  • Duplicated queries and data-access patterns can be removed and refactored into the repository.

  • Controller methods can use strongly typed parameters, which means the compiler finds data-typing errors each time you compile instead of relying of finding data-typing errors at run time when testing.

  • Data access is centralized, which provides the following benefits:

    • Greater separation of concerns (SoC), another tenet of MVC, which increases maintainability and readability.

    • Simplified implementation of centralized data caching.

    • A more flexible and less coupled architecture that can be adapted as the overall design of the application evolves.

  • Behavior can be associated with related data. For example, you can calculate fields or enforce complex relationships or business rules between the data elements within an entity.

  • A domain model can be applied in order to simplify complex business logic.

Using the repository pattern with MVC and TDD typically requires you to create an interface for your data-access class. The repository interface will make it easy to inject a mock repository when you unit test our controller methods.

Source: MSDN

Tags:

ASP.NET MVC

Lazy, Eager, and Explicit Loading of Related Data

by Admin 27. April 2012 19:16

Lazy, Eager, and Explicit Loading of Related Data

There are several ways that the Entity Framework can load related data into the navigation properties of an entity:

  • Lazy loading. When the entity is first read, related data isn't retrieved. However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. This results in multiple queries sent to the database — one for the entity itself and one each time that related data for the entity must be retrieved.

    Lazy_loading_example

  • Eager loading. When the entity is read, related data is retrieved along with it. This typically results in a single join query that retrieves all of the data that's needed. You specify eager loading by using the Include method.

    Eager_loading_example

  • Explicit loading. This is similar to lazy loading, except that you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load method for collections or the Reference.Load method for properties that hold a single entity. (In the following example, if you wanted to load the Administrator navigation property, you'd replace Collection(x => x.Courses) withReference(x => x.Administrator).)

    Explicit_loading_example

Because they don't immediately retrieve the property values, lazy loading and explicit loading are also both known as deferred loading.

In general, if you know you need related data for every entity retrieved, eager loading offers the best performance, because a single query sent to the database is typically more efficient than separate queries for each entity retrieved. For example, in the above examples, suppose that each department has ten related courses. The eager loading example would result in just a single (join) query. The lazy loading and explicit loading examples would both result in eleven queries.

On the other hand, if you need to access an entity's navigation properties only infrequently or only for a small portion of a set of entities you're processing, lazy loading may be more efficient, because eager loading would retrieve more data than you need. Typically you'd use explicit loading only when you've turned lazy loading off. One scenario when you might turn lazy loading off is during serialization, when you know you don't need all navigation properties loaded. If lazy loading were on, all navigation properties would all be loaded automatically, because serialization accesses all properties.

The database context class performs lazy loading by default. There are two ways to turn off lazy loading:

  • For specific navigation properties, omit the virtual keyword when you declare the property.
  • For all navigation properties, set LazyLoadingEnabled to false.

Lazy loading can mask code that causes performance problems. For example, code that doesn't specify eager or explicit loading but processes a high volume of entities and uses several navigation properties in each iteration might be very inefficient (because of many round trips to the database), but it would work without errors if it relies on lazy loading. Temporarily disabling lazy loading is one way to discover where the code is relying on lazy loading, because without it the navigation properties will be null and the code will fail.

Taken from ASP.NET SITE

Tags:

ASP.NET MVC

Dependency Injection

by Admin 23. April 2012 09:27

Dependency Injection (alias names Inversion of Control / IOC) is a design patern that helps removing tight coupling between dependant components.

It provides the flexibility to design and implement loosely coupled, reusable and testable objects. It also offers runtime loading and initialization of components, which makes solutions load on demand.

Tags:

ASP.NET MVC

Replace a text in SQL Server Table Column

by Admin 3. April 2012 13:33
A simple replace text in a column
UPDATE [TableName]
SET [ColumnName] = Replace([ColumnName],'OldText','NewText')

Tags:

SQL Server

The launch of the uk replica watches new timepiece series undoubtedly symbolizes the replica watches history of the brand, and the classic hublot replica watches design explains the mission of the replica watches uk brand's precision watchmaking.

TextBox

Tag cloud

    RecentPosts