【问题标题】:GridView shows queried data but does not show all data when primary key is nullGridView显示查询到的数据,但主键为null时不显示所有数据
【发布时间】:2021-09-16 23:52:39
【问题描述】:

我正在尝试在 GridView 中显示所有资助者信息,并且还能够过滤它们。截至目前,如果我在 URL 中硬编码 FunderId=somenumber,它会显示数据。但是当 data 为 null 时,它什么也不显示,而程序告诉它显示每个 FunderId 的所有数据。

我正在使用这个选择存储过程。如果有指定的FunderId,则显示该FunderId 的数据。如果没有指定FunderId,则显示所有资助者的数据。

ALTER PROCEDURE [dbo].[spFunderId_Select]
    @FunderId int = NULL 
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * 
    FROM vueFunders 
    WHERE ([FunderId] = @FunderId OR @FunderIdIS NULL) 
END

这是 aspx 标记:

<asp:GridView ID="gvFunderData" runat="server" AutoGenerateColumns="False"
     DataSourceID="sdsFunders"
     ForeColor="#333333" GridLines="None"
     CssClass="GridView"
     PagerStyle-CssClass="pgr"
     ShowFooter="true"
     AlternatingRowStyle-CssClass="alt" AllowPaging="true"
     PageSize="20" AllowSorting="true"
     EmptyDataText="There are no data records to display."">

            <Columns>
                <%--FnderId--%>
                <asp:TemplateField HeaderText="FunderId" SortExpression="FunderId">
                    <ItemTemplate>
                        <asp:Label ID="lblFunderId" runat="server" Text='<%# Eval("FunderId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


                <%--Money--%>
                <asp:TemplateField HeaderText="Money" SortExpression="Money">
                    <ItemTemplate>
                        <asp:Label ID="lblMoney" runat="server" Text='<%# Eval("Money")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>


                <%--BankName--%>
                <asp:TemplateField HeaderText="NameOfBank" SortExpression="NameOfBank">
                    <ItemTemplate>
                        <asp:Label ID="lblNameOfBank" runat="server" Text='<%#Eval("NameOfBank") %>'></asp:Label>
                    </ItemTemplate>
                  
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


  <asp:SqlDataSource ID="sdsFunders" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseLive %>"
        SelectCommandType="StoredProcedure"
        SelectCommand="spFunderId_Select">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="0" Name="FunderId" QueryStringField="FunderId" Type="Int32"/>
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="FunderId" DbType="Int32" />
            <asp:Parameter Name="Money" DbType="Int32" />
            <asp:Parameter Name="NameOfBank" DbType="String" />
        </UpdateParameters>
    </asp:SqlDataSource>

当然,如果我注释掉QueryStringParameter,它会显示所有资助者的所有数据,但是你不能过滤它,因为你不能查询FunderId。感谢所有帮助!

【问题讨论】:

  • 如果 FunderId 为 NULL,您的存储过程是否会返回所有数据?你检查过这个吗?
  • 是的,如果我在 SSMS 中运行它,它会返回所有数据。 DECLARE FunderId int = NULL SELECT * FROM vueFunders WHERE ([FunderId] = FunderId OR FunderId IS NULL) END 绝对返回所有内容。这真是令人难以置信
  • 如果我声明 FunderId = 4 或任何实际的 FunderId,也会在 SSMS 中运行,所以对我来说为什么前端没有显示任何内容是没有意义的。
  • 我不是 ASP 专家,但您似乎发送的是 0 作为默认值而不是 null。如果您运行扩展事件来跟踪发送到服务器的内容,您可以看到 proc 作为参数接收的内容。
  • @JMabee 我在调试时也尝试过。更改了

标签: c# asp.net tsql webforms


【解决方案1】:

试试这个

SELECT  * FROM vueFunders WHERE (   @FunderId IS NULL OR  @FunderId = 0 OR [FunderId] = @FunderId )

【讨论】:

  • 这结合 解决了我的问题。谢谢!
猜你喜欢
  • 2013-10-09
  • 2016-10-16
  • 2014-02-21
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多