Exception details: System. InvalidOperationException: ExecuteReader requires an enabled and available connection. The current status of the connection is closed.

Source: Internet
Author: User

Exception details: system. invalidoperationexception: executereader requires an enabled and available connection. The current status of the connection is closed.

Source error:

Line 232: mycommand. commandtype = commandtype. text;
Line 233: sqldatareader DR = NULL;
Row 234: DR = mycommand. executereader (commandbehavior. closeconnection );//???????????????????????
Row 235:
Row 236: return Dr;

Source File: C:/inetpub/wwwroot/tragrammini/webtragmini/app_code/folder. CS row: 234

Stack trace:

[InvalidOperationException: ExecuteReader requires an enabled and available connection. The current status of the connection is closed.]
System. Data. SqlClient. SqlConnection. GetOpenConnection (String method) + 861892
System. Data. SqlClient. SqlConnection. ValidateConnectionForExecute (String method, SqlCommand command) + 9
System. Data. SqlClient. SqlCommand. ValidateCommand (String method, Boolean async) + 101
System. Data. SqlClient. SqlCommand. RunExecuteReader (CommandBehavior describehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) + 73
System. Data. SqlClient. SqlCommand. RunExecuteReader (CommandBehavior describehavior, RunBehavior runBehavior, Boolean returnStream, String method) + 32
System. Data. SqlClient. SqlCommand. ExecuteReader (CommandBehavior behavior, String method) + 122
System. Data. SqlClient. SqlCommand. ExecuteReader (CommandBehavior behavior) + 62
TangramMiniWeb. Folder. GetFolders () in c:/Inetpub/wwwroot/TragramMini/WebTragMini/App_Code/Folder. cs: 234
TangramMiniWeb. MailDesktop. BindFolderData () in c:/Inetpub/wwwroot/TragramMini/WebTragMini/Email/MailDesktop. aspx. cs: 29
TangramMiniWeb. MailDesktop. Page_Load (Object sender, EventArgs e) in c:/Inetpub/wwwroot/TragramMini/WebTragMini/Email/MailDesktop. aspx. cs: 23
System. Web. Util. CalliHelper. EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) + 15
System. Web. util. callieventhandlerdelegateproxy. Callback (Object sender, eventargs e) + 34
System. Web. UI. Control. onload (eventargs e) + 99
System. Web. UI. Control. loadrecursive () + 47
System. Web. UI. Page. processrequestmain (Boolean includestagesbeforeasyncpoint, Boolean includestagesafterasyncpoint

Source code:

Public sqldatareader getfolders ()
{

/// Create a link
Sqlconnection myconnection = new sqlconnection (constring );
If (myConnection. State = ConnectionState. Open)
MyConnection. Open ();
/// Define an SQL statement
String plain text = "SELECT * FROM Folders ";
SqlCommand myCommand = new SqlCommand (plain text, myConnection );

MyCommand. CommandType = CommandType. Text;
SqlDataReader dr = null;
Dr = myCommand. ExecuteReader (CommandBehavior. CloseConnection );//???????????????????????

Return dr;
}

Private string conString = ConfigurationManager. receivettings ["EngString"];

 

1. if (myConnection. State = ConnectionState. Open)

2. if (myConnection. State = ConnectionState. Closed)

3. CommandBehavior. CloseConnection: When executing this command, If you disable the associated DataReader object, the associated Connection object will also be closed.
Therefore, the link to the database depends on the DataReader object you have defined. The DataReader object obtained by using the GetFolders () function should be correct after use. Close ().
In addition, if (myConnection. state = ConnectionState. open) to if (myConnection. state = ConnectionState. closed.

 

4. Your if (myConnection. State = ConnectionState. Open) causes the program to skip the myConnection. Open () statement.
So no database connection is enabled.

 

Finally, my solution is:

My WEB. I wrote the config code in different headers, because this is a Chinese and English website. I separated the Chinese and English preparations, and I created another webeng. the configuration in English cannot be exported to the config file by default. All the above program errors are caused. Haha.

When I put the Chinese and English preparations into another file, all the database connections are back to normal.

 

 

 

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.