2012-01-10 40 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]; 
     } 
    } 
} 
相关问题