i have 2 dropdownlists and a submit button when i should click the submit button the selected values shold be taken as input parametres for filtering and populating the data in gridview?

this is the asp code:

<%@ Page Title="" Language="C#" MasterPageFile="~/grid.master" AutoEventWireup="true" CodeFile="yearwiseex.aspx.cs" Inherits="Default2" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Select Project Code"></asp:Label>
 
 
<asp:DropDownList ID="DropDownList1" runat="server"
       Height="20px" Width="130px"
       onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
        AutoPostBack="True"> </asp:DropDownList>

    <asp:Label ID="Label2" runat="server" Text="Select Financial Year"></asp:Label>
      
  
<asp:DropDownList ID="DropDownList2" runat="server" 
        Height="20px" Width="130px"></asp:DropDownList>


    <asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" ></asp:Button>
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
    <br />
    <br />
    <br />
    <br />
    <br />
 
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" 
        BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" 
        CellSpacing="2" DataSourceID="sds" ForeColor="Black" style="margin-left: 30px" 
        Width="95%">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowSelectButton="True" ></asp:CommandField>
            <asp:BoundField DataField="pcode" HeaderText="Project Code" 
                SortExpression="pcode" ></asp:BoundField>
            <asp:BoundField DataField="fyyear" HeaderText="Financial Year" 
                SortExpression="fyyear" ></asp:BoundField>
            <asp:BoundField DataField="date" HeaderText="Date" SortExpression="date" ></asp:BoundField>
            <asp:BoundField DataField="salary" HeaderText="Salary" 
                SortExpression="salary" ></asp:BoundField>
            <asp:BoundField DataField="ta" HeaderText="TA" SortExpression="ta" ></asp:BoundField>
            <asp:BoundField DataField="contigency" HeaderText="Contingency" 
                SortExpression="contigency" ></asp:BoundField>
            <asp:BoundField DataField="nrc" HeaderText="NRC" SortExpression="nrc" ></asp:BoundField>
            <asp:BoundField DataField="institcharges" HeaderText="Institutional Charges" 
                SortExpression="institcharges" ></asp:BoundField>
            <asp:BoundField DataField="others" HeaderText="Others" 
                SortExpression="others" ></asp:BoundField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ></FooterStyle>
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" ></HeaderStyle>
        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" ></PagerStyle>
        <RowStyle BackColor="White" ></RowStyle>
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" ></SelectedRowStyle>
        <SortedAscendingCellStyle BackColor="#F1F1F1" ></SortedAscendingCellStyle>
        <SortedAscendingHeaderStyle BackColor="#808080" ></SortedAscendingHeaderStyle>
        <SortedDescendingCellStyle BackColor="#CAC9C9" ></SortedDescendingCellStyle>
        <SortedDescendingHeaderStyle BackColor="#383838" ></SortedDescendingHeaderStyle>
    </asp:GridView>
    <asp:SqlDataSource ID="sds" runat="server" 
        ConnectionString="<%$ ConnectionStrings:connstr %>" 
        SelectCommand="SELECT DISTINCT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly]">
    </asp:SqlDataSource>
    <br />
    </asp:Content>

this is the code behind file m using:




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        if (Page.IsPostBack == false)
        {
            string query = "select * from project";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            DropDownList1.DataSource = ds;
            DropDownList1.DataValueField = ds.Tables[0].Columns[0].ToString();
            DropDownList1.DataTextField = ds.Tables[0].Columns[1].ToString();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select--");

            cmd.Dispose();
        }

        con.Close();

    }




    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        try
        {
            string query1 = "Select * from yearly where pid = " + DropDownList1.SelectedItem.Value.ToString();
            SqlCommand cmd = new SqlCommand(query1, con);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            DropDownList2.DataSource = ds;

            DropDownList2.DataValueField = ds.Tables[0].Columns[0].ToString();
            DropDownList2.DataTextField = ds.Tables[0].Columns[1].ToString();

            DropDownList2.DataBind();
            DropDownList2.Items.Insert(0, "--Select--");

            cmd.Dispose();
        }
        catch
        {

        }

        con.Close();



    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        sds.SelectCommand = "SELECT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly] WHERE ([pcode] = " + DropDownList1.SelectedValue.ToString() + ") and ([fyyear]=" + DropDownList2.SelectedValue.ToString() + ")";
        sds.DataBind();
        GridView1.DataBind();

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.