Solved

How to open connection for datareader

Posted on 2014-11-21
6
101 Views
Last Modified: 2014-11-21
ExecuteReader requires an open and available Connection. The connection's current state is closed.

protected void fill_ddlCodes()
        {
            using (SqlConnection connCode = new SqlConnection(str1964))
            {
                using (SqlCommand cmdCode = new SqlCommand("Select * from view_tbl_codes", connCode))
                {
                    SqlDataReader drCodes;
                    //Getting exception error here...
                    drCodes = cmdCode.ExecuteReader();
                    
                    List<string> lstCodes = new List<string>();
                    foreach(DataRow dr in drCodes)
                    {
                        lstCodes.Add(drCodes["Code"].ToString());
                    }
                    ddlCodes.DataSource = lstCodes;
                    ddlCodes.DataValueField = "Code";
                    ddlCodes.DataTextField = "Code";
                    ddlCodes.DataBind();
                }
            }            
        }

Open in new window

0
Comment
Question by:Scarlett72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40457764
Change this line:

drCodes = cmdCode.ExecuteReader();

...to this:

connCode.Open();
drCodes = cmdCode.ExecuteReader()

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40457777
You should also have a close connection before leaving the code, something like this.

connCode.Close();
ddlCodes.DataSource = lstCodes;
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40457780
@Fernando

There's already a "close connection" in that code  ; )
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40457786
@Kaufmed missed the using statement, thanks for the update.
0
 

Author Comment

by:Scarlett72
ID: 40457822
Hi Fernando / Kauffmed, thank you both again for your assistance.  The 'open' method on the datareader worked, but I thought because I put my reader in a connection dispose method I didn't have to open or close the connections...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40457837
You don't have to close it--the using takes care of that--but you do have to open it. If you think about it, it kind of makes sense:  Once you dispose of the connection object, you really don't need it anymore, so you implicitly shouldn't need the underlying connection TCP connection either; but in terms of opening the connection, you may need to do some setup first, potentially a lengthy operation. From a code perspective, you're not always guaranteed at what exact point you will open a connection, but you should always know when you no longer need it.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSS question 16 59
.NET Redemption/Outlook Examples 3 24
async questions 5 46
Intermittent OleDbConnection Error 20 44
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question