none
ASP.NET Update RRS feed

  • Question

  • I am using the update command with a stored procedure in a formview to update user records. In the past I have handled the update in the code behind so I could post a message on the form depending on whether or not the update succeeded. I use a try catch for that. How do you catch success or fail when the update is accomplished through the SQlDataSource?
    Friday, November 12, 2010 11:52 AM

Answers

  • Hi Dave,

    Do not use exceptions to signal success or failure. This is a common mistake and anti-pattern. You should only use exceptions when something "exceptional" has occurred. 

    Do use an output parameter signalling success or failure. Using the SQLDataSource is OK for simple scenario's; I would steer away from it most of the time because it's much more obvious to the reader of your code what happens if you keep everything contained in code behind.

    Then again, if you want to persist in using the SQLdatasource, I believe it's possible to use an output parameter, and read it's value on the "updated" event. Have a look at the available events here.

    Kind regards,
    Tom de Koning


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Tom de Koning Sunday, November 14, 2010 10:11 AM
    • Marked as answer by eryang Tuesday, November 23, 2010 5:24 AM
    Sunday, November 14, 2010 10:10 AM

All replies

  • Hi Dave,

    I am not sure, if I understood your requirements correctly.

    Inside a stored procedure, you can raise an error if required. And these errors will be an exception in your code, so you can use exceptions.

    Exceptions are time consuming, so another solution can be to simply return the number of changed records from the stored procedure. Then you simply call the stored procedure and check that the number of changed records is not 0.

    I hope I was able to help with these general statements. Feel free to clarify if you need more information about these topics or I didn't understand your problem at all.

    With kind regards,

    Konrad

    Friday, November 12, 2010 12:18 PM
  • Hi Dave,

    you may also consider asking the question at http://forums.asp.net/  for quicker and better support.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Saturday, November 13, 2010 3:44 AM
  • Hi Dave,

    Do not use exceptions to signal success or failure. This is a common mistake and anti-pattern. You should only use exceptions when something "exceptional" has occurred. 

    Do use an output parameter signalling success or failure. Using the SQLDataSource is OK for simple scenario's; I would steer away from it most of the time because it's much more obvious to the reader of your code what happens if you keep everything contained in code behind.

    Then again, if you want to persist in using the SQLdatasource, I believe it's possible to use an output parameter, and read it's value on the "updated" event. Have a look at the available events here.

    Kind regards,
    Tom de Koning


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Tom de Koning Sunday, November 14, 2010 10:11 AM
    • Marked as answer by eryang Tuesday, November 23, 2010 5:24 AM
    Sunday, November 14, 2010 10:10 AM