Catch errors when updating a DetailsView control

S

stuart.d.jones

Hi,

I have a DetailsView control bound to a SqlDataSource. I've got
everything working nearly as I want, but here's my problem.

I have a table which has a Guid as a Primary Key, and a VarChar field
(a Title) which is also unique. If the user edits the Title field in
the DetailsView to make it a duplicate, and hits Update, an exception
is thrown (SQLException - statement cancelled as it would violate the
key restrictions).

I was under the impression that regardless of the outcome, the
ItemUpdated event of the DetailsView control would fire, and I could
then check the Exception properties of the event arguments, and act
accordingly.

How can I catch these errors - I have no control over the code where
the error's occurring, so I can't put a try-catch block in? Can I do
this, or do I have to faff around making sure they don't happen before
it tries to save the changes? And if I can't do this, when do the
Exception properties of the ItemUpdated event arguments ever get
used...???

Any help much appreciated!

Cheers.

Stuart
 
M

Mr Newbie

You need to post more accurate information rather than have us Guess what
your table design is. It sounds to me like the GUID in the current record is
being used as the PK in the duplicate, this is not allowed.

Post your data description for this table and relevent code.
 
S

stuart.d.jones

It isn't anything to do with the Guid. The Title field is also a key.
If I change the title field to a value which is also in the table, then
it throws the error below (I couldn't get the description last night,
as I was at home). How can I catch this error, or get the DetailsView
or SqlDataSource to catch it for me, and keep it so I can check on it?
As far as I'm concerned that's all I need to tell you - I don't want to
confuse the issue by pasting the full schema of a massive table when I
know precisely what's going wrong - I just want to catch it! And as
for what code is being executed - there isn't any, it's all handled by
the DetailsView! Hence my puzzlement.

Violation of UNIQUE KEY constraint 'IX_TableName_1'. Cannot insert
duplicate key in object 'dbo.TableName.
The statement has been terminated.

This key is the one referring to the Title field.

Stack trace follows, in case you need it:
[SqlException (0x80131904): Violation of UNIQUE KEY constraint
'IX_Courses_1'. Cannot insert duplicate key in object 'dbo.Courses'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection) +177
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection) +68

System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj) +199
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
+2300
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader
ds, RunBehavior runBehavior, String resetOptionsString) +147
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean
async) +1021
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
method, DbAsyncResult result) +314

System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) +413
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +115

System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand
command, DataSourceOperation operation) +392

System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary
keys, IDictionary values, IDictionary oldValues) +643
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary
values, IDictionary oldValues, DataSourceViewOperationCallback
callback) +78
System.Web.UI.WebControls.DetailsView.HandleUpdate(String
commandArg, Boolean causesValidation) +1153
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e,
Boolean causesValidation, String validationGroup) +450
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source,
EventArgs e) +88
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +35
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object
source, EventArgs e) +109
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs
args) +35
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +86
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +155

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+4921
 
S

stuart.d.jones

Fixed it.

Setting e.ExceptionHandled = True rather than False seems to work just
fine. D'oh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top