C# 无法按价格对dropdownlist进行排序

C# 无法按价格对dropdownlist进行排序,c#,asp.net,sqldatareader,C#,Asp.net,Sqldatareader,我想按价格对下拉列表进行排序,但不起作用。我有如下错误: “=”附近的语法不正确 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 例外情况详情: System.Data.SqlClient.SqlException:靠近“=”的语法不正确。来源 错误:第72行:reader=cmd.ExecuteReader() 这是我的密码 New-Arrivals.aspx.cs using System; using System.Collect

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

“=”附近的语法不正确

描述:执行当前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;
}
}
新来者。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>

.style2
{
宽度:80%;
}
新产品



-挑选- 价格-从最高到最低 价格-从最低到最高



价格:$



您的select语句错误。您正在where子句中使用相等比较器作为
=
。SQL Server T-SQL不使用C-style equals,而是使用单个
=
运算符

更新此语句

FROM [tb_ListPdts] WHERE newPdt==1 ORDER BY price asc
要使用单个
=
运算符,如下所示:

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

欢迎来到stackoverflow。请尽量少发代码,只显示你的问题。异常会向您显示错误所在的确切代码行。我们不需要你所有的aspx与风格等。