2011-07-11 120 views
3

我在ASP.NET中編寫了一個代碼,它從SQL Table讀取數據並在Grid View中顯示並使用Row Data Bound Event.But在運行該程序時,在mscorlib.dll中發生未處理的System.StackOverflowException異常

private void BindAllUsers() 
    { 
     SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Name, Email, Password, Contact, CreatedOn, CreatedBy,CreatedIP From tbl_Users",con); 
     DataSet ds = new DataSet(); 
     da.Fill(ds);  <------(Error occurs in this line) 
     gdv_Users.DataSource = ds; 
     gdv_Users.DataBind(); 

    } 

的RowDataBoundEvent處理程序是:

protected void gdv_Users_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     e.Row.Cells[0].Style["Cursor"] = "hand"; 
     e.Row.Cells[0].ToolTip = "Click Here"; 
     e.Row.Cells[0].Attributes.Add("onclick","window.open('Details.aspx'?ID=" + e.Row.Cells[0].Text.ToString()+"'Details';'width = 735,height= 350,left = 220,top = 300,resizable = 0,scrollbars = 0,status = no')"); 
    } 

的BindAllUser功能是此異常的代碼所示聲明出現「類型‘System.StackOverflowException’mscorlib.dll中發生未處理的異常」這裏叫:

protected void Page_Load(object sender, EventArgs e) 
{ 
    BindAllUsers(); 
    BindDropDown(); 

} 
+1

你怎麼在Visual Studio調用堆棧看? – SLaks

+3

我想你是在OnRowDataBoundEvent中調用BindAllUsers,並且因爲你正在使網格重新陷入無限循環。你能發佈RowDataaBound事件處理程序嗎? – Chandu

+0

@Rachit:你還可以在你調用BindAllUsers方法的地方發佈代碼嗎? – Chandu

回答

0

試試這個:

 private void BindAllUsers() 
    { 
     using (SqlConnection con = new SqlConnection("connection string")) 
     { 
      con.Open(); 
      SqlCommand command = new SqlCommand(); 
      command.Connection = con; 
      command.CommandText = "SELECT ID, Name, Email, Password, Contact, CreatedOn, CreatedBy,CreatedIP From tbl_Users"; 
      SqlDataReader dr = command.ExecuteReader(); 
      if (dr.HasRows) 
      { 
       gdv_Users.DataSource = ds; 
       gdv_Users.DataBind(); 
      } 
     } 

    } 
+0

同時檢查您是否從OnDataBound事件調用此方法? – matrix

+0

是的,我從OnDataBound調用這個方法。刪除,它工作正常。謝謝。 –

相關問題