简体   繁体   English

如何在 TDataSet.UpdateRecord 中捕获错误?

[英]How can I catch errors in TDataSet.UpdateRecord?

My application uses a QuantumGrid (v6.56) and a TSimpleDataSet as its data source.我的应用程序使用 QuantumGrid (v6.56) 和 TSimpleDataSet 作为其数据源。

Problem: an exception is thrown if the user enters a non-numeric value in aa numeric field.问题:如果用户在数字字段中输入非数字值,则会引发异常。 The code does not reach the OnBeforePost handler.代码未到达 OnBeforePost 处理程序。

How can the application catch the error (and display a message to the user), if the exception is thrown before OnBeforePost?如果在 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

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.您需要在 cxGrid 中设置列的“Properties”属性,在那里您可以分配 CurrencyEdit,以便用户只能输入数字,同样在设置“Properties”后,您可以使用 OnValidate 事件来验证用户输入。

In your example, the event OnBeforePost isn't reached indeed.在您的示例中,确实没有达到 OnBeforePost 事件。

You mention the cxGrid, but the fact is, your issue is easy to reproduce.您提到了 cxGrid,但事实是,您的问题很容易重现。

Take a simple TDBEdit linked on a TFloatField and try to enter the value "7.7.7.7.7.7.7.7".取一个链接在 TFloatField 上的简单 TDBEdit 并尝试输入值“7.7.7.7.7.7.7.7”。 Float fields accept the decimal separator, but won't validate that only 1 is present.浮点字段接受小数分隔符,但不会验证仅存在 1。 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.如果您想“在途中”拦截它,那么 TField.OnSetText 可能是您最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM