Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# 如果查询字符串为空,则返回表中的所有值_C#_Asp.net_Query String - Fatal编程技术网

C# 如果查询字符串为空,则返回表中的所有值

C# 如果查询字符串为空,则返回表中的所有值,c#,asp.net,query-string,C#,Asp.net,Query String,好的……我有一个页面,根据querystring列出了表中的产品 因此,如果我说foo.aspx?fam=1,那么家族1的所有产品都将被列出 如果查询字符串为空,如何使代码列出所有值 我的SQL命令必须是不同的…我真的不知道如何才能做到这一点 <asp:SqlDataSource ID="ds_produtos" runat="server" ConnectionString="<%$ ConnectionStrings:LocalSqlServer2 %>"

好的……我有一个页面,根据querystring列出了表中的产品

因此,如果我说foo.aspx?fam=1,那么家族1的所有产品都将被列出

如果查询字符串为空,如何使代码列出所有值

我的SQL命令必须是不同的…我真的不知道如何才能做到这一点

<asp:SqlDataSource ID="ds_produtos" runat="server" 
   ConnectionString="<%$ ConnectionStrings:LocalSqlServer2 %>" 

    SelectCommand="SELECT DISTINCT [IdProduct], [Name], [Description], [IdFamily], [Price],  [Stock] FROM [Product] WHERE ([IdFamily] = @IdFamily )">
    <SelectParameters>
        <asp:QueryStringParameter Name="IdFamily" QueryStringField="fam" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

将查询更改为

从[Product]中选择不同的[IdProduct]、[Name]、[Description]、[IdFamily]、[Price]、[Stock],其中([IdFamily]=@IdFamily或@IdFamily为空)


如果没有为fam指定值,则传递null。

为属性设置一个默认值,以便在不向页面传递任何值时,该属性具有一个值:

<asp:QueryStringParameter Name="IdFamily" QueryStringField="fam" Type="Int32" DefaultValue="-1" />

确保CancelSelectOnNullParameter设置为false。
SelectCommand="select IdProduct, [Name], Description, IdFamily, Price, Stock from Product where IdFamily = @IdFamily or @IdFamily = -1"