How to trap SQL2005 RI messages to show a nice message to the user?


Author
Message
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi,

When trying to delete a parent record, the MS-SQL2005 throws an execption preventing the deletion of the record, which is just fine.  Is there a way to trap this exception and display a more user friendly message? or is there something in SF tha will take care of this for us?

Here is the Exception message:

SqlException
  The DELETE statement conflicted with the REFERENCE constraint "Has_many". The conflict occurred in database "ATR", table "dbo.ServiceCalls", column 'FK_Customers'.
The statement has been terminated.

Source     : .Net SqlClient Data Provider

Stack Trace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at MicroFour.StrataFrame.Data.DbDataSourceItem.InternalExecuteNonQuery(DbCommand Command, Boolean IsTransactional, String TransactionKey)
   at MicroFour.StrataFrame.Data.DbDataSourceItem.ExecuteNonQuery(DbCommand Command, Boolean IsTransactional, String TransactionKey)
   at MicroFour.StrataFrame.Data.DataLayer.DeleteByPrimaryKey(PrimaryKeyValue PKValue, DataRow Row)
   at MicroFour.StrataFrame.Business.BusinessLayer.DeleteByPrimaryKey(PrimaryKeyValue PKValue)
   at MicroFour.StrataFrame.Business.BusinessLayer.DeleteCurrentRow(Boolean CheckSecurity, Boolean OnlyMarkAsDeleted)
   at ATR_System.frmCustomers.tsiDelete_Click(Object sender, EventArgs e) in E:\Visual Studio 2005\StrataFrame Projects\ATR Systems\ATR System with not security\ATR System\Forms\frmCustomers.vb:line 217
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
   at System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)



Edhy Rijo

Replies
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Ben,

Thanks for clarifications on this one.  I decided to handle this in the BO's BeforeDelete as follow, if you have any suggestion on how to improve this code, it will be greatly appreciated. Tongue

''' <summary>

''' Validate the Service Call before trying to delete it.

''' </summary>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ServiceCallsBO1_BeforeDelete(ByVal e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgs) Handles ServiceCallsBO1.BeforeDelete

     Dim LoopCounter As Integer = 0

     For Counter As Integer = 1 To e.BusinessObject.ChildBusinessObjects.Count

          If e.BusinessObject.ChildBusinessObjects.Item(LoopCounter).Count > 0 Then

               e.Cancel = True

               Me.AutoShowDeleteConfirmation = False

               MessageBox.Show("To delete this Service Call you must delete all related records first.", "Record Delete Warning...", MessageBoxButtons.OK, MessageBoxIcon.Information)

               Exit For

          End If

          LoopCounter += 1

     Next

End Sub



Edhy Rijo

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Edhy,



When you BeforeDelete event handler is called, the user has already confirmed or canceled the delete, so the line

Me.AutoShowDeleteConfirmation = False


is not going to do much at this point.



What you need to do is to set this in the designer for the form, the in your BeforeDelete, handle the confirmation for the user. So, the logic in you BeforeDelete would be:



- Check that all child objects are deleted (current logic, minus call to turn off AutoShowDeleteConfirmation)

- If any children found, show message (as your doing), cancel delete (as your doing), exit for, then exit handler

- If no children found, get confirmation from user that they want to do delete.

- If they cancel, cancel delete (e.cancel=true).



Hope that makes sense.
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Greg,

I tried your suggestion, but the problem is that when entering into the form's BeforeDelete() event, the delete confirmation message is already shown, wich leads me to believe that this message is only displayed in the BO.

I ended up creating a form's public var:

Public CheckAutoShowDeleteConfirmation As Boolean = False

Then in the BO's BeforeDelete:

Dim LoopCounter As Integer = 0

For Counter As Integer = 1 To e.BusinessObject.ChildBusinessObjects.Count

     If e.BusinessObject.ChildBusinessObjects.Item(LoopCounter).Count > 0 Then

          e.Cancel = True

          Me.CheckAutoShowDeleteConfirmation = True

          Me.AutoShowDeleteConfirmation = False

          MessageBox.Show("To delete this Service Call you must delete all related records first.", "Record Delete Warning...", MessageBoxButtons.OK, MessageBoxIcon.Information)

          Exit For

     End If

     LoopCounter += 1

Next

Then in the Form's BeforeDelete:

If Me.CheckAutoShowDeleteConfirmation = True Then

     Me.CheckAutoShowDeleteConfirmation = False

     Me.AutoShowDeleteConfirmation = True

End If

This way, the AutoShowDeleteConfirmation will work if the end user wants to delete a record with no children and will get the confirmation first.



Edhy Rijo

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Threaded View
Threaded View
Edhy Rijo - 18 Years Ago
Paul Chase - 18 Years Ago
Edhy Rijo - 18 Years Ago
Greg McGuffey - 18 Years Ago
Edhy Rijo - 18 Years Ago
Jc Martinez - 18 Years Ago
Greg McGuffey - 18 Years Ago
StrataFrame Team - 18 Years Ago
StrataFrame Team - 18 Years Ago
Edhy Rijo - 18 Years Ago
Greg McGuffey - 18 Years Ago
Edhy Rijo - 18 Years Ago
Larry Caylor - 18 Years Ago
Edhy Rijo - 18 Years Ago
Greg McGuffey - 18 Years Ago
Edhy Rijo - 18 Years Ago

Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search