Error analysis of SqlDataReader execution with output parameter stored procedure

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.