Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GridView不返回行_C#_Asp.net_Sql Server_Gridview - Fatal编程技术网

C# GridView不返回行

C# GridView不返回行,c#,asp.net,sql-server,gridview,C#,Asp.net,Sql Server,Gridview,我有一个简单的GridView,它使用3个会话变量;用户登录时捕获的CoName和PurchaseOrderDate,以及作为SQLServer 2012数据库上存储过程的输入参数在文本框中捕获的HeatNumber(两者都是可选的)。我有一个CustomerLanding.aspx页面,上面有一个搜索按钮,单击该按钮时,会将用户重定向到一个TestReportLanding.aspx页面,该页面上当前有GridView。在这个场景中,一切都正常工作 我想将GridView移动到Customer

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

我想将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>
这是我的搜索按钮后面的代码: 请注意,如果我在这里取消对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);

    }

提前谢谢你的帮助

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

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

存储过程的参数不是真正的“可选”参数,除非它们已被初始化为某种类型

终于在这里找到了答案:


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

我只是做了,但仍然没有记录。请记住,当我将用户重定向到另一个页面时,此sp工作。不过,我会将Date和Heat初始化为NULL,这是有意义的。您在这两个可选字段中使用会话变量有什么特别的原因吗?您可以将这些参数改为asp:controlparameters:我也很好奇,如果您按两次按钮,是否会得到任何结果。可能是某种奇怪的回发问题,在页面发布(或重定向)之前不会将文本框中的值保存到会话中。老实说,我不知道ControlParameter控件。几分钟后我会调查一下。按两次按钮会得到相同的结果。明白了。如果不需要在每页之间保留这些值,我肯定会使用ControlParameter。“Name”属性应与SP参数名称相同。我突然想到,gridview上也可能缺少一个databind()方法。更新参数后再进行一次尝试。Gridview1.Databind();
@CustomerName varchar(50),
@Date date = NULL,
@Heat varchar(50) = NULL