简体   繁体   中英

Gridview search/filter using sql

I got a gridview in c# webform, which contains the database from SQL server database, through sqlDataSource. The SqlDataSource contains a SQL string

SELECT * FROM [MaterialView] WHERE (([MaterialName] LIKE '%' + @MaterialName + '%') AND ([SupplierID] = @SupplierID))    

The SQL string filter the data twice, by SupplierID and MaterialName.

First the User determine SupplierID (@SupplierID parameter) on previous page, when gridview page is load, it execute ([SupplierID] = @SupplierID)).

And then, i got a "search box" + "button" which enable the the user to filter the gridview again, by entering text in "search box" (@MaterialName parameter), which execute ([MaterialName] LIKE '%' + @MaterialName + '%').

My problem is, when the gridview page is load,since the "search box" is initially EMPTY (no @MaterialName Parameter) , the gridview return 0 record ().

My way of fixing it is creating 2 similar Gridviews, one contains only supplierID filter,another contains both.

<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Height="40px" Width="289px"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="搜尋" OnClick="Button1_Click" />
            <!--初始表單-->
            <asp:Panel ID="Panel1" runat="server">
                <asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Height="498px" Width="712px" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="auto-style1">          
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="ClassType"   SortExpression="ClassType">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ClassType") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ClassType") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MaterialName" SortExpression="MaterialName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MaterialName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button2" runat="server" commandname="choose" CommandArgument='<%# Eval("MaterialID") %>' Text='<%# Eval("MaterialName") %>' />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MeasureUnit" HeaderStyle-width="10%" SortExpression="MeasureUnit">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("MeasureUnit") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("MeasureUnit") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Width="10%" />
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" Visible="False" />
                <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" Visible="False" />
                <asp:BoundField DataField="MaterialID" HeaderText="MaterialID" SortExpression="MaterialID" Visible="False" />
                <asp:TemplateField HeaderText="SupplierName" SortExpression="SupplierName" Visible="False">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("SupplierName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("SupplierName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
            </asp:Panel>
            <!--過濾表單-->
            <asp:Panel ID="Panel2" runat="server">
                <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" Height="498px" Width="710px" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="auto-style1">          
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="ClassType" HeaderText="ClassType" SortExpression="ClassType" />
                <asp:BoundField DataField="MaterialName" HeaderText="MaterialName" SortExpression="MaterialName" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
                <asp:BoundField DataField="MeasureUnit" HeaderText="MeasureUnit" SortExpression="MeasureUnit" />
                <asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" Visible="False" />
                <asp:BoundField DataField="MaterialID" HeaderText="MaterialID" SortExpression="MaterialID" Visible="False" />
                <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" Visible="False" />
                <asp:BoundField DataField="SupplierName" HeaderText="SupplierName" SortExpression="SupplierName" Visible="False" />
            </Columns>
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
            </asp:Panel>
        </div>

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyConnectionString2 %>" SelectCommand="SELECT * FROM [MaterialView] WHERE ([SupplierID] = @SupplierID)">
            <SelectParameters>
                <asp:SessionParameter Name="SupplierID" SessionField="Gsuply" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyConnectionString2 %>" SelectCommand="SELECT * FROM [MaterialView] WHERE (([MaterialName] LIKE '%' + @MaterialName + '%') AND ([SupplierID] = @SupplierID))">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="MaterialName" PropertyName="Text" Type="String" />
                <asp:SessionParameter Name="SupplierID" SessionField="Gsuply" Type="Int32" />
            </SelectParameters>
</asp:SqlDataSource>
    </form>

I expect an One gridview solution. When the textbox is empty (@MaterialName parameter = ""), the gridview return ALL @MaterialName instead of no @MaterialName. Thank you.

You need to check whether your query parameter is null or its length is 0 or if its contains some value then filter with Like

SELECT * FROM [MaterialView] 
WHERE 
((
 @MaterialName IS NULL 
 OR LEN(@MaterialName) = 0 
 OR ([MaterialName] LIKE '%' + @MaterialName + '%')
)
AND ([SupplierID] = @SupplierID));

It works when I set textbox default text = 0 (@MaterialName parameter source), and modified @er-sho SQL into

SELECT * FROM [MaterialView] 
WHERE 
((
 @MaterialName = 0 
 OR ([MaterialName] LIKE '%' + @MaterialName + '%')
 )
 AND ([SupplierID] = @SupplierID));

So i guess the problem is textbox.text default value (if not set) is neither NULL or LEN 0. I know this is a sloppy solution but i'm satisfy with it, Thanks again @er-sho

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM