dpreznik
dpreznik

Reputation: 247

GridView is empty

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();
}

Upvotes: 1

Views: 587

Answers (4)

dpreznik
dpreznik

Reputation: 247

It worked when I removed DataSourceID from GridView's markup and added grdUsers.DataSource = UserSqlDataSource; in code behind. I wonder why it didn't work the way it was before...

Upvotes: 1

Varga Tamas
Varga Tamas

Reputation: 66

Maybe you set the SelectCommand too late in the page life cycle? Do you call grdUsers.DataBind() after setting the SelectCommand?

Upvotes: 0

Joel C
Joel C

Reputation: 5567

From the MSDN documentation for the SqlDataSource control:

Note
By default, if one of the parameters is null when you execute a Select command, no data will be returned and no exception will be thrown. You can change this behavior by setting the CancelSelectOnNullParameter property to false.

Upvotes: 1

Ta01
Ta01

Reputation: 31610

User is a reserved keyword, have you tried:

SELECT ID, Name, FirstName, LastName, EMail, 
      FROM [User] WHERE Name NOT IN ('*All', 'Admin', 'TPS') ";

Upvotes: 1

Related Questions