I have a page (which has a MasterPage) with a GridView and an SqlDataSource. If I set SelectCommand in the markup, the GridView is displayed fine. But if I generate the same SelectCommand dynamically, the GridView is empty. And I know that the query is executed. What can be the reason for it? Thanks.
<asp:SqlDataSource ID="UserSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:BookList %>" DataSourceMode="DataSet"
ProviderName="<%$ ConnectionStrings:BookList.ProviderName %>" >
</asp:SqlDataSource>
<asp:GridView ID="grdUsers" runat="server" AllowPaging="True" ShowHeader="false" ShowFooter="true"
AutoGenerateColumns="false"
Width="1480px" Height="100%" PageSize="50" DataSourceID="UserSqlDataSource" DataKeyNames="ID">
<AlternatingRowStyle CssClass="alternatingrowstyle" />
<Columns>
<asp:TemplateField HeaderText="User Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUsers();
}
}
protected void BindUsers()
{
string strSelectClause =
@"SELECT ID, Name, FirstName, LastName, EMail,
FROM User WHERE Name NOT IN ('*All', 'Admin', 'TPS') ";
string strQuery = strSelectClause + GenerateWhereClause() + " ORDER BY Name;";
SqlDataSource UserSqlDataSource = GetControl(this, "UserSqlDataSource") as SqlDataSource;
UserSqlDataSource.SelectCommand = strQuery;
grdUsers.DataBind();
}