我们如何在浏览器上打印数据库错误 [英] how we can print database error on browser

查看:102
本文介绍了我们如何在浏览器上打印数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有表StudentDetails和字段名称如

(id int primery key not null)

(name varchar(50)null)

(地址varchar(max)null)



现在我在Id字段中插入了错误的值



suppose i have table StudentDetails and Field name like
(id int primery key not null)
(name varchar(50) null)
(Address varchar(max) null)

Now I am inserting wrong Value in Id field

insert into StudentDetails (Id,name,Address) Values ('','irfan','new delhi')



但它显示




but its displaying

ERROR is ::>>>  Cannot insert explicit value for identity column in table 'tbl_MinOrder' when IDENTITY_INSERT is set to OFF.





因为Id字段自动递增为主要key

所以请您告诉我当我在填写学生详细信息表单时插入值时,我在浏览器上看到这个错误信息。



编辑:mdirfan_00@yahoo.co.in



Because Id field is auto incremented as primary key
so please will you tell me how i see this error messege on browser when trying to insert value by filling student details form .

Edit : mdirfan_00@yahoo.co.in

推荐答案

我建​​议您禁用ID字段,不要从用户输入ID。



最简单有效的解决方案。
I would suggest you to disable the ID field and don't take the input for ID from User.

Easiest and efficient solution.


避免插入部分中的id并在id.ref中设置自动增量此链接进行设置自动增量自动增量

Avoid id in insertion section and set auto increment in your id.ref this link for setting auto increment Auto Increment.
insert into StudentDetails (name,Address) Values ('irfan','new delhi')






如果你想打印数据库错误,你可能先捕获catch块中的错误,并在浏览器中显示,你可以写下面的代码



catch(exception ex)

{

Hi,

If you want to print the DB error ,you may catch the error in catch block first and for show in the browser ,you could write the below code

catch(Exception ex)
{
string myScript = @"alert(ex.Message.ToString());";
       Page.ClientScript.RegisterStartupScript(this.GetType(),
          "MyScript", myScript, true);



}



更好的是维护Log for DB错误。


}

better is to maintain the Log for DB error.


这篇关于我们如何在浏览器上打印数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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