【发布时间】:2013-08-07 09:41:15
【问题描述】:
我遇到了简单的问题,但我不知道问题出在哪里:/ 所以在我的GridView 中,我正在使用 ObjectDataSource 和本教程中的自定义分页 http://www.codedigest.com/Articles/ASPNET/180_Custom_GridView_Paging_with_ObjectDataSource_Control_with_ASPNet_20.aspx
这是我的 aspx 标记:
<asp:ObjectDataSource ID="ObjectDataSource2"
runat="server"
onselecting="ObjectDataSource2_Selecting"
EnablePaging="true"
SelectCountMethod="GetItemsCount"
SelectMethod="BindItems"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows"
TypeName="eSova.Utilities.RecordUtilities"
>
以及调用的方法:
public static DataTable BindItems(int category,int search,int startRowIndex,int maximumRows)
{
DataTable table = new DataTable();
using (SqlConnection connection = new SqlConnection())
{
ConnectionUtilities.OpenConnection(connection);
SqlTransaction transaction = connection.BeginTransaction();
try
{
SqlCommand command = new SqlCommand("GetItems",connection,transaction);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@RowIndex", SqlDbType.Int, 4).Value = startRowIndex;
command.Parameters.Add("@MaxRows", SqlDbType.Int, 4).Value = maximumRows;
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
transaction.Commit();
}
catch
{
transaction.Rollback();
}
}
return table;
}
我的存储过程运行良好,并从表中返回所有项目。
但是当我分析代码时,我在返回时遇到断点,并且表变量没有记录。不知道哪里出了问题。
更新:
create proc [dbo].[GetItems](@RowIndex int,@MaxRows int)
as
declare @StartRows int
declare @EndRow int
set @StartRows=(@RowIndex+1)
set @EndRow=(@StartRows+@MaxRows)
select *
from ( select id, name, filepath, descript, itemlanguage,
filetypeid, ROW_NUMBER() over (ORDER by id)as row FROM Items)as NumberesItems
where row between @StartRows and @EndRow
【问题讨论】:
-
你为什么要在精选集上唱
SqlTransaction?你有例外吗?如果是,那么您当前的代码正在吞噬该异常。使用 catch 块catch 异常,以及在发生错误时您可能想要执行的任何其他操作。 -
如果您直到返回行才中断程序,则很可能上面发生了一些事情,引发了异常,然后撤消了选择。在方法的开头放置一个断点,并单步执行代码以查看发生了什么。
-
好点,SqlTransaction 它只是我重新编码的另一种方法的混乱代码,但是如果删除 SQLTransaction 我得到的结果仍然相同!没有错误没有例外,但变量表为空 ={} 所以我猜适配器也是空的,并且 StoredProcedure 根本不调用
-
您是否已单步执行代码?您表明存储过程正常工作 - 您在 SQL Server Management Studios 中尝试过吗?
-
是的,我执行了我的代码。但是我没有得到它,我在SMS中尝试为storedProcedure设置maximumRows,但我为aprametrs 0和20设置了确切的值。我的程序正在更新中
标签: asp.net stored-procedures datatable objectdatasource