In Asp.net SQLdatasource, I applied
filter expression and filter parameter. While filtering a data in the
dropdownlist, the Gridview should be loaded based on the filteration in the
dropdownlist. But Gridview is not loaded with data and gridview structure as
follows:
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="sqlDataSourceLocation" DataTextField="FirstName" DataValueField="ID"
AutoPostBack="True">
asp:DropDownList>
td>
tr>
<tr>
<td>
<asp:GridView ID="GridView1" ShowHeader="False" AllowPaging="True" runat="server"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
Columns>
asp:GridView>
td>
tr>
<tr><td> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString3 %>"
SelectCommand="SELECT [ID],[FirstName],[LastName],[Location] FROM [Employees]"
FilterExpression="[FirstName] = '{0}'">
<FilterParameters>
<asp:ControlParameter ControlID="DropDownList1"
Name="FirstName" PropertyName="SelectedValue" />
FilterParameters>
asp:SqlDataSource>
<asp:SqlDataSource ID="sqlDataSourceLocation" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString3 %>"
SelectCommand="SELECT Distinct [FirstName], [ID] FROM [Employees]">asp:SqlDataSource>td>tr>
table>
Please help me regarding this.
Thanks a lot

Mohan JPosted Nov 15, 2012, 1:50 AM
The code you provided will be helpful to me. But I asked using sqldatasource Filter Parameter. In that case, filtering based on dropdown is not working. And so, I would like to expect answers based on sqldatasource filter parameter.
Thanks a lot.
Satyapriya NayakPosted Nov 10, 2012, 11:10 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Page_Load(object sender, EventArgs e)
{
Dropdownlist1.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
Dropdownlist1.Items.Add("Choose FirstName");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Dropdownlist1.Items.Add(reader["FirstName"].ToString());
}
reader.Close();
con.Close();
}
}
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee where FirstName='" + Dropdownlist1.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
GridView1.DataSource = com.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}