0

I'm using a gridview and a SqlDataSource.

In the database, I've set a unique constraint on one of the columns.

When I'm changing the value of a cell from a row in the gridview with a new value but the value already exists in other row in the same column it gives me the error :

Cannot insert duplicate key row in object 'dbo.tb1' with unique index 'IX_tb1'.
The statement has been terminated.

I need to show a friendly message to the user for example in a label

Error: value already exists

Is there any way of doing this? Because I'm not using any sql command to catch something.

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jax
  • 977
  • 8
  • 22
  • 40

2 Answers2

0

if you coding in c# then handle error in catch block and show custom error like bellow:

try
{
  // Statement which can cause an exception.
}
catch(Type x)
{
  // Statements for handling the exception
  //show your custom error by assigning to label or other control
}
finally
{
  //Any cleanup code
} 

and if you you want to handle error in sql query then use error handling method like bellow:

BEGIN TRY
   SELECT convert(smallint, '2003121')
END TRY
BEGIN CATCH
   PRINT 'errno: ' + ltrim(str(error_number()))
   PRINT 'errmsg: ' + error_message()
END CATCH
matt
  • 78,533
  • 8
  • 163
  • 197
R.D.
  • 7,153
  • 7
  • 22
  • 26
0

Take a look at this answer. You need to use an event of the sqldatasource in order to handle the exception to show something friendly to the user.

Community
  • 1
  • 1