我有一个gridview,它由存储过程通过标准n层结构提供,应该触发空行对象,但它没有。相反,我得到一个异常“IListSource 不包含任何数据源”。如果我在 SQL Server Management Studion 中运行查询,它会返回一个空行。
我怀疑问题出在 transact-sql 上,我使用的是 PIVOR 构造,由于我不太熟悉它的工作原理,我认为这会导致问题。它是从这个博客借来的,并由我的前任修改。
有谁知道是什么导致了这个问题?这里是代码。
<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
Just to reiterate, if I run 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 substitute the @SiteGuid with an actual value that I know has no record I get an empty row but in my app I get the exception.