如何在尝试保存之前捕获重复的记录错误? [英] How can i capture a duplicate record error before trying to save it?

查看:89
本文介绍了如何在尝试保存之前捕获重复的记录错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向数据库输入新记录时遇到问题。有一个名为'Title'的关键字段,有时我搞砸了,并尝试输入已存在的东西。输入完整数据后,我保存它,但如果有重复,它会关闭并显示错误消息。如何捕获此错误并允许用户中止例程?



我认为的输入代码是通过BindingNavigator及以下,以及下面的错误。是我的记录保存代码。错误显示在'Me.DVD_ListBindingSource.EndEdit()'语句中。如果它有所作为我在Visual Basic 2015中工作。







 私有  Sub  DVD_ListBindingNavigatorSaveItem_Click(发件人 As  对象,e 作为 EventArgs)句柄 DVD_ListBindingNavigatorSaveItem.Click 

If MessageBox.Show(& quot; SAVE THIS RECORD TO THE DATABASE& quot;& amp;
vbCrLf& amp; vbLf& amp;& quot;你确定吗?& quot;,
MessageBoxButtons.YesNo,MessageBoxIcon。停止)=
DialogResult.Yes 然后

.Validate ()
.DVD_ListBindingSource.EndEdit()
.TableAdapterManager.UpdateAll ( Me ._ D _DATA_FILES_DVD_LIST_MDFDataSet)
MessageBox.Show(& quot; RECORD SAVED& quot;,& quot; AS REQUESTED& quot;,
Else
MessageBox.Show(& quot; RECORD NOT SAVED& quot;,& ;AS REQUESTED& quot;,
结束 如果



错误消息。



类型#39的未处理异常; System.Data.ConstraintException'发生在System.Data.dll中< br $> b $ b

我尝试过的事情:



我最初想到的是抓住错误并显示它的捕获方法,但我不确定我在寻找什么for。

解决方案

也许您想重新考虑将 Title 作为关键字段。从理论上讲,可能会有不同的DVD具有相同的标题。



无论你是否遵循这个建议我都会解决这个主要问题:



用户输入标题后(即输入字段失去焦点后)查询数据库是否存在重复。如果涉及潜在的网络延迟,则应该异步完成以不阻止UI。如果找到任何重复项,请向用户显示,以便他可以判断它是实际重复还是偶然相同的标题。如果他确定它是重复的,请清除新行。



它的美妙之处:如果有重复,用户很早就会知道并且不仅在填写完所有内容之后其他领域也没有。并且您不必处理插入错误(当然,如果您不遵循我的第一个建议,那么您将不得不询问用户是否重复但是立即清除该行。)


使用Try ... Catch块:尝试... Catch ...最后声明(Visual Basic) [ ^

I am having a problem entering new records into the database. There is a key field called 'Title' and sometimes I screw up and try to enter something that is already there. after entering the full data I save it but if there is a duplicate it shuts down with an error message. How can I capture this error and allow the user to abort the routine?

The The entry code I believe is through BindingNavigator and below and the error below that. is my record save code. The error shows at the 'Me.DVD_ListBindingSource.EndEdit()' statement. If it makes a difference I am working in Visual Basic 2015.



Private Sub DVD_ListBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles DVD_ListBindingNavigatorSaveItem.Click

 If MessageBox.Show(&quot;SAVE THIS RECORD TO THE DATABASE&quot; &amp;
                             vbCrLf &amp; vbLf &amp; &quot;Are you sure?&quot;,     
                             MessageBoxButtons.YesNo, MessageBoxIcon.Stop) =
                             DialogResult.Yes Then

            Me.Validate()
            Me.DVD_ListBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me._D__DATA_FILES_DVD_LIST_MDFDataSet)
            MessageBox.Show(&quot;RECORD  SAVED&quot;, &quot;AS REQUESTED&quot;,  
        Else
            MessageBox.Show(&quot;RECORD NOT SAVED&quot;, &quot;AS REQUESTED&quot;,                    
        End If


The error msg.

An unhandled exception of type #39;System.Data.ConstraintException' occurred in System.Data.dll

What I have tried:

I originally thought of a Try-Catch method of catching the error and displaying it, but I'm not sure of what I'm looking for.

解决方案

Maybe you want to reconsider making Title a key field. Theoretically there could be different DVD's with the same title.

Regardless if you follow this suggestion or not I would solve the main issue like this:

After the user has entered the title (that is, after the input field loses focus) query the database for potential duplicates. If there's potentially network latency involved this should be done asynchronously to not block the UI. If any duplicates are found, show them to the user so he can decide whether it's an actual duplicate or just an identical title by chance. If he decides that it's a duplicate, clear the new row.

The beauty of it: If there is a duplicate the user knows early and not only after filling in all other fields as well for nothing. And you don't have to handle insert errors (of course, if you don't follow my first suggestion you would then have to not ask the user if it's a duplicate but clear the row right away).


Use a Try...Catch block: Try...Catch...Finally Statement (Visual Basic)[^]


这篇关于如何在尝试保存之前捕获重复的记录错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆