2012-01-10 37 views
0

StackOverflowException未處理。我需要幫助。我上線StackOverflowException在SqlDataAdapter.Fill上未處理()

adp.Fill(ds) 

錯誤此外,我不知道爲什麼,但我不能刪除throw,它返回一個值的所有代碼說不。

string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbCustConn"].ToString(); 
    string cmdStr = "Select * from MainDB"; 

    public DAL() // default parameter. Use? 
    { 
    } 

     public DataTable Load() // what is this for? (loads all the records from the database) 
     { 
      SqlConnection conn = new SqlConnection(connStr); 
      //SqlCommand cmd = new SqlCommand(cmdStr, connStr); 
      SqlDataAdapter adp = new SqlDataAdapter(cmdStr, connStr); // SqlDataAdapater? Load all? 
      DataSet ds = new DataSet(); 
      try 
      { 
       adp.Fill(ds); 
       return ds.Tables[0]; 
      } 
      catch 
      { 
       throw; 
      } 
      finally 
      { 
       ds.Dispose(); 
       adp.Dispose(); 
       conn.Close(); 
       conn.Dispose(); 
      } 
     } 

 public DataTable Load() // what is this for? (loads all the records from the database) 
     { 
      SqlDataAdapter adp = new SqlDataAdapter(cmdStr, connStr); // SqlDataAdapater? Load all? 
      DataSet ds = new DataSet(); 
      using(SqlConnection conn = new SqlConnection(connStr)) 
      { 
       adp.Fill(ds); 
       return ds.Tables[0]; 
      } 
     } 

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      BindGrid(); 
     } 
    } 

    private void BindGrid() 
    { 
     MasterCust.DataSource = GridDataSource(); 
     MasterCust.DataBind(); 
     //DetailCust.DataSource = ds2; 
     //DetailCust.DataBind(); 
    } 

    private DataTable GridDataSource() 
    { 
     BAL p = new BAL(); 
     DataTable dTable = new DataTable(); 
     try 
     { 
      dTable = p.Load(); 
     } 
     catch (StackOverflowException ee) 
     { 
      string message = ee.Message.ToString(); 
     } 
     finally 
     { 
      p = null; 
     } 
     return dTable; 
    } 
+0

「沒有返回值的所有代碼。」 - 如果你捕捉到一個異常但不重新拋出,那麼它將從catch塊中跳出到函數的結尾,而不通過'return'。你也可以使用'使用'塊來重寫try/finally,這可能會更清楚 - 你不需要關閉和處理連接,就足夠處理了(因爲我之前被告知) – Rup 2012-01-10 01:22:11

+3

你能提供嗎堆棧跟蹤?您也可以從學習使用'語句中受益。 – 2012-01-10 01:23:13

+0

我是新來的多層是這個代碼。我如何獲得堆棧跟蹤?我通常做Label1.Text = ex.StackTrace.ToString()。 – healxph0enix 2012-01-10 01:34:31

回答

3

首先,我認爲這個問題可能是在MasterCust。我認爲,然而,這是定義可能會導致你的問題。如果您更新關於如何定義這個問題的問題,這可能會帶來一些額外的亮點。

其次,你有很多無關的代碼可能會混淆這個問題。以下是我認爲你需要做的,削下來的最低限度:

protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      if (!IsPostBack) 
      { 
       BindGrid(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
      // Note that this is for debug purposes only. Production code should log 
      // this exception somewhere so that it can be observed and dealt with 
     } 
    } 

    private void BindGrid() 
    { 
     MasterCust.DataSource = BAL.Load(); 
     MasterCust.DataBind(); 
    } 

那麼你的業務的接入類:

public class BAL 
{ 
    private static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbCustConn"].ToString(); 
    private static string cmdStr = "Select * from MainDB"; 

    public static DataTable Load() // what is this for? (loads all the records from the database) 
    { 
     using (var adp = new SqlDataAdapter(cmdStr, connStr)) 
     { 
      var ds = new DataSet(); 
      adp.Fill(ds); 
      return ds.Tables[0]; 
     } 
    } 
} 
相關問題