2013-01-17 32 views
2

我有一個gridview由一個存儲proc通過標準的n層結構,應該觸發emplty行對象,但它沒有。相反,我得到一個異常「IListSource不包含任何數據源」。如果我在SQL Server Management Studion中運行查詢,它將返回一個空行。GridView不拾取空的數據對象

我懷疑問題是與transact-sql,我使用PIVOR construct,因爲我不那麼熟悉它是如何工作的,我認爲這會導致問題。它是從this blog借來的,並被我的前任修改過。

有沒有人有一個想法是什麼原因導致這個問題?這裏的代碼。

<asp:GridView ID="gvSystemRMRiskRpt" runat="server" AutoGenerateColumns="true" 
    CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue" 
    BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact" GridLines="Vertical" 
    OnRowDataBound="gvSystemRMRiskRpt_OnDataBound" EmptyDataText="Your request has returned zero records" >     
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> 
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> 
    <AlternatingRowStyle BackColor="Gainsboro" /> 
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" VerticalAlign="Top" /> 
</asp:GridView> 

代碼文件:

protected void btnSearch_Click(object sender, EventArgs e) 
    { 
     Guid site = new Guid(ddlSites.SelectedValue); 
     int interval = Convert.ToInt16(ddlIntervals.SelectedValue); 
     string[] startDate = tbStartDate.Text.Split('/'); // 01-2001 
     string[] stopDate = tbEndDate.Text.Split('/');  // 12/2002 

     gvSystemRMRiskRpt.DataSource = Data.ByMonthYear(connectionManager,site,startDate[1],stopDate[1],interval); 
     gvSystemRMRiskRpt.DataBind(); 
    } 

數據層:

public static DataSet ByMonthYear(ConnectionManager connectionManager, Guid site, string startDate, string stopDate, int interval) 
    { 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = "[dbo].[ByMonthYear_StoredProc]"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Connection = connectionManager.GetSqlConnectionContext(); 
     cmd.Transaction = connectionManager.GetSqlTransactionContext(); 

     SqlParameter paramSiteGUID = new SqlParameter("@SiteGUID", SqlDbType.UniqueIdentifier); 
     paramSiteGUID.Value = site; 
     cmd.Parameters.Add(paramSiteGUID); 

     SqlParameter paramStartDate = new SqlParameter("@StartDate", SqlDbType.VarChar); 
     paramStartDate.Value = startDate; 
     cmd.Parameters.Add(paramStartDate); 

     SqlParameter paramStopDate = new SqlParameter("@StopDate", SqlDbType.VarChar); 
     paramStopDate.Value = stopDate; 
     cmd.Parameters.Add(paramStopDate); 

     SqlParameter paramProcedureAction = new SqlParameter("@ProcedureAction", SqlDbType.Int); 
     paramProcedureAction.Value = 0; // From Primary Key 
     cmd.Parameters.Add(paramProcedureAction); 

     SqlParameter paramInterval = new SqlParameter("@Interval", SqlDbType.Int); 
     paramInterval.Value = interval; 
     cmd.Parameters.Add(paramInterval); 

     SqlDataAdapter sqlDA = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     sqlDA.Fill(ds); 

     cmd.Dispose(); 
     cmd = null; 

     return ds; 
    } 

和實際的存儲過程:

alter PROCEDURE [dbo].[ByMonthYear_StoredProc] 
@ProcedureAction int, 
@SiteGUID uniqueidentifier = null, 
@StartDate varchar(4), 
@StopDate varchar(4), 
@Interval int 
AS 

DECLARE @cols NVARCHAR(2000) 
DECLARE @query NVARCHAR(4000) 

IF (@ProcedureAction = 0) -- From 
BEGIN 

    SET NOCOUNT ON 
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED 

    IF (@Interval = 1) -- Montly 
    BEGIN 
     SELECT @cols = STUFF((SELECT DISTINCT TOP 100 PERCENT '],[' + cast(MonthYear as varchar(10)) FROM vByMonthYear where SiteGUID = @SiteGuid 
     and [Year] BETWEEN '2005' AND '2010' ORDER BY '],[' + cast(MonthYear as varchar(10)) FOR XML PATH('')), 1, 2, '') 
     + ']' SET @query = N'SELECT EventType, ' + @cols +' FROM (SELECT MonthYear,EventType,Value,OrderBy FROM vRByMonthYear 
     where SiteGUID = ' + CHAR(39) + CONVERT(nvarchar(36), @SiteGuid) + CHAR(39) + ' and Year BETWEEN 2005 AND 2010) p 
     PIVOT (Sum ([Value]) FOR MonthYear IN ('+ @cols +')) AS pvt order by OrderBy' 
     execute(@query) 
    END 
END 

只是重申,如果我運行SELECT DISTINCT TOP 100 PERCENT'],['+ cast(MonthYear as varchar (10))FROM vByMonthYear where SiteGUID = @SiteGuid and [Year] BETWEEN'2005'AND'2010'in the query window,and I + YourSiteGuid with a actual value that I know not no record I get a empty row but in我的應用程序,我得到的例外。

+0

是年數據庫中的實際4字符字段..?關於BTWEEN聲明..?如果你得到一個錯誤/異常,那麼你需要在這裏粘貼這個異常。 – MethodMan

+0

@DJKRAZE:是的,年份是4個字符的數據庫字段。例外是「IListSource不包含任何數據源。」拋出DataBind(); – Risho

+0

當您調試此行上的代碼時vSystemRpt.DataSource是否返回填充的DataSource? – MethodMan

回答

0

我懷疑這是發生了什麼,但我不是100%確定。該適配器

SqlDataAdapter sqlDA = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    sqlDA.Fill(ds); 

是充滿不一行數據,因此被返回的數據集不視爲空 - 不知道爲什麼。我懷疑奇怪的SQL Pivot正在返回一些引用或空白區域。

無論如何,我改變了返回一個DataTable而不是DataSet方法和DROP掉整個SqlDataAdapter,而是使用了SqlReader其工作。在這個過程中可能會節省大約1毫秒的開銷。

using (SqlDataReader _reader = cmd.ExecuteReader()) 
{ 
    DataTable dt = new DataTable(); 
    dt.Load(_reader); 
    cmd.Dispose(); 
    return dt; 
}