SelectCommand错误:Validation(ASP.NET)如果将此属性值括在引号中,则引号必须匹配

时间:2018-08-15 10:35:14

标签: sql asp.net sql-server

我在sqlserver中进行了此查询,当我在项目中将第一行显示为绿色时,标题出现错误,在第三行之后,它似乎不再位于SelectCommand内,因为它是红色字母。我认为这与<或''有关。我该如何解决?谢谢

<asp:SqlDataSource ID="ds_order" runat="server" CancelSelectOnNullParameter="false"
                        ConnectionString="myconnection"
                        SelectCommand=
                        "SELECT SUM(Total) as Totais, DateCreated, 
                         CASE 
                             WHEN DAY(DateCreated) <=15 THEN CAST(YEAR(DateCreated) AS VARCHAR) + '-' + CAST(MONTH(DateCreated) AS VARCHAR) + '-1' 
                             ELSE CAST(YEAR(DateCreated) AS VARCHAR) + '-' + CAST(MONTH(DateCreated) AS VARCHAR) + '-16' 
                         END AS Month              
                         FROM Orders
                         WHERE ReferenceID = @refid 
                         GROUP BY DateCreated" ProviderName="System.Data.SqlClient">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddl_ref_type" Name="refid" />
        </SelectParameters>
        </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:2)

您需要用XML实体引用&quot;&lt;替换单引号,并将其替换为查询中较少的内容,以使XML的格式正确:

<asp:SqlDataSource ID="ds_order" runat="server" CancelSelectOnNullParameter="false"
                        ConnectionString="myconnection"
                        SelectCommand=
                        "SELECT SUM(Total) as Totais, DateCreated, 
                         CASE 
                             WHEN DAY(DateCreated) &lt;=15 THEN CAST(YEAR(DateCreated) AS VARCHAR) + &quot;-&quot; + CAST(MONTH(DateCreated) AS VARCHAR) + &quot;-1&quot; 
                             ELSE CAST(YEAR(DateCreated) AS VARCHAR) + &quot;-&quot; + CAST(MONTH(DateCreated) AS VARCHAR) + &quot;-16&quot; 
                         END AS Month              
                         FROM Orders
                         WHERE ReferenceID = @refid 
                         GROUP BY DateCreated" ProviderName="System.Data.SqlClient">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddl_ref_type" Name="refid" />
        </SelectParameters>
</asp:SqlDataSource>