0

Hi..

I have 3 DropDownList:-

DropdownList1 has status,DropDownlist2 has date and DropDownList3 has duedate and based on these values selected i want to populate the Gridview following is my code..

 protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlDataAdapter sda = new SqlDataAdapter("Select * from DropDownFilter where status='" + DropDownList1.SelectedValue + "' and date='" + DropDownList2.SelectedValue + "' and duedate='" + DropDownList3.SelectedValue + "'", con);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

But unable to achieve it,can anyone please guide me??

Edited by Dani: Formatting fixed

2
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by bhagyap
0

Well I couldn't see in your code the "Connection object". And connection open method. Please see the following simple example:

http://www.dotnetperls.com/sqldataadapter

Second thing, store the dynamic query into variable and run it in SQL management studio to check either query is good.

Check the result in dataset, either it is filled or not.

Else grid binding is correct.

0

Hi..

I can filter gridview by only one dropdownlist not by other two dropdowns..

0

Couldn't get you, you wrote the code under button click event and in event's body you are getting the values from dropdowns then how it is possible to get data against one dropdown but not others? If you want to handle it on drop down selected index change event then you have to turn on the AutoPostback property of all dropdowns and write all code in SelectedIndexChange event. Looks fairly simple, don't know howz your development enviorment is setup.

0

<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True" >
<asp:ListItem>open</asp:ListItem>
<asp:ListItem>close</asp:ListItem>
<asp:ListItem>pending</asp:ListItem>

</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource2" DataTextField="date" DataValueField="id"
AutoPostBack="True"
>
</asp:DropDownList>
&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource3" DataTextField="duedate" DataValueField="id"
AutoPostBack="True"
>
</asp:DropDownList>
&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
<br />
<br />

</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource4">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="companyname" HeaderText="companyname"
SortExpression="companyname" />
<asp:BoundField DataField="status" HeaderText="status"
SortExpression="status" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:BoundField DataField="duedate" HeaderText="duedate"
SortExpression="duedate" />
</Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT id, companyname, status, date, duedate FROM DropDownFilter">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="status"
Name="status" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" DefaultValue="date" Name="date"
PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList3" DefaultValue="" Name="duedate"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

This is what i am trying but unable to filter gridview by dropdownlist..

0

I am trying to filter it here itself using filter parameters..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.