Asp.net 在浏览器上调试以创建适当的捕获错误消息时,是否有方法获取数据库sqlException/Error?
在浏览器上调试时,是否有方法获取数据库Asp.net 在浏览器上调试以创建适当的捕获错误消息时,是否有方法获取数据库sqlException/Error?,asp.net,sql,sql-server-2005,visual-studio-2008,error-handling,Asp.net,Sql,Sql Server 2005,Visual Studio 2008,Error Handling,在浏览器上调试时,是否有方法获取数据库SQLException? 例如,主键冲突(添加记录时复制PK) 我使用的是VS 2008和SQL Server 2005,如果有必要的话。在SQL Server上运行命令: RAISERROR('Here comes the exception text', 16,1) SQL代码 Set NOCOUNT ON Set XACT_ABORT ON Begin Try ...Your SQL Code End Try Begin Catch
SQLException
?
例如,主键冲突(添加记录时复制PK)
我使用的是VS 2008和SQL Server 2005,如果有必要的话。在SQL Server上运行命令:
RAISERROR('Here comes the exception text', 16,1)
SQL代码
Set NOCOUNT ON
Set XACT_ABORT ON
Begin Try
...Your SQL Code
End Try
Begin Catch
Rollback Tran
Select Error_Number() as No, Error_Message() as Message
End Catch
页面级错误
protected override void OnError(EventArgs e)
{
Exception CurrentException = Server.GetLastError();
string ErrorDetails = CurrentException.ToString();
// this could also be a string[] if you prefer
var script = string.Format("foo({0});", ErrorDetails);
ClientScript.RegisterStartupScript(GetType(), "call", script, true);
base.OnError(e);
}
void Application_Error(object sender, EventArgs e)
{
Exception CurrentException = Server.GetLastError();
string ErrorDetails = CurrentException.ToString();
// this could also be a string[] if you prefer
var script = string.Format("foo({0});", ErrorDetails);
ClientScript.RegisterStartupScript(GetType(), "call", script, true);
base.OnError(e);
}
应用程序级错误
protected override void OnError(EventArgs e)
{
Exception CurrentException = Server.GetLastError();
string ErrorDetails = CurrentException.ToString();
// this could also be a string[] if you prefer
var script = string.Format("foo({0});", ErrorDetails);
ClientScript.RegisterStartupScript(GetType(), "call", script, true);
base.OnError(e);
}
void Application_Error(object sender, EventArgs e)
{
Exception CurrentException = Server.GetLastError();
string ErrorDetails = CurrentException.ToString();
// this could also be a string[] if you prefer
var script = string.Format("foo({0});", ErrorDetails);
ClientScript.RegisterStartupScript(GetType(), "call", script, true);
base.OnError(e);
}
可以,您可以使用System.Web.HttpContext输出JavaScript警报消息框 您将System.Data.SqlClient.SqlCommand放入try catch中,并在catch中输出异常作为JavaScript警报(您需要转义消息以正确处理特殊字符和引号) 这里是我的方法(VB.NET)
Public Shared Sub Show(ByRef objMessage作为对象,可选ByRef strTitle作为字符串=Nothing)
Dim strMessage As String=CStr(objMessage)
Dim pgCallingPage As Page=TryCast(HttpContext.Current.Handler,第页)
如果pgCallingPage不是什么,那么
strMessage=Microsoft.JScript.GlobalObject.escape(strMessage)'在项目引用中引用Microsoft.JScript.dll
strMessage=“警报(unescape(“+strMessage+”);”
pgCallingPage.ClientScript.RegisterStartupScript(pgCallingPage.GetType(),System.Guid.NewGuid().ToString(),strMessage,False)
如果结束
端接头
这里是C#转换:
public static void Show(object objMessage, string strTitle = null)
{
string strMessage = Convert.ToString(objMessage);
Page pgCallingPage = HttpContext.Current.Handler as Page;
if (pgCallingPage != null) {
strMessage = Microsoft.JScript.GlobalObject.escape(strMessage);
// Reference Microsoft.JScript.dll in the project reference
strMessage = "<script type=\"text/javascript\" language=\"javascript\">alert(unescape(\"" + strMessage + "\"));</script>";
pgCallingPage.ClientScript.RegisterStartupScript(pgCallingPage.GetType(), System.Guid.NewGuid().ToString(), strMessage, false);
}
}
publicstaticvoidshow(objectobjmessage,stringstrtitle=null)
{
字符串strMessage=Convert.ToString(objMessage);
Page pgCallingPage=HttpContext.Current.Handler作为页面;
如果(pgCallingPage!=null){
strMessage=Microsoft.JScript.GlobalObject.escape(strMessage);
//在项目引用中引用Microsoft.JScript.dll
strMessage=“警报(unescape(\”“+strMessage+”\”);”;
pgCallingPage.ClientScript.RegisterStartupScript(pgCallingPage.GetType(),System.Guid.NewGuid().ToString(),strMessage,false);
}
}
如果您不喜欢警报消息框(因为无法复制粘贴),我建议您只输出一行新文本(将位于文档顶部):
公共共享子写入线(ByRef objMessage作为对象)
Dim strMessage As String=CStr(objMessage)
HttpContext.Current.Response.Write(“
“+HttpContext.Current.Server.HtmlEncode(strMessage)+“
”)
端接头
公共静态无效写入线(对象objMessage)
{
字符串strMessage=Convert.ToString(objMessage);
HttpContext.Current.Response.Write(“
”+HttpContext.Current.Server.HtmlEncode(strMessage)+“
”;
}
我认为您需要问自己一个问题:“我为什么要尝试复制主键?”。根据定义,您不应该这样做。这就是为什么它们被称为主键。使用暂存表…很抱歉我的问题,这里的新手。@mitch my table从用户处获取主键(学生id号)我的代码不是那种“添加记录时生成/增加PK”的代码。我只是想考虑各种可能性,而且这只是一个示例。请给我一些建议。