views:

5053

answers:

4

I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField.
The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview.
The SelectCommand has a WHERE clause which is to compare a string.
Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause.

How do I add single quotes inside an Eval FormatString?

I have tried using 'Replace'.
I have tried using 'Special Characters' (... WHERE StringField = '{0}' ...)

No luck so far. I appreciate any help you may be able to offer.

<asp:GridView ID="GridView1" runat="server" DataSourceID="DataSource1" DataKeyNames="Foo" AutoGenerateColumns="False" AllowSorting="true" >
    <Columns>
     <asp:BoundField DataField="Foo" HeaderText="Foo" SortExpression="Foo" />
     <asp:BoundField DataField="Bar" HeaderText="Bar" SortExpression="Bar" />
     <asp:TemplateField>
      <ItemTemplate>
       <asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSourceNested">
        <ItemTemplate>
         <asp:Label ID="Label1" runat="server" Text='<%# Eval("Blah") %>'></asp:Label>
        </ItemTemplate>
       </asp:Repeater>
       <asp:SqlDataSource ID="DataSourceNested" runat="server" DataFile="~/App_Data/DatabaseName"
                    SelectCommand='<%# Eval("Bar", "SELECT Blah FROM TableName WHERE (StringField = {0})") %>' >
                </asp:SqlDataSource>
      </ItemTemplate>
     </asp:TemplateField>
    </Columns>
</asp:GridView>
A: 

Have you tried escaping the single quote characters?

... WHERE (StringField = \'{0}\') ...
Ty
Hi Ty, I just tried that (saw it on http://weblogs.asp.net/jgalloway/archive/2005/10/15/427577.aspx) but it kicked a 'The server tag is not well formed' error on the datasource declaration line.
David HAust
And just to make sure I hadn't screwed up something else with the datasource tag, I re-gigged the SQL to use an Integer in the WHERE instead of a string and it worked perfect. Doesn't return the data I need but proved that the datasource declaratiion and SQL weren't busted.
David HAust
+2  A: 

Why don't you define this WHERE clause as a const in your codebehind. Define:

protected const string SELECTCLAUSE = 
"SELECT Blah FROM TableName WHERE (StringField = '{0}')";

Then your SelectCommand property would be:

SelectCommand='<%# Eval("Bar", SELECTCLAUSE ) %>'
Keltex
Thanks Keltex, that works. And please don't take offence, but there has to be a more elegant solution. It just feels kind of 'brute force'-ish to be doing it like that. Anyone else have any other ideas?
David HAust
+2  A: 

Don't forget that a .aspx page is simply XML. You just escape the quotes as you normally would.

For example:

<asp:Repeater ID="repeatTheLabel" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" Text="<%# Eval(&quot;Id&quot;, &quot;This is item '{0}'.&quot;) %>" runat="server" />
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

When the above expression is databound the value between <%# and %> becomes:

Eval("Id", "This is item '{0}'.")

...which produces on the HTML page as output when databound with an array of objects with "Id" property values from 1 to 5:

This is item '1'.
This is item '2'.
This is item '3'.
This is item '4'.
This is item '5'.

Aydsman
Hi Aydsman, I have tried using " and this technique doesn't work. The page kicks 'Compiler Error Message: BC32017: Comma, ')', or a valid expression continuation expected'. I could be wrong, but I believe that the ASP.NET rendering engine must convert it into a quote earlier than required.
David HAust
That's strange David. The label code I posted there was tested and worked fine.
Aydsman
I think it's the fact that I'm doing it inside an Eval format string that is causing the problem. Could it be that the Eval is converting the '"' into a quotes and then the ASP.NET Rendering engine comes along and kicks the error? I'm not sure.
David HAust
David, did you remember to change the quotes around the attribute value (i.e. change SelectCommand='<%# to be SelectCommand="<%# ) when you escaped the quotes?
Aydsman
Aydsman, Nice one, that fixed it. I hadn't thought to swap the outside and inside quotes like that.
David HAust
Glad to hear it, David. I'd recomend you consider following the advice from @Rune and removing your need to do this altogether for a better architecture.
Aydsman
+2  A: 

Store your sql queries in properties in your Page class. Not only does it work :-) but it makes your code easier to read and maintain.

Oh, and you should use parameters in your queries instead of doing string replacements. That will solve the problem by removing the need for single quotes.

Rune Grimstad