none
try-catch RRS feed

  • Question

  • Hi all,

     

    How can use the try-catch to tell the user that the number you put already saved in database. I mean I have a windows form which contains (member_no) as primary key and save button. I want to show the user a message like " the record already exist" if the record is saved.

     

    Regards Ali,

     

    Monday, October 27, 2008 8:59 AM

All replies

  • assuming you have something like

    SqlDataAdapter adapter = ...;

    adapter.Update(dataset);

     

    You can do something like

    int dataRowCount = 0;

    SqlDataAdapter adapter = ..;

    adapter.OnRowUpdated = delegate(object sender, RowUpdatedEventArgs args) {

    // + args.RecordsAffected

    if (UpdateStatus.Continue == args.Status) {

    // ++ dataRowCount;

    }

    };

     

    int dataRowCountSaved = 0;

    try {

    dataRowCountSaved = adapter.Update(dataset);

    // generally (dataRowCount == dataRowCountSaved) at this point

    }

    catch(Exception e) {

    // if in a TransactionScope - everything in database may rollback.

    throw;

    }

    Monday, October 27, 2008 4:20 PM
    Moderator
  • In addition to doing the code above, I would just use a MessageBox that would surface the error message that is thrown rather than rethrowing the exception as Mark describes.

     

    try {

    dataRowCountSaved = adapter.Update(dataset);

    // generally (dataRowCount == dataRowCountSaved) at this point

    }

    catch(Exception e) {

    MessageBox.Show("Error, Records cannot commit:"+e.ToString());

    }

     

    Also you could catch different exceptions and be more precise, but using the message box would seem to be the way to go to get the message to your user.

     

    Thanks

    Chris Robinson

    Program Manager - DataSet

    Monday, October 27, 2008 5:37 PM
  • Hi Guys,

     

    Thanks for your help, could you please give me an example of this code.

     

    Thanks Ali

    Tuesday, October 28, 2008 12:09 AM