Monday, June 4, 2012

XHTML 1.0 Transitional "element is not supported XHTML"

While working with MVC 3 or MVC 4 application, some of your HTML tags may be showing you a document validation warning message saying "Element section is not supported", etc.

First of all, be clear that this validation errors has nothing to do with how the page will be displayed or application will be executed. They will still be shown and executed  You may be even seeing them when you just create a new ASP.NET MVC 3/ MVC 4 project using built-in template, and without writing even single line of code.

This is actually because, they are all HTML 5 tags, and your IDE is not set to use HTML 5 standards.

In order to get rid of those validation warnings, you need to set your IDE to use HTML 5 standards for document validation.
In order to do that -

Make sure your document (_Layout) page is using following: (this is default in case of MVC 3 and MVC 4)

<!DOCTYPE html>

Once done, set your IDE to use HTML 5 standards by going through Tools >> Options menu, and below screen:

 Change the target to "HTML 5" and click OK, and you will see those validation warnings are disappeared.

Create Unique Index with IGNORE_DUP_KEY ON/ OFF on SQL Server column

Unique index/ constraint in SQL Server is used to enforce uniqueness over an SQL Column. That is, if a particular column(s) is defined with UNIQUE constraint/ index, user cannot insert duplicate values in that column.

We can have only one NULL value for that column. means, not more than one row can contain NULL value in that column.

While creating a UNIQUE index, we also can specify how to handle duplicate violation. I.e. whether to throw an error, or silently ignore adding a row having a value getting duplicated for that column.

Try following example -

create table #Countries (Id int, Name varchar(20))
GO
ALTER TABLE #Countries
ADD CONSTRAINT IX_Countries
UNIQUE (Name)
WITH (IGNORE_DUP_KEY = ON)
GO
insert into #Countries (Id, Name)
select 1, 'India'
union
select 10, NULL
union
select 11, NULL
union
select 2, 'Australia'
union
select 3, 'India'
GO
select * from #Countries

drop table #Countries

This query inserts only 3 rows in the table

10    NULL
2    Australia
1    India

But it does not throw any error, because we have configured to ignore duplicate rows.

In the same query if you set IGNORE_DUP_KEY =OFF, it will give you following message:

Violation of UNIQUE KEY constraint 'IX_Countries'. Cannot insert duplicate key in object 'dbo.#Countries'.