I have use one dropdownList and one grideview, dropdownList is binding with sql database, and on selection of item in dropdownlist result will display in gridview. When i have running the program it will take only 1st index from dropdownlist and gives ans, i have to select 2nd , 3rd or any other item but it will take only 1st. Why this problem occour,
And how i have to solve this...
My code is as below:
Default.aspx
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged1">
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string str = "SELECT DISTINCT(year(date)) as date FROM OrderDetails";
ObjCon.Open();
SqlCommand cmd = new SqlCommand(str, ObjCon);
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataTextField = "date";
DropDownList1.DataBind();
dr.Close();
ObjCon.Close();
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
string Str = "SELECT OrderNo,CustId,Amount,CONVERT(VARCHAR(10),date,107) FROM OrderDetails WHERE year(date)='" + DropDownList1.SelectedItem.Text+ "'";
SqlCommand cmd = new SqlCommand(Str, ObjCon);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "OrderDetails");
GridView1.DataSource = ds;
GridView1.DataBind();
}