如何捕获 TDataSet.UpdateRecord 中的错误?
我的应用程序使用 QuantumGrid (v6.56) 和 TSimpleDataSet 作为其数据源。
问题:如果用户在数字字段中输入非数字值,则会引发异常。该代码不会到达 OnBeforePost 处理程序。
如果在 OnBeforePost 之前 抛出异常,应用程序如何捕获错误(并向用户显示消息)?
堆栈跟踪:
exception class : EDatabaseError
exception message : '...' is not valid Integer value for Field <somefieldname>
main thread ($504):
00526c7e +082 EditBooking.exe DB DatabaseError
00526d0f +04b EditBooking.exe DB DatabaseErrorFmt
0052c428 +07c EditBooking.exe DB TIntegerField.SetAsString
0052af30 +00c EditBooking.exe DB TField.SetText
0052abf9 +021 EditBooking.exe DB TField.SetEditText
007c3a3f +053 EditBooking.exe cxDBData
007e14a8 +018 EditBooking.exe cxGridDBDataDefinitions TcxGridDBDataController.UpdateData
006f8159 +039 EditBooking.exe cxCustomData TcxCustomDataProvider.DoUpdateData
007c1c18 +008 EditBooking.exe cxDBData TcxDBDataLink.UpdateData
0053325d +021 EditBooking.exe DB TDataLink.UpdateRecord
005333f8 +0d8 EditBooking.exe DB TDataLink.DataEvent
007c1683 +00f EditBooking.exe cxDBData TcxDBDataLink.DataEvent
00533973 +03f EditBooking.exe DB TDataSource.NotifyLinkTypes
005339a2 +01e EditBooking.exe DB TDataSource.NotifyDataLinks
005339cf +023 EditBooking.exe DB TDataSource.DataEvent
005392ad +131 EditBooking.exe DB TDataSet.DataEvent
00553d7c +058 EditBooking.exe DBClient TCustomClientDataSet.DataEvent
00539396 +042 EditBooking.exe DB TDataSet.UpdateRecord
0053a356 +006 EditBooking.exe DB TDataSet.Post
0055677c +054 EditBooking.exe DBClient TCustomClientDataSet.Post
My application uses a QuantumGrid (v6.56) and a TSimpleDataSet as its data source.
Problem: an exception is thrown if the user enters a non-numeric value in a a numeric field. The code does not reach the OnBeforePost handler.
How can the application catch the error (and display a message to the user), if the exception is thrown before OnBeforePost?
Stack trace:
exception class : EDatabaseError
exception message : '...' is not valid Integer value for Field <somefieldname>
main thread ($504):
00526c7e +082 EditBooking.exe DB DatabaseError
00526d0f +04b EditBooking.exe DB DatabaseErrorFmt
0052c428 +07c EditBooking.exe DB TIntegerField.SetAsString
0052af30 +00c EditBooking.exe DB TField.SetText
0052abf9 +021 EditBooking.exe DB TField.SetEditText
007c3a3f +053 EditBooking.exe cxDBData
007e14a8 +018 EditBooking.exe cxGridDBDataDefinitions TcxGridDBDataController.UpdateData
006f8159 +039 EditBooking.exe cxCustomData TcxCustomDataProvider.DoUpdateData
007c1c18 +008 EditBooking.exe cxDBData TcxDBDataLink.UpdateData
0053325d +021 EditBooking.exe DB TDataLink.UpdateRecord
005333f8 +0d8 EditBooking.exe DB TDataLink.DataEvent
007c1683 +00f EditBooking.exe cxDBData TcxDBDataLink.DataEvent
00533973 +03f EditBooking.exe DB TDataSource.NotifyLinkTypes
005339a2 +01e EditBooking.exe DB TDataSource.NotifyDataLinks
005339cf +023 EditBooking.exe DB TDataSource.DataEvent
005392ad +131 EditBooking.exe DB TDataSet.DataEvent
00553d7c +058 EditBooking.exe DBClient TCustomClientDataSet.DataEvent
00539396 +042 EditBooking.exe DB TDataSet.UpdateRecord
0053a356 +006 EditBooking.exe DB TDataSet.Post
0055677c +054 EditBooking.exe DBClient TCustomClientDataSet.Post
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 cxGrid 中设置列的“属性”属性,在那里您可以分配一个货币编辑,以便用户只能输入数字,在设置“属性”后,您可以使用 OnValidate 事件来验证用户输入。
You need to set the "Properties" property of your column in the cxGrid, there you can assign a CurrencyEdit so the user can only enter numbers, also after setting "Properties" you can use the OnValidate event to validate the user entry.
在您的示例中,确实未到达事件 OnBeforePost 。
您提到了 cxGrid,但事实是,您的问题很容易重现。
采用链接在 TFloatField 上的简单 TDBEdit 并尝试输入值“7.7.7.7.7.7.7.7”。浮点字段接受小数分隔符,但不会验证仅存在 1。因此,输入此内容也会导致以类似的方式引发错误,无论是当您退出该字段时还是在发布时(如果您有允许执行此操作的快捷方式)。
如果您想“在传入的过程中”拦截它,那么 TField.OnSetText 可能是您最好的选择。
In your example, the event OnBeforePost isn't reached indeed.
You mention the cxGrid, but the fact is, your issue is easy to reproduce.
Take a simple TDBEdit linked on a TFloatField and try to enter the value "7.7.7.7.7.7.7.7". Float fields accept the decimal separator, but won't validate that only 1 is present. So entering this will also result in an error being raised in a similar fashion, either when you exit the field, or while posting, if you have a shortcut that allows you to do so.
If you want to intercept it "on the way in", then TField.OnSetText is probably your best bet.