locked
Details View Insert RRS feed

  • Question

  • User1120668264 posted

    I am geting the follow error on my details view.  How do I trap this error before it occurs (I am using an Access database and VB).  Thanks ~

    Server Error in '/MNN2' Application.

    The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

    I need the give the user an message that say you have already created a record for this employee on this date and cancel the add. 

    Thanks again.

    Saturday, February 14, 2009 9:03 PM

Answers

  • User-821857111 posted

    Rather than let someone submit potentially duplicate values, why don't you prevent them from doing so by checking the database before they try to add?

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=99

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 17, 2009 3:00 AM
  • User187056398 posted

    why don't you prevent them from doing so by checking the database before they try to add?

    Because it is slow.

    Because between the time you check and the time you insert, someone else could insert a record and you'll still get a duplicate key violation.

    You could do the check and the insert inside of a transaction but again, that is slow and even worse, complicated.

    If you do want to do the check before insert, it would be best to do it inside of a Stored Procedure....but you would still have to handle the situation where the Stored Procedure returned an error...not that much different than catching the exception.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 17, 2009 9:34 AM

All replies

  • User187056398 posted

    Something like this:

     

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                LabelStatus.Text = "Error Inserting Record: " + e.Exception.Message;
                LabelStatus.BackColor = Color.Yellow;
                LabelStatus.ForeColor = Color.Red;
                e.ExceptionHandled = true;
            }
        }
    
     
    Sunday, February 15, 2009 10:32 AM
  • User-821857111 posted

    Rather than let someone submit potentially duplicate values, why don't you prevent them from doing so by checking the database before they try to add?

    http://www.mikesdotnetting.com/Article.aspx?ArticleID=99

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 17, 2009 3:00 AM
  • User187056398 posted

    why don't you prevent them from doing so by checking the database before they try to add?

    Because it is slow.

    Because between the time you check and the time you insert, someone else could insert a record and you'll still get a duplicate key violation.

    You could do the check and the insert inside of a transaction but again, that is slow and even worse, complicated.

    If you do want to do the check before insert, it would be best to do it inside of a Stored Procedure....but you would still have to handle the situation where the Stored Procedure returned an error...not that much different than catching the exception.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 17, 2009 9:34 AM
  • User-821857111 posted

    Because it is slow.
     

    <Cough>

    He's using Access.

    </Cough>

    Because between the time you check and the time you insert, someone else could insert a record and you'll still get a duplicate key violation.

    True, in a high transaction environment, which Access isn't suited for.

    Perhaps a combination of the suggestions provided so far would be best. 

     

    Tuesday, February 17, 2009 2:48 PM