Let's talk about the sqldatareader closure problem. The following error occurs: "The attempt to call fieldcount when the reader is disabled is invalid ")

Source: Internet
Author: User

When you use repeater to bind a data source, "fieldcount cannot be called when the reader is disabled. "Error.
I saw his Code The executereader method under the sqlhelper class is used, and a sqldatareader is returned for binding.
Public static sqldatareader executereader (commandtype primitive type, string plain text, Params sqlparameter [] partition parms) {sqlcommand cmd = new sqlcommand (); sqlconnection conn = new sqlconnection (conn_string ); // We use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandbehaviour. closeconnection will not work try {preparecommand (CMD, Conn, null, partition type, plain text, partition parms); sqldatareader RDR = cmd. executereader (commandbehavior. closeconnection); cmd. parameters. clear (); Return RDR;} catch {Conn. close (); throw;} Then, he wrote another class, getdata, using one of the methods to call sqlhelper.
Public sqldatareader getreader () {using (sqldatareader reader = sqlhelper. executereader (commandtype. storedprocedure, "getalluser", null) {return reader ;}} among them, getalluser is a stored procedure that queries all users without parameters, and binds the data source through the returned reader.
This error occurs because the reader has been disabled before reading the reader (the reason for using ). Therefore, if the data cannot be read, the error will be reported. Therefore, modify the call method:
Public sqldatareader getreader () {try {sqldatareader reader = sqlhelper. executereader (commandtype. storedprocedure, "getalluser", null); Return reader;} catch (exception) {return NULL ;}} in this way, no error is reported, and there is no problem with data binding on the UI Layer. However, a new problem arises: If sqldatareader is not manually disabled, will it cause the connection pool to reach the maximum value?
We noticed that commandbehavior is used in sqlhelper. closeconnection: "disabling sqldatareader will automatically close sqlconnection. "That is to say, whether to disable sqlconnection is performed on the premise of closing sqldatareader, or whether to manually close sqldatareader. What should I do if I want to return sqldatareader and disable it? Some netizens suggested that reader should be closed before return reader, that is, reader. Close ();
In fact, this is not the case. It will report the same error as the previous one. It has been disabled before reading data.
Commandbehavior. closeconnection is used to close the database connection. This is positive, but will it close sqldatareader together. That is to say, if commandbehavior. closeconnection is used, you do not need to manually close sqldatareader.
We use sqlhelper and then test it on the front-end webpage.

Protected void BIND () {sqlconnection conn = new sqlconnection (configurationmanager. connectionstrings ["constr"]. tostring (); Conn. open (); sqlcommand cmd = new sqlcommand ("getalluser", Conn); sqldatareader SDR = cmd. executereader (commandbehavior. closeconnection); repeater1.datasource = SDR; repeater1.databind (); response. write (SDR. isclosed. tostring () + "<br/>"); response. write (Conn. state. tostring ();} the output result is
True
Closed
This indicates that both sqlconnection and sqldatareader have been disabled.
If commandbehavior. closeconnection is removed, the output result is:
False
Open
Therefore, after commandbehavior. closeconnection is used, you do not need to manually close sqldatareader and sqlconnection. I heard that the purpose of Microsoft's commandbehavior. closeconnection was to solve the database shutdown problem.

ArticleTo learn IT network: http://www.xueit.com/html/2011-04/21-106291928620114692812765.html

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.