Question

I am using an SqlDataSource control with c#. ASP.NET 3.5 Visual Studio 2013.

I have two drop down list controls with values for sorting using ORDER BY with the following order by clause in the SqlDataSource query...

select ... ORDER BY @sortfield @order

@sortfield is the name of a table field from the value in the DropDownList as a Filterparameter and @order is ASC or DESC as selected from the other DropDownList value for an SqlDataSource FilterParameter.

It clearly substitutes in the values at runtime, because the error returned is:

Syntax error: Missing operand after 'ASC' operator

or else, if the FilterParams field is left empty and the variables only put in the query

incorrect syntax after @order operator

If it does not return an error - then the result set is empty (but remove the order by clause and the query returns a result set!)

I have tried adding a semicolon and various quotes and brackets, but to no avail.

I have also tried using the FilterExpression set to {0} {1} and ending the query with ORDER BY - but this is worse.

Why the syntax error?? Is there some problem with using FilterParams with an ORDER BY clause as above?

Here is the SqlDataSource code. Note I have not used a filter expression here, but that seems to be worse:

<asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
    OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
    SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')

ORDER BY [@sortfield] [@order]">

Here it is with a filter expression using {0} {1}, but this chokes on the second parameter with: Syntax error: Missing operand after 'ASC' operator.

So the FilterParams values are going in, but the parser doesn't like the end of the expression. I don't know why.

Again I am using the value field from the drop down lists, but the wizard in the SqlDataSource FilterParameters property only seems to allow the use of value - not text:

<asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
    OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
    SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')

ORDER BY" FilterExpression="{0} {1}">

This one uses the FilterParams property as above, but with [@sortfield] [@order]. IOt gives Syntax error: Missing operand after '[@order]' operator:

   <asp:SqlDataSource ID="sqlds_copy_1" runat="server" ConnectionString="<%$ ConnectionStrings:abConnectionString %>"
    OnInit="sqlds_copy_1_Init" OnPreRender="sqlds_copy_1_PreRender"
    SelectCommand="SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name FROM ab_pages INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') AND (ab_pages.page_url LIKE '/pa_home.aspx')

ORDER BY" FilterExpression="[@sortfield] [@order]">

THE DROP DOWN LISTS CONTROLS:

 <asp:DropDownList ID="ddl_SortField" runat="server" AppendDataBoundItems="True">
            <asp:ListItem Value="ab_genericentry.title" Selected="True">Entry Name</asp:ListItem>
            <asp:ListItem Value="ab_genericentry.date">Entry Date</asp:ListItem>
</asp:DropDownList>
        <asp:DropDownList ID="ddl_Order" runat="server">
            <asp:ListItem Value="DESC">↓</asp:ListItem>
            <asp:ListItem Value="ASC" Selected="True">↑</asp:ListItem>
        </asp:DropDownList>
Was it helpful?

Solution

For starters you may want to surround @sortfield with square brackets to eliminate the possibility of there being spaces in the column name causing a syntax error (Missing operand after 'Operator Name' operator).
Also, are you setting the parameter using the correct value from the dropdown box (ie, Text instead of Value)? Unfortunately, without being able to see more of the code it's hard to give more of a direct answer.

Code-behind example:

1) add change event to sort dropdown

asp:DropDownList ID="ddl_SortField" runat="server" AppendDataBoundItems="True" OnSelectedIndexChanged="ddl_SortField_SelectedIndexChanged"

2) code-behind

   protected void ddl_SortField_SelectedIndexChanged(object sender, EventArgs e)
   {
       var sqlCommand = string.Format("SELECT ab_genericentry.title, ab_genericentry.heading, ab_genericentry.body, " +
           "ab_genericentry.time, ab_genericentry.date, ab_genericentry.header, ab_genericentry.synopsis, " +
           "ab_genericentry.abstract, ab_genericentry.footer, ab_genericentry.introduction, ab_genericentry.conclusion, " +
           "ab_genericentry.headline, ab_genericentry.release_date, ab_copyinstances.control_name " +
           "FROM ab_pages " +
           "INNER JOIN ab_sites ON ab_pages.site_id = ab_sites.id " +
           "INNER JOIN ab_copyinstances ON ab_pages.id = ab_copyinstances.page_id AND ab_sites.id = ab_copyinstances.site_id " +
           "INNER JOIN ab_genericentry ON ab_copyinstances.id = ab_genericentry.copy_instance_id " +
           "WHERE (ab_sites.site_name LIKE 'philaxiom') AND (ab_copyinstances.control_name LIKE 'dl_copy_1') " +
           "AND (ab_pages.page_url LIKE '/pa_home.aspx')" +
           "ORDER BY [{0}] {1}", ddl_SortField.SelectedValue.ToString(),
           ddl_Order.SelectedValue.ToString());

       sqlds_copy_1.SelectCommand = sqlCommand;
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top