sgibbons
sgibbons

Reputation: 3630

Dynamically built SelectCommand for GridView SqlDataSource in asp.net

I'm working with a GridView that uses a SqlDataSource element that looks like this:

        <asp:SqlDataSource ID="InventoryDB" runat="server" ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>"
            SelectCommand="SELECT [Server], [Customer] FROM [Website] WHERE [Owner] = 'someOwner'">
        </asp:SqlDataSource>

I'd like to replace the 'someOwner' part of the where clause with something dynamic, like so:

SelectCommand="SELECT [Server], [Customer] FROM [Website] WHERE [Owner] = '<%# UserManager.getCurrentUser(Request) %>'"

But when I do this, it seems to use the literal text of the WHERE clause instead of evaluating my function call, which of course does not work. What is the right way to do this?

Upvotes: 3

Views: 11705

Answers (1)

Jeromy Irvine
Jeromy Irvine

Reputation: 11804

The proper way to handle that is to use parameters. The MSDN documentation on it is pretty thorough in showing how to use them.

User Parameters with Data Source Controls has some more detailed and accessible information on using parameters.

Upvotes: 4

Related Questions