0

I have got very basic Product data table, I set up my grid view data source to sqlDataSoruce with paging and sorting options true.

But I also have a DropDownList and if a user chooses a certain product group ( Vegetables, Fruits, Frozen etc), grid view only shows that product group items.

String sqlSearchByProduct = " SELECT ID, pID,pGroupID, pName, pPrice, Status, Stock FROM productsTable  WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)";
    sqlDataSource.SelectCommand = sqlSearchByProduct ; 

The grid shows all products from groupID selected from dropdownlist but the issue is when I want to sort it by name or price, grid view shows all products again.

I will be very grateful if you know how to fix this issue. Thank you.

2 Answers 2

1

You need to keep SelectedValue of the dropdown and the easiest way to do so is to bind all data declaratively. Something like this:

<asp:DropDownList ID="ddCategory" runat="server" DataTextField="categoryname"
    DataValueField="categoryid"
    AppendDataBoundItems="true" DataSourceID="sqlCategory" AutoPostBack="true">
    <asp:ListItem Value="0" Text="- Select -"></asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="sqlCategory" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
    SelectCommand="select categoryid,categoryname from dbo.categories">
</asp:SqlDataSource>

<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="ProductID" DataSourceID="sqlNWind">
    <Columns>
        <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="sqlNWind" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
    SelectCommand="SELECT Products.ProductID, Products.ProductName FROM Products where (@categoryid=0 or CategoryID=@categoryId)">
    <SelectParameters>
        <asp:ControlParameter Name="categoryid" ControlID="ddCategory" Type="Int32" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>

No code behind required.

Side note: never build sql query string like this

" ... WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)"

Such code is prone to SQL Injection attack. Always use parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Alex, for help, but what will I do if I have got more than one DropDownList. " SELECT Products.ProductID, Products.ProductName FROM Products where (CategoryID=@categoryId) or (Price < @price) or (inStock = @stockStatusTrue" etc.. How about if user only choose only two option.
@Adil All the same. Add parameters to the select query, add dropdownlists and set all of them AutoPostBack="true". They work independently and filter select as a sum of filters. Wrap GridView into UpdatePanel for better performance.
"How about if user only choose only two option". See how where clause is built in my answer. where (@categoryid=0 /*nothing selected*/ or CategoryID=@categoryId)
0

Have you rebound your data to the gridview, e.g. below

GridView1.DataBind();

1 Comment

Yes, I did and nothing changes. Same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.