我想按价格对下拉列表进行排序,但是它不起作用。我有如下错误:
'='附近的语法不正确。
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:
System.Data.SqlClient.SqlException:'='附近的语法不正确。资源
错误:第72行:reader = cmd.ExecuteReader();
这是我的代码
新来港人士.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;
}
}
新来港定居人士
<%@ 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>
</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>
最佳答案
您的选择语句是错误的。您在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
关于c# - 无法按价格对下拉列表进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13902520/