2013-04-06 52 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);