GridView不返回行

时间:2016-03-22 17:16:59

标签: c# asp.net sql-server gridview

我有一个使用3个会话变量的简单GridView;用户登录时捕获的CoName和在文本框中捕获的PurchaseOrderDate和HeatNumber(两者都是可选的)作为SQLServer 2012数据库上存储过程的输入参数。我有一个CustomerLanding.aspx页面,上面有一个搜索按钮,当点击它时,会将用户重定向到当前在其上有GridView的TestReportLanding.aspx页面。在这种情况下,一切都正常。

我想将GridView移动到CustomerLanding.aspx,这样我就可以在包含搜索参数和搜索按钮的同一页面上显示搜索结果。最终我想使用AJAX进行更新,但是现在我只是将GridView放在另一个div中。当我使用此设置时,查询仅在提供可选参数(PurchaseOrderDate和HeatNumber)时返回结果。我的存储过程只需要CoName进行搜索,因此我应该返回更多行,并将可选参数留空。

对.NET来说有点新,我希望我在这里遗漏一些简单的东西。

这是CustomerLanding中的GridView代码以及SqlDataSource:

<div class="col-xs-12 col-lg-10">
<asp:Label ID="NoRecordsFound" runat="server"  Visible ="false" Text="No records found."></asp:Label>
               <asp:GridView ID="GridView1" runat="server" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" GridLines="Vertical" BackColor="White" ForeColor="Black" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                   <AlternatingRowStyle BackColor="#CCCCCC" />
                   <Columns>
                       <asp:BoundField DataField="doc" HeaderText="doc" SortExpression="doc" />
                       <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                       <asp:BoundField DataField="Heat" HeaderText="Heat" SortExpression="Heat" />
                       <asp:BoundField DataField="Dir" HeaderText="Dir" SortExpression="Dir" />
                   </Columns>
                </asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:iiConnectionString %>" SelectCommand="sp_test_reports" SelectCommandType="StoredProcedure">
                   <SelectParameters>
                       <asp:SessionParameter Name="CustomerName" SessionField="CoName" Type="String" />
                       <asp:SessionParameter DbType="Date" Name="Date" SessionField="PurchaseOrderDate" />
                       <asp:SessionParameter Name="Heat" SessionField="HeatNumber" Type="String" />
                   </SelectParameters>
               </asp:SqlDataSource>
</div>

这里有什么值得我的存储过程(我确定这不是问题,因为当DataGrid在一个单独的页面上时它可以工作:

CREATE PROCEDURE [dbo].[sp_test_reports] 
-- Add the parameters for the stored procedure here
@CustomerName varchar(50),
@Date date,
@Heat varchar(50)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here


IF @Heat IS NULL AND @Date IS NULL
    SELECT DISTINCT TOP 1000 d.doc, d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 and d.Customer = @CustomerName;
ELSE IF @Heat IS NOT NULL AND @Date IS NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND dl.Heat = @Heat;
ELSE IF @Heat IS NULL AND @Date IS NOT NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND d.Date = @Date;
ELSE IF @Heat IS NOT NULL AND @Date IS NOT NULL
    SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
    FROM documents d
    INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
    WHERE d.type = 2 AND d.Customer = @CustomerName AND d.Date = @Date AND dl.Heat = @Heat;


END

以下是我的搜索按钮背后的代码: 请注意,如果我在此处取消注释Response.Redirect,则GridView的工作方式应该如此。

protected void TestReports_Click(object sender, EventArgs e)
    {
        Session["PurchaseOrderDate"] = DatePurchaseOrder.Text;
        Session["HeatNumber"] = HeatText.Text;



        GridView1.Visible = true;



        MessageBox.Show(Session["CoName"].ToString());
        MessageBox.Show(Session["HeatNumber"].ToString());

        if (GridView1.Rows.Count == 0)
        {
            NoRecordsFound.Visible = true;
        }
        else
        {
            NoRecordsFound.Visible = false;
        }

        //Response.Redirect("TestReportLanding.aspx", false);

    }

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

在存储过程中,将两个可选参数初始化为NULL。

@CustomerName varchar(50),
@Date date = NULL,
@Heat varchar(50) = NULL

存储过程的参数实际上并不是“可选的”,除非它们已经初始化为某些东西。

答案 1 :(得分:0)

终于在这里找到了答案:

empty gridview although the sqldatasource has values

基本上在SqlDatasource上设置CancelSelectOnNullParameter =“false”。