如何自定义外键错误消息 [英] How to customize foreign key error message

查看:87
本文介绍了如何自定义外键错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好...
当我删除网页中从后端获取的项目时,如果它是外键,则会发生错误.很好,但是我得到此错误的页面是显示错误msg以及直接sql错误msg的页面,其中表的名称用于主键和外键.如何自定义呢?我从不希望其他人看到我的后端表名称.如何为用户提供自定义消息,警告不能删除数据?一种方法是检查数据的存在,并在例如Label中给出一条消息.还有其他方法吗?请帮帮我.
在此先谢谢您..

Hi there...
When I delete an item in my webpage, which is fetched from back end, if it is a foreign key, an error occurs. That is fine, but I get this error as a page showing error msg with the direct sql error msg with the tables'' name used for primary and foreign keys. How to customize this? I never want others to see my back end table names. How can I give a customized message for the users warning that the data can''t be deleted? One way is to check the existnce of data and giving a message in for instance a Label. Any alternative way? help me please.
Thanks in advance..

推荐答案

首先,您必须在应用程序中创建一个自定义错误页面.对于服务器错误,通常为"500.aspx",对于找不到"错误,通常为"404.aspx".
您无需在此页面中指定错误描述,只需指定有关该错误的一般消息即可.
不要忘记将以下代码放入您的web.config:

First of all you have to create a custom error page in your application. Usually it is "500.aspx" for server errors, and "404.aspx" for "not found" errors.
You don''t need to specify an error description in this pages, just a generic message about the error.
Don''t forget to put the following code into your web.config:

<pre><br />
<system.web><br />
   <customErrors mode="On" defaultRedirect="404.aspx"><br />
     <error statusCode="404" redirect="404.aspx"/><br />
     <error statusCode="500" redirect="500.aspx"/><br />
   </customErrors><br />
 </system.web> </pre><br />
If you want to catch and report the particular error then use the code provided by two previous answers and either email the error to the admin or place it into an error log.


要自定义错误msg,只需将代码包含在try和catch中,然后在异常处理程序中即可显示自定义msg ...
to customize the error msg , you just include your code in try and catch , and then in the exception handler you can show the your custome msg...


如前一篇文章所述,您需要使用try catch块.但是您需要将错误捕获到正确的异常类型.

例如,为了捕获SQL Server的错误,我将使用

as the previous post said you need to use a try catch block. but you need to trap the error to the correct exception type.

For example to trap errors with SQL Server I would use a

try
catch ex as sqlexception
end



那么您需要确定需要捕获的错误,例如检查错误号并报告您希望用户看到的消息.



then You need to work out which error you need to trap, e.g. check the error number and report the message that you want the user to see.


这篇关于如何自定义外键错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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