I want to sort a drop down list by price but it doesn’t work. I have the error like the following:
Incorrect syntax near ‘=’.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:
System.Data.SqlClient.SqlException: Incorrect syntax near ‘=’. Source
Error: Line 72: reader = cmd.ExecuteReader();
Here are my codes
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>
</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>
Your select statement is wrong. You are using equal comparator as
==in your where clause. SQL Server T-SQL does not use C-style equals, instead a single=operator is used.Update this statement
to use a single
=operator as below: