2014-06-16 317 views
0

不填充我有以下ASP GridView控件:GridView控件使用數據綁定

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
      <Columns> 
       <asp:BoundField DataField="TEXT" HeaderText="TEXT" SortExpression="TEXT" /> 
       <asp:BoundField DataField="session_id" HeaderText="session_id" SortExpression="session_id" /> 
       <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" /> 
       <asp:BoundField DataField="command" HeaderText="command" SortExpression="command" /> 
       <asp:BoundField DataField="cpu_time" HeaderText="cpu_time" SortExpression="cpu_time" /> 
       <asp:BoundField DataField="total_elapsed_time" HeaderText="total_elapsed_time" SortExpression="total_elapsed_time" /> 
       <asp:BoundField DataField="dbname" HeaderText="dbname" ReadOnly="True" SortExpression="dbname" /> 
       <asp:BoundField DataField="user_running_query" HeaderText="user_running_query" SortExpression="user_running_query" /> 
      </Columns> 
     </asp:GridView> 

在後面的代碼我有:

protected void Page_Load(object sender, EventArgs e) 
     { 
      using (Repository.SqldbAllQueries()) 
      { 
       GridView1.DataBind(); 
      }  
     } 

而且我的倉庫類包含:

public static class Repository 
    { 
     public static SqlDataReader SqldbAllQueries() 
     { 
      SqlConnection sqlConnection1 = new SqlConnection("Data Source=sqldb;Initial Catalog=ReportServer;Integrated Security=True"); 
      SqlCommand cmd = new SqlCommand(); 
      SqlDataReader reader; 

      cmd.CommandText = @"SELECT sqltext.TEXT,req.session_id,req.status,req.command,req.cpu_time, 
           req.total_elapsed_time,DB_NAME(req.database_id) as dbname,s.login_name as user_running_query 
           FROM sys.dm_exec_requests req 
           CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext 
           INNER JOIN sys.dm_exec_sessions s ON req.session_id = s.session_id"; 

      cmd.CommandType = CommandType.Text; 
      cmd.Connection = sqlConnection1;    
      sqlConnection1.Open(); 
      reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
      return reader; 
     }   
    } 

我試圖捕獲每個當前正在運行的查詢在SQL數據庫和輸出在我的ASP頁面作爲網格視圖使用數據庫nd方法,代碼會生成並運行,但是gridview不會在頁面上顯示。我已經通過代碼,我可以看到reader包含行,但我似乎錯過了這裏的東西,因爲它們沒有被添加到gridview,任何人都可以看到問題?

+0

'DataBind'之前你需要設置'DataSource'屬性 – Grundy

回答

1

嘗試頁面加載

GridView1.DataSource = Repository.SqldbAllQueries(); 
GridView1.DataBind(); 
+0

GridView1在版本庫的上下文中不存在 – JsonStatham

+0

試試更新的代碼 – KarthikManoharan

1

這一項,

試試這個代碼。其實,如果你退回讀者,你可能會得到一個例外。

protected void Page_Load(object sender, EventArgs e) 
{ 
    DBAccess db = new DBAccess(); 
    db.FetchData(BindData); 
} 

private void BindData(SqlDataReader reader) 
{ 
    DataGridView1.DataSource = reader; 
    DataGridView1.DataBind(); 
} 

而這裏DBACCESS類(類似於庫類)

public void FetchData(Action<SqlDataReader> bindMethod) 
{ 
    using (SqlConnection sqlConnection = new SqlConnection("ConnectionString")) 
    { 
     sqlConnection.Open(); 
     using (SqlCommand sqlCommand = new SqlCommand("Query", sqlConnection)) 
     { 
      bindMethod.Invoke(sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)); 
     } 
    } 
} 

希望這個代碼可以幫助。