超奇怪问题,存储过程出数问题!!怪怪!!!!!
本人测试一个分页问题,但输出的值写到Web上时,出现这种怪异常现象。晕!
System.NullReferenceException: 未将对象引用设置到对象的实例。
采用SQL Server中示例数据库Northwind
存储过程如下:
if exists(select * from sysobjects where id=object_id('dbo.Usp_CustomersList'))
begin
drop procedure Usp_CustomersList
end
go
create procedure Usp_CustomersList
@PageRequest int,
@TotalPages int output
as
set nocount on
if((select count(*) from customers)=0)
begin
select @TotalPages=0
select @PageRequest=0
return 0
end
else
begin
select @TotalPages=ceiling(count(*)/9) from customers
end
if(@PageRequest<=1)
begin
select top 9 * from customers order by CustomerID
set @PageRequest=1
-- select @TotalPages=1
return 0
end
else
if(@PageRequest>@TotalPages)
set @PageRequest=@TotalPages
declare @RowCount int
set @RowCount=(@PageRequest*9)
exec('select * from
(select top 9 a.* from
(select top '+@RowCount+'* from customers order by
customerid) a
order by customerid desc) b
order by customerid')
return 0
输出的参数值如下:
private void databinder(int requestpage)
{
SqlConnection con = new SqlConnection("data source=mypc;initial catalog=northwind;user id=sa;password=weblogic");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Usp_CustomersList";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@PageRequest", SqlDbType.Int);
cmd.Parameters.Add("@TotalPages", SqlDbType.Int);
cmd.Parameters[0].Value = requestpage;
cmd.Parameters[0].Direction=ParameterDirection.Input;
cmd.Parameters[1].Direction=ParameterDirection.Output;
SqlDataReader sdr=cmd.ExecuteReader();
DataList1.DataSource=sdr;
DataList1.DataBind();
txtTotalPages.Text = cmd.Parameters[1].Value.ToString();
}
出错位置如下语句:
txtTotalPages.Text = cmd.Parameters[1].Value.ToString();
本人用了直接写SqlParameter来写的参数还是有这问题,这样写也有问题,用Win Form来写也有问题,在DOS下Console来写也有问题,但在Visual Basic 6下写没问题,原因何在?
麻烦大家指点指点!
还有C#中用数据绑定,不同时间出来不同的结果,而有时则没有DataBind();有时有,为什么?很是晕!