无法按价格对下拉列表进行排序

时间:2012-12-16 15:01:08

标签: c# asp.net sqldatareader

我想按价格对下拉列表进行排序但不起作用。我有如下错误:

  

'='附近的语法不正确。

     

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

     

异常详细信息:   System.Data.SqlClient.SqlException:'='附近的语法不正确。资源   错误:第72行:reader = cmd.ExecuteReader();

这是我的代码

New Arrivals.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class NewArrivals : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        if(IsPostBack)
        {
            bindDropDownList();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList();
    }
    public void bindDropDownList()
    {
        DropDownList1.DataTextField = "price";
        DropDownList1.DataSource = getReader();
        DropDownList1.DataBind();

        DropDownList1.Items.Insert(0, new ListItem("-Select-"));
        DropDownList1.Items.Insert(1, new ListItem("Price - Highest to Lowest"));
        DropDownList1.Items.Insert(2, new ListItem("Price - Lowest to Highest"));

    }
    public SqlDataReader getReader()
{
    SqlDataReader reader = null;
    DataTable DataList1 = new DataTable();

    if(DropDownList1.Text == "-Select-")
    {
        SqlConnection myConnect = new SqlConnection();
        myConnect.ConnectionString = ConfigurationManager.ConnectionStrings["ProductCS"].ConnectionString;

        string strCommandText ="SELECT * FROM [tb_ListPdts] WHERE newPdt=1";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.CommandText = strCommandText;
        cmd.Connection = myConnect;

        myConnect.Open();
        reader = cmd.ExecuteReader();
        DataList1.Load(reader);
        myConnect.Dispose();
        cmd.Dispose();

    }
    else if (DropDownList1.SelectedValue == "Price - Highest to Lowest")
    {
        SqlConnection myConnect = new SqlConnection();
        myConnect.ConnectionString = ConfigurationManager.ConnectionStrings["ProductCS"].ConnectionString;

        string strCommandText = "SELECT [image], [productName], [price], [newPdt] FROM [tb_ListPdts] WHERE newPdt==1 ORDER BY price desc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.CommandText = strCommandText;
        cmd.Connection = myConnect;

        myConnect.Open();
        reader = cmd.ExecuteReader();
        DataList1.Load(reader);
        myConnect.Dispose();
        cmd.Dispose();

    }

    else if (DropDownList1.DataTextField == "Price - Lowest to Highest")
    {
        SqlConnection myConnect = new SqlConnection();
        myConnect.ConnectionString = ConfigurationManager.ConnectionStrings["ProductCS"].ConnectionString;

        string strCommandText = "SELECT [image], [productName], [price], [newPdt] FROM [tb_ListPdts] WHERE newPdt==1 ORDER BY price asc";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.CommandText = strCommandText;
        cmd.Connection = myConnect;

        myConnect.Open();
        reader = cmd.ExecuteReader();
        DataList1.Load(reader);
        myConnect.Dispose();
        cmd.Dispose();

    }
    return reader;
}
}

New Arrivals.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="NewArrivals.aspx.cs" Inherits="NewArrivals" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        .style2
        {
            width: 80%;
        }
    </style>
    </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p id="product">New Products</p>
            <hr />

    <br />

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
        <asp:ListItem>-Select-</asp:ListItem>
            <asp:ListItem>Price - Highest to Lowest</asp:ListItem>
            <asp:ListItem>Price - Lowest to Highest</asp:ListItem>
    </asp:DropDownList>
    <br />

    <br />
    <table class="style2" id="newTable" rules="groups">
        <tr>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" 
                    BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
                    CellSpacing="2" GridLines="Both" 
                    RepeatColumns="3" RepeatDirection="Horizontal">
                    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                    <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                    <ItemTemplate>
                        <asp:Image ID="Image1" ImageUrl= '<%# Eval("image") %>'
                            runat="server" Height="180px" Width="230px" />
                        <br />
                        <asp:Label ID="productNameLabel" runat="server" 
                            Text='<%# Eval("productName") %>' />
                        <br />
                        Price: $
                        <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
                        <br />
                        <asp:Label ID="newPdtLabel" runat="server" Text='<%# Eval("newPdt") %>' Visible="False" />
                        <br />
                        <br />
                    </ItemTemplate>
                    <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                </asp:DataList>
            </td>
        </tr>
    </table>
    </asp:Content>

1 个答案:

答案 0 :(得分:3)

您的select语句错误。您在where子句中使用等于==的比较器。 SQL Server T-SQL不使用C风格的等号,而是使用单个=运算符。

更新此声明

FROM [tb_ListPdts] WHERE newPdt==1 ORDER BY price asc

使用单个=运算符,如下所示:

FROM [tb_ListPdts] WHERE newPdt=1 ORDER BY price asc