未将对象引用设置到对象的实例

nightmaple 2010-09-30 01:27:36
运行到_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString()); 时提示未将对象引用设置到对象的实例。怎么解决?

            using (SqlConnection sqlcon = new SqlConnection("server=.;database=Platform;user id=reportuser;password=reportuser"))
{
using (SqlCommand sqlcom = new SqlCommand())
{
sqlcom.Connection = sqlcon;
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.CommandText = "getData";
sqlcom.Parameters.AddWithValue("@PageSize", SqlDbType.Int).Value = int.Parse(this.txtPageSize.Text);
sqlcom.Parameters.AddWithValue("@PageIndex", SqlDbType.Int).Value = _CurPage;
sqlcom.Parameters.AddWithValue("@TableName", SqlDbType.VarChar).Value = _spName;
sqlcom.Parameters.AddWithValue("@SelField", SqlDbType.VarChar).Value = _SelField;
sqlcom.Parameters.AddWithValue("@ParaField", SqlDbType.VarChar).Value = _ParaStr;
sqlcom.Parameters.AddWithValue("@SortField", SqlDbType.VarChar).Value = _ReturnField;
sqlcom.Parameters.Add("@TotalCount", SqlDbType.VarChar, 255).Direction = ParameterDirection.Output;

sqlcon.Open();
sqlcom.ExecuteReader();
_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString()); //未将对象引用设置到对象的实例。

if (ds.Tables["Data"] != null)
ds.Tables.Remove("Data");
using (SqlDataAdapter sqladp = new SqlDataAdapter(sqlcom))
{
sqladp.Fill(ds, "Data");
this.dataGridView1.DataSource = ds.Tables["Data"];

if (this.chkPage.Checked)
{
_TotalPage = _TotalRecord / int.Parse(this.txtPageSize.Text);
if (_TotalRecord % int.Parse(this.txtPageSize.Text) > 0)
_TotalPage++;
}
}
}
}
...全文
112 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nightmaple 2010-09-30
  • 打赏
  • 举报
回复
问题解决了,就是1楼说的那样,要先关掉SqlDataReader才能取值,谢谢大家帮忙,结贴了。

                    sqlcon.Open();
SqlDataReader reader = sqlcom.ExecuteReader();
reader.Close();
_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString());
nightmaple 2010-09-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 qq346127416 的回复:]
sqlcom.Parameters.Add("@TotalCount", SqlDbType.VarChar, 255).Direction = ParameterDirection.Output;

"@TotalCount" 存储过程里不是int么
[/Quote]

是int型,我打错了,改成int型还是报那个未将对象引用设置到对象的实例。

我想知道既查出数据放到DataSet(需要的页的记录,如20条),同时又得到所有满足条件的记录数(这里是一个总数,用Return或输出参数返回,如69)要怎么做。
qq346127416 2010-09-30
  • 打赏
  • 举报
回复
sqlcom.Parameters.Add("@TotalCount", SqlDbType.VarChar, 255).Direction = ParameterDirection.Output;

"@TotalCount" 存储过程里不是int么
K0528 2010-09-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 symbol_bc 的回复:]
楼主是想读数据是吧,这代码不对啊


C# code

sqlcon.Open();
sqlcom.ExecuteReader();
_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString()); //未将对象引用……
[/Quote]

應該正解吧~
nightmaple 2010-09-30
  • 打赏
  • 举报
回复
怎么没人复了?

我需要的就是,同时要查询出记录,又要有一个返回值,用一个存储过程做不做得到?

存储过程的用途是
根据参数pagesize和pageindex,返回第pageindex页的pagesize条记录,
同时返回所有满足查询条件的所有记录条数。

请高人指点~
nightmaple 2010-09-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 symbol_bc 的回复:]
楼主是想读数据是吧,这代码不对啊


C# code

sqlcon.Open();
sqlcom.ExecuteReader();
_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString()); //未将对象引用……
[/Quote]

我的存储过程中即查出数据,又要返回一个值(用Return方式或Output参数方式都报同一个错)
这样可以做到吗?

存储过程如下
if object_id('getData','p') is not null
drop proc getData
go

create proc getData
(
@PageSize int,
@PageIndex int,
@TableName varchar(20),
@SelField varchar(2000),
@ParaField varchar(2000),
@SortField varchar(200),
@TotalCount int output
)
as

declare @SQLStr nvarchar(4000)
--set @SQLStr='select top '+convert(varchar(20),@PageSize)+' Row_number() over(order by getdate()) as F0,'+@SelField+' from '+@TableName+' where 1=1 and '+@ParaField+'
--and '+@KeyField+' in (select top '+convert(varchar(20),@PageSize*@PageIndex)+' '+@KeyField+' from '+@TableName+' where 1=1 and '+@ParaField+' order by '+@SortField+' desc)
--order by '+@SortField

set @SQLStr='with tt as (
select top '+convert(varchar(20),@PageSize*@PageIndex)+' Row_number() over(order by '+@SortField+') as F0,'+@SelField
+' from '+@TableName+' where 1=1 and '+@ParaField+' order by '+@SortField
+') select * from tt where F0 between '+convert(varchar(20),@PageSize*(@PageIndex-1)+1)+' and '+convert(varchar(20),@PageSize*@PageIndex)+' order by '+@SortField

EXEC sp_executesql @SQLStr

--declare @TotalCount int
set @SQLStr='select @iRet=count(1) from '+@TableName+' where 1=1 and '+@ParaField
EXEC sp_executesql @SQLStr,N'@iRet int output',@TotalCount output
return --@TotalCount
go

declare @out int
exec getData 20,1,'sp7101','F1,F3,F5,F6,F11,F10,F4,F7,F8,F9','F5=''200''','F1', @out output
print @out
nightmaple 2010-09-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
返回值 必须是在
dr.close()以后才可以获取的
[/Quote]

dr是什么?
还是sqlcom的?
symbol_bc 2010-09-30
  • 打赏
  • 举报
回复
楼主是想读数据是吧,这代码不对啊


sqlcon.Open();
sqlcom.ExecuteReader();
_TotalRecord = int.Parse(sqlcom.Parameters["@TotalCount"].Value.ToString()); //未将对象引用设置到对象的实例。

//改为
sqlcon.Open();
SqlDataReader reader = sqlcom.ExecuteReader();
while(reader.Read())
{
string res = int.Parse(reader["TotalCount"].ToString());
}

wuyq11 2010-09-30
  • 打赏
  • 举报
回复
返回值 必须是在
dr.close()以后才可以获取的

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧