2013-04-06 53 views
1

我正在使用“服务”类连接到数据库并将DataTable返回到我的ASP.NET页面。来自类的ASP.NET提醒框

我使用try/catch来捕获SqlExceptions。我想在警告框中向用户显示异常文本。我该怎么做呢?

这是我在Service.class功能:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Web; 

namespace Classes 
{ 
    public class Service 
    { 
     private static DataTable getDataTable(string query) 
     { 
      DataTable dt = null; 
      SqlConnection con = Dao.getConnection(); 
      if (Dao.checkConnection()) 
      { 
       SqlDataAdapter da = new SqlDataAdapter(query, con); 
       dt = new DataTable(); 
       try 
       { 
        da.Fill(dt); 
       } 
       catch (SqlException ex) 
       { 
       //MessageBox.Show(ex.Message); 
       Response.Write("<script>alert('This is Alert');</script>"); 
       //HttpContext.Current.Response.Write(); 
       //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ","alert('ERROR')", true); 
       //var clientScript = Page.ClientScript; 
       //clientScript.RegisterClientScriptBlock(null, "AlertScript", "alert('ERROR')'", true); 
       //System.Diagnostics.Debug.WriteLine("Database error: " + ex.Message); 
      } 
     } 
     return dt; 
    } 

编译器给了我这样的: 名称“回应”在目前情况下不存在。

我想这是非常基本的,只是一直未能找到答案。 评论的东西是我到目前为止所尝试的!

+0

返回结果给调用者ASPX并使用[RegisterStartupScript](http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx)从UI中正确显示警报?业务/数据对象不应写入响应流。 – 2013-04-06 18:23:27

回答

0

继承试试这个

ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + String Exception message + "');", true);