2009-11-17 55 views
5

我真的很奇怪的情況。 我已經創建了新的aspx頁面,並且沒有使用任何自定義邏輯對象(用視覺工作室嚮導創建的所有東西)嘗試從sqldatasource創建網格視圖。空gridview雖然sqldatasource有值

數據來自存儲過程,具有默認值的單個參數。當我刷新模式或單擊「測試查詢」時,我看到結果行和GridView字段被正確地創建。但是當我運行該頁面時,沒有網格視圖(它只是空的 - 當我添加EmptyDataTemplate時,它顯示)。我已經添加了自定義(空)函數和DataBind,DataBinded和RowCreted事件,並且只激發了數據綁定和datavound事件(儘管,正如我寫的 - 存儲過程的默認參數返回行和.net可以在設計模式下讀取它們)

程序中沒有任何「花哨」,我已經多次做這個,沒有任何問題。我試着在我們的生產ENV另一個存儲過程至極的作品,仍然得到了同樣的emty gridview的

這裏是代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TEST.aspx.cs" Inherits="site.TEST" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
      AllowSorting="True" OnDataBinding="GridView1_DataBinding" OnDataBound="GridView1_DataBound" 
      OnRowCreated="GridView1_RowCreated"> 
      <EmptyDataTemplate> 
       No Data Available 
      </EmptyDataTemplate> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
      SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure"> 
      <SelectParameters> 
       <asp:Parameter DefaultValue="val1" Name="par1" Type="String" /> 
       <asp:Parameter Name="val2" Type="Int32" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 

和代碼隱藏

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 

namespace site 
{ 
    public partial class TEST : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     {//brake here 

     } 

     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
     {//brake here 

     } 

     protected void GridView1_DataBinding(object sender, EventArgs e) 
     {//brake here 

     } 

     protected void GridView1_DataBound(object sender, EventArgs e) 
     {//brake here 

     } 
    } 
} 

回答

13

從你最近的評論看起來好像SQL事件探查器沒有顯示時,SelectCommandSqlDataSource發出的任何活動執行的選擇。這可能是由於SelectParameters集合中包含的參數默認設置爲true。另外,默認情況下,SqlDataSource上的CancelSelectOnNullParameter設置爲true。這意味着你的'val2'參數可能傳遞一個NULL值,這反過來取消了數據檢索操作。這就是爲什麼你在SQL Profiler中看不到任何活動的原因。

嘗試在SqlDataSource上將設置CancelSelectOnNullParameter設置爲false。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:myConnectionString %>" SelectCommand="myStoredProcedure"SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> </asp:SqlDataSource>

+1

你無法想象我是多麼的感恩:) – SimSimY 2009-11-18 08:47:06

+0

+1你剛剛救了我幾個小時! :) – meda 2013-07-02 19:14:44

+1

謝謝。 6年後,你幫了我:-) 不知道爲什麼MS做默認的東西造成混亂。 – 2015-09-06 07:50:31

0

我還沒有看到參數(val1,val2)的分配,所以默認啓動將爲字符串和int分配默認值並傳遞給SQL服務器。

嘗試的SQL Server Profiler來看看你是否有真正的參數

+0

外觀和數據passas他們應該(與第一和沒有價值的默認值(在存儲過程本身) 它看起來像有到DB沒有連接(缺省值profiler沒有顯示BatchStarting事件,但是當我從managamnet工作室執行該程序時 - 它出現了) – SimSimY 2009-11-17 16:35:15