Datareader associated with this command already opened must be disabled first. Personal understanding of this exception!

Source: Internet
Author: User

We will share with you today that "datareader associated with this command already opened must be disabled first ." To understand this exception, first declare the following:

1. It may be a bit elementary to explain. I hope that the experts will not "spray" me, because I know that not everyone is a master, and I am afraid that the experts will say that I have installed 13;

2. If there is anything wrong with it, I hope you can point out that you must learn it modestly;

Let's start with the following:

I also checked a lot of information online when I encountered this problem. It seems that I didn't find any good solution. Then I started debugging.Program, I found that sometimes the operation does not report errors, sometimes the operation reports errors, this is very tangled, but later I found that I put ASP. net Development Server-port XXX exits, and then runs the program, no error is reported... I thought it was okay. But I found a problem later. As long as the port is not closed or the page is constantly refreshed during the program running, an error will be reported "The datareader associated with this command has been opened, you must first disable it."

I got angry. I started to put n breakpoints in the program and debug them in one step. I didn't find that I used the datareader object. That's strange. Why didn't I use the datareader object, but does it include such errors? I am speechless. After repeated scrutiny, I finally found a solution. It turns out that this exception is not necessarily related to datareader, but may also be related to the connection object!

See the following:Code:

 Private   Static Sqlconnection;
Public Static Sqlconnection
{
Get
{
String Sqlconnectionstring = system. configuration. configurationmanager. connectionstrings [ " Tandytang " ]. Tostring ();
If (Sqlconnection = Null )
{
Sqlconnection = New Sqlconnection (sqlconnectionstring );
Sqlconnection. open ();
}
Else If (Sqlconnection. State = system. Data. connectionstate. Closed)
{
Sqlconnection. open ();
}
Else If (Sqlconnection. State = system. Data. connectionstate. Broken)
{
Sqlconnection. Close ();
Sqlconnection. open ();
}
Return Sqlconnection;
}
}
/// <Summary>
/// Sqlgetdatatable
/// <Param name = "proc"> </param>
/// <Param name = "type"> </param>
/// <Param name = "Param"> </param>
/// <Param name = "count"> </param>
/// <Returns> Datatable </Returns>
Public Static Datatable sqlgetdatatable ( String Proc, commandtype type, String [] Param, Out Int Count)
{
Dataset DS = New Dataset ();
Using (Sqlcommand cmd = New Sqlcommand (Proc, sqlconnection ))
{
Sqlparameter [] myparms =New Sqlparameter [ 2 ];

Myparms [ 0 ] = New Sqlparameter ( " @ ID " , Sqldbtype. Int, 4 );
Myparms [ 0 ]. Value = paramvalue [ 0 ];
Myparms [ 1 ] =New Sqlparameter ( " @ Name " , Sqldbtype. varchar, 50 );
Myparms [ 1 ]. Value = paramvalue [ 1 ];
Foreach (Sqlparameter Parameter In Myparms)
{
Cmd. Parameters. Add (parameter );
}
Cmd. commandtype = type;
Sqldataadapter da = new Sqldataadapter (CMD );
Da. Fill (DS );
}
Return DS. Tables [ 0 ];
}

The above code, I believe, in the eyes of ordinary people, should be okay, I think there is no problem. The first code is to create a data connection and open a database connection. The second code is to create a sqlcommand, where sqlconnection is used as the first parameter, which is the key to the problem.

If you run the sqlgetable able () method once on the page, there will be no problem. If you refresh the page without stopping, the page will be "A datareader associated with this command has been opened, you must first disable it." This exception may not occur.The red part of the code indicates the error message.)

After my thinking, I finally found a solution. The solution is as follows:

 Private   Static   String Connectionstring = system. configuration. configurationmanager. connectionstrings [ "  Tandytang  " ]. Connectionstring;
/// <Summary>
/// Sqlgetdatatable
/// <Param name = "proc"> </param>
/// <Param name = "type"> </param>
/// <Param name = "Param"> </param>
/// <Param name = "count"> </param>
/// <Returns> Datatable </Returns>
Public Static Datatable sqlgetdatatable ( String Proc, commandtype type, String [] Param, Out Int Count)
{
Dataset DS = New Dataset ();
Using (Sqlconnection con = New Sqlconnection (connectionstring ))
{
Using (Sqlcommand cmd = New Sqlcommand (Proc, con ))
{
Sqlparameter [] myparms = New Sqlparameter [ 11 ];

Myparms [ 0 ] = New Sqlparameter ( " @ ID " , Sqldbtype. Int, 4 );
Myparms [ 0 ]. Value = paramvalue [ 0 ];
Myparms [ 1 ] = New Sqlparameter ( " @ Name " , Sqldbtype. varchar, 50 );
Myparms [ 1 ]. Value = paramvalue [ 1 ];
Foreach (Sqlparameter Parameter In Myparms)
{
Cmd. Parameters. Add (parameter );
}
Cmd. commandtype = type;
Using (Sqldataadapter da = New Sqldataadapter (CMD ))
{
Da. Fill (DS );
}
}
Return DS. Tables [ 0 ];
}
}

The above method does not use the sqlconnection method, but writes the sqlconnection object to using.

PS: I personally suggest you do not use the first method. It may be very troublesome. I suggest using the using () {} statement to put ADO. net has no objects in using () {}. I think everyone knows the benefits of using () {}, so I will not explain it too much. I wish you a pleasant write of code...

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.