2012-01-10 14 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

「の値を返さないすべてのコード。」 - 例外をキャッチしても再スローしない場合、キャッチブロックから関数の最後まで、 'return'を通らずに落ちます。また、try/finallyを 'using'ブロックを使って書き直すことができます - それはもっと明白かもしれません - あなたは接続を閉じて処分する必要はありません。十分に処分してください(私は前に話しましたように) – Rup

+3

スタックトレース?また、 'using'ステートメントについて学ぶことで利益を得ることができます。 –

+0

私はこのコードであるマルチレイヤーに新しいです。 stacktraceを取得するには?私は通常、Label1.Text = ex.StackTrace.ToString()を行った。 – healxph0enix

答えて

3

まず、私は問題がMasterCustにおそらくあると思います。私はそれがあなたの問題を引き起こす可能性があります定義されていると思う。これがどのように定義されているかについて質問を更新すると、それはいくつかの追加の光を放つかもしれません。

第2に、問題を混乱させる余分なコードがたくさんあります。ここで私はあなたが最低限にそれを削り取っするために必要だと思うものです:

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]; 
     } 
    } 
} 
関連する問題