2011-07-11 72 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调用这个方法。删除,它工作正常。谢谢。 –

相关问题