In the previous essay: SqlDataReader read the paging data, PageCount are you swollen?
There's a very troubling problem: SqlDataReader executing a stored procedure with an output parameter always gets no correct value for the output parameter. Here, the solution and analysis process are listed to provide a solution for small partners who are experiencing the same problem.
Read paging data in SqlDataReader, PageCount are you swollen? In the comments, Bo friends proposed will
PARAM[2]. Direction = ParameterDirection.Output;
This is a part of the reason that the sentence code is placed before executing the query. According to the way the Bo friends provided I tried, but still have a problem:
PARAM[2]. Direction = ParameterDirection.Output;
using (SqlDataReader reader = Sqlhelper.executereader (sql, CommandType.StoredProcedure, param))
{while
( Reader. Read ())
{
list. ADD (Convertreadertostudentmodel (reader));
}
Assign a value to an output parameter
PageCount = (int) param[2]. Value;
Error:
This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/
This reason, primarily must be param for empty, what is the cause of it?
After debugging found that the original culprit in the SqlHelper
Before the execution of CMD. Parameters.clear ();
After execution:
Dbparametercollention count is 0, that is, there are no arguments in the collection to add, so the error "set object reference to an instance of object" appears.
Know the reason is wrong in the place, know how to change. By annotating the sentence, you can get the value of the output parameter.
Results:
But many sqlhelper on the internet have a lot of parameters of the situation, it will be encountered in the future to modify the SqlHelper, certainly not, encounter this kind of execution with output parameters of the stored procedures, or a dataset better, in short, each has its own benefits.
This problem has tortured me for a week, today finally solved, very happy.
Suddenly think of a sentence, share: "Learning not to share for the purpose, is bullying ...";
Author: wolfy
Source: http://www.cnblogs.com/wolf-sun/