如何在配置的datalist中的select查询中申请循环?

时间:2012-11-25 04:10:03

标签: c# asp.net sql-server-2008

我用

创建了一个datalist
GridLines="Both" RepeatDirection="Horizontal" BorderColor="Black" 
                    BorderStyle="None" CellPadding="5" CellSpacing="1" RepeatColumns="6">

我还创建了数据库,其中包含productid的列,这是我表的主键 所以在这里我插入他们在水平列的数据列表中插入的项目,(增加productid的值),并且当它的限制超过6时,创建了一个新行。但我每次只想要12个项目,即只有2个行。

我在页面的页脚模板中添加了两个链接按钮。我希望当我点击下一个按钮时,项目显示从产品ID = 13到24的数量,依此类推,直到count(n)。

我已经使用SqlDataSource1配置了我的datalist,并希望像这样使用查询语句

 SelectCommand="SELECT * FROM Products where ProductId>=n and ProductId<=m order by [Sports Name]"

我在页面加载时声明m = 1和n = 12  但是当我运行我的页面时发生以下错误

Invalid column name 'n'.
Invalid column name 'm'.

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: Invalid column name 'n'.
Invalid column name 'm'.

现在请帮我在我的项目中进行这种编码。

1 个答案:

答案 0 :(得分:0)

尝试string.Format

SelectCommand = string.Format("SELECT * FROM Products where ProductId={0} and ProductId={1} order by [Sports Name]", n, m);
相关问题