Sunday, February 12, 2012

ASP.Net and MS SQL

Dreamweaver MX and ASP.Net and MS SQL

I am displaying a page of lists of addresses based on state.

.../results.aspx?state=id

I want to add a little "re-sort" links. For example: sort by | name | city | style

.../results.aspx?state=id&sort=city
(if sort is missing default to "sort=name")

But I a having a problems... here is part of my code, following dreamwevers standards for ASP.Net -

CommandText='<%# "SELECT * FROM tblRestaurants WHERE ""State"" = @.State ORDER BY @.Sort ASC" %>'

<parameters>
<parameter name="@.State" value='<%# IIf((Request.QueryString("state") <> Nothing), Request.QueryString("state"), "") %>' type="VarChar" />
<parameter name="@.Sort" value='<%# IIf((Request.QueryString("sort") <> Nothing), Request.QueryString("sort"), "name") %>' type="VarChar" />
</parameters
But "@.Sort" is causing errors, I think @. is a reserved keyword in MS SQL (?),
here is the error message -

System.Data.SqlClient.SqlException: The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.

I have tried to change @.Sort about a dozen different names "MMSort", "urlSort" etc.
here is the error message -

System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'urlSort'.

One side note, I displaying the results in a repeating datalist, a datagrid is out.
A rough draft version without working sort is at
http://www.jbmcgregor.com/cira/results.aspx?state=id

Any suggestion, ideas or help would be extremely appreciated. Thanks.

DanielIt would help if you provide the actual sql query that gets executed. Use Try...Catch to catch the exception and use response.write to figure out exactly what was the sql command that gets executed. Probably it is a systax error.

No comments:

Post a Comment