Catch errors when updating a DetailsView control

Discussion in 'ASP .Net' started by stuart.d.jones@gmail.com, Jan 25, 2006.

  1. Guest

    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
     
    , Jan 25, 2006
    #1
    1. Advertising

  2. Mr Newbie Guest

    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.

    --
    Best Regards

    The Inimitable Mr Newbie º¿º


    <> wrote in message
    news:...
    > 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
    >
     
    Mr Newbie, Jan 25, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Jan 26, 2006
    #3
  4. Guest

    Fixed it.

    Setting e.ExceptionHandled = True rather than False seems to work just
    fine. D'oh!
     
    , Jan 26, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    985
    Mark Goldin
    Jan 17, 2004
  2. Roger
    Replies:
    1
    Views:
    472
    joeka
    Nov 17, 2005
  3. ashishbuddha

    updating in detailsview

    ashishbuddha, Jul 18, 2006, in forum: .NET
    Replies:
    0
    Views:
    2,273
    ashishbuddha
    Jul 18, 2006
  4. DotNetter
    Replies:
    0
    Views:
    477
    DotNetter
    Jun 21, 2006
  5. Feege
    Replies:
    0
    Views:
    485
    Feege
    Dec 20, 2005
Loading...

Share This Page