Checking for duplicate record with FormView

When we are using FormView control often we need to check for existence of record in database at the time of insert or update.

Suppose we are having table 'CategoryMst' and we want that 'CategoryName' column should be unique.

1. Add Unique key to the column 'CategoryName' in the 'CategoryMst' table.

2. Then just catch the exception and keep the FormView in insert/ edit mode in ItemInserted/ ItemUpdated event of FormView control.

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception != null)
{
if (((SqlException)e.Exception).Number == 2627)
{
e.ExceptionHandled = true;
e.KeepInInsertMode = true;
// Display error message.
}
}
}

Comments

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out