Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my asp code:
XML
<%@ 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>
&nbsp;
&nbsp;
<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>
    &nbsp;&nbsp;
&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList2" runat="server"
        Height="20px" Width="130px"></asp:DropDownList>


    <asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" />
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True" AutoGenerateColumns="False" DataSourceID="sds"
        Width="775px">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="pcode" HeaderText="Project Code"
                SortExpression="pcode" />
            <asp:BoundField DataField="fyyear" HeaderText="Financial year"
                SortExpression="fyyear" />
            <asp:BoundField DataField="date" HeaderText="Date" SortExpression="date" />
            <asp:BoundField DataField="salary" HeaderText="Salary"
                SortExpression="salary" />
            <asp:BoundField DataField="ta" HeaderText="TA" SortExpression="ta" />
            <asp:BoundField DataField="contigency" HeaderText="Contingency"
                SortExpression="contigency" />
            <asp:BoundField DataField="nrc" HeaderText="NRC" SortExpression="nrc" />
            <asp:BoundField DataField="institcharges" HeaderText="Institutional Charges"
                SortExpression="institcharges" />
            <asp:BoundField DataField="others" HeaderText="others"
                SortExpression="others" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="sds" runat="server"
        ConnectionString="<%$ ConnectionStrings:connstr %>"
        SelectCommand="SELECT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly] WHERE ([pcode] = @pcode)">
        <SelectParameters>
            <asp:Parameter Name="pcode" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    </asp:Content>





my code behind file where i havent done much i have just populated two dropdownlists and i m struggling with how to pass the values of dropdownlist which will act as filter for displaying data in gridview:
Posted

When you are creating the datasource of the gridview you can click the "where" button, here you can assign the dropdown as a where clause.
 
Share this answer
 
Instead of binding gridview in design page try by binding itin code behind, on click of submit button

protected void btnSubmit_Click(object sender, EventArgs e) {

Conn.Open();
Cmd = new SqlCommand("select * from Tablename where colum1=@value1 and column2=@value2", Conn);
Cmd.Parameters.Add("@value1", SqlDbType.datatype).Value = dropdownlist1.selectedvalue
Cmd.Parameters.Add("@value2", SqlDbType.datatype).Value = dropdownlist2.selectedvalue
da = new SqlDataAdapter(Cmd);
da.Fill(ds, "Tablename");
GridView1.DataSource = ds;
Conn.Close();


}
 
Share this answer
 
Comments
a2ulthakur 6-Nov-12 8:13am    
wat do i have to write in value1 and value2??
Divya RS 6-Nov-12 23:22pm    
I think i had clearly mentioned the values for value1 and value2:

Cmd.Parameters.Add("@value1", SqlDbType.datatype).Value = dropdownlist1.selectedvalue
Cmd.Parameters.Add("@value2", SqlDbType.datatype).Value = dropdownlist2.selectedvalue
in your codebehind.cs go to Button1_Click event inside it..

add code as...

sds.selectcommand="SELECT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly] WHERE ([pcode] = "+DropDownList1.selectedvalue.tostring()+") and ([fyyear]="+DropDownList2.selectedvalue.tostring()+")";
HTML



because when u edit the selectcommand it automatically populates the sqlds..and databind it..following databind the gridview..

sds.databind();
grid.databind();

it works..but one thing google it properly..u'll find no of examples..
 
Share this answer
 
Comments
a2ulthakur 6-Nov-12 10:48am    
m getting this error
Source Error:


Line 83: sds.SelectCommand = "SELECT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly] WHERE ([pcode] = " + DropDownList1.SelectedValue.ToString() + ") and ([fyyear]=" + DropDownList2.SelectedValue.ToString() + ")";
Line 84: sds.DataBind();
Line 85: GridView1.DataBind();
Line 86:
Line 87: }
a2ulthakur 6-Nov-12 10:54am    
one more thing this is happening when m selecting values from dropdownlist and then hitting the go button
a2ulthakur 6-Nov-12 22:08pm    
my problem still remains ...when i click my button nothing happens :(
Nandakishore G N 6-Nov-12 23:11pm    
what is the datatype of pcode and fyyear...if both are varchar..then alter the code like this..

sds.SelectCommand = "SELECT [pcode], [fyyear], [date], [salary], [ta], [contigency], [nrc], [institcharges], [others] FROM [monthly] WHERE ([pcode] = '" + DropDownList1.SelectedValue.ToString() + "') and ([fyyear]='" + DropDownList2.SelectedValue.ToString() + "')";

if u get any error related to sql queries..better pate the code in ssms and try u'll find out the problem...
Divya RS 6-Nov-12 23:24pm    
I suggest you to use breakpoint and check where you get the error
public void Button2_Click(object sender, EventArgs e)
   {
       try
       {


           SqlDataAdapter da = new SqlDataAdapter("select pcode,fyyear,date,salary,ta,contigency,nrc,institcharges,others from monthly where pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND fyyear=('" + DropDownList2.SelectedItem.ToString() + "')", con);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();

           DropDownList1.SelectedIndex = 0;
           DropDownList2.SelectedIndex = 0;
       }

       catch (Exception ex)
       {
           ex.Message.ToString();
       }


   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900