Connection must be closed first

Please could someone tell me why I am getting this error message?

Exception Details: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

Source Error:


Line 35:             MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(strSql, myConnection);
Line 36:             DataSet myDataSet = new DataSet();
Line 37:             myDataAdapter.Fill(myDataSet, "tbl_brands");
Line 38:             brands_Dlist.DataSource = myDataSet;
Line 39:             brands_Dlist.DataBind();
 


In my code I do nt have an open Connection at all, although I must be doing something wrong, but it is driving me crazy.  Thanks in advance and here is my complete code.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;

public partial class Default5 : System.Web.UI.Page
{
MySqlConnection myConnection = new MySqlConnection("server=localhost; user id=andy; password=xxxxxxxxx; database=penny_lane_takings; pooling=false;");
    public int Shop1Ind = 0;
    public int Shop2Ind = 0;
    public String IntSql;
    public DateTime SysT = new DateTime();
    protected void Page_Load(object sender, EventArgs e)
    {
        SysT = DateTime.Now.Date;
        SysToday.InnerHtml = "Today: "+String.Format("{0:D}", SysT);
        String strSql = "SELECT takings_date FROM days_takings WHERE shopname = 'shop1' AND takings_date ='" + SysT + "'";
        myConnection.Open();
        DataSet shop1DS = new DataSet();
        //if (shop1DS.Tables["days_takings"].Rows.Count == 0)
        MySqlDataAdapter shop1DA = new MySqlDataAdapter(strSql, myConnection);
shop1DA.Fill(shop1DS, "days_takings");
myConnection.Close();
//LoadData();

}


}
LVL 20
REA_ANDREWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

REA_ANDREWAuthor Commented:
Sorry this is the error

Exception Details: MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.

Source Error:


Line 27:         //if (shop1DS.Tables["days_takings"].Rows.Count == 0)
Line 28:         MySqlDataAdapter shop1DA = new MySqlDataAdapter(strSql, myConnection);
Line 29: shop1DA.Fill(shop1DS, "days_takings");
Line 30: myConnection.Close();
Line 31: //LoadData();
 

Source File: d:\home\Default\nwcms.co.uk\htdocs\john\pennylanetakings\index.aspx.cs    Line: 29
0
Ahmad1012Commented:
can you try running your code with the myConnection.Open();. You new code should look like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;

public partial class Default5 : System.Web.UI.Page
{
MySqlConnection myConnection = new MySqlConnection("server=localhost; user id=andy; password=xxxxxxxxx; database=penny_lane_takings; pooling=false;");
    public int Shop1Ind = 0;
    public int Shop2Ind = 0;
    public String IntSql;
    public DateTime SysT = new DateTime();
    protected void Page_Load(object sender, EventArgs e)
    {
        SysT = DateTime.Now.Date;
        SysToday.InnerHtml = "Today: "+String.Format("{0:D}", SysT);
        String strSql = "SELECT takings_date FROM days_takings WHERE shopname = 'shop1' AND takings_date ='" + SysT + "'";
        //myConnection.Open();
        DataSet shop1DS = new DataSet();
        //if (shop1DS.Tables["days_takings"].Rows.Count == 0)
        MySqlDataAdapter shop1DA = new MySqlDataAdapter(strSql, myConnection);
shop1DA.Fill(shop1DS, "days_takings");
//myConnection.Close();
//LoadData();

}


}
0
REA_ANDREWAuthor Commented:
I still get the same error
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

aki4uCommented:
try:

myConnection.Close();
myConnection.Dispose();
0
Jojo1771Commented:
myConnection.Open();

Dont do this before you adapter.
The adapater will open its self. Its does NOT need the open or the close. This is all handled by the adapter.
0
Ahmad1012Commented:
can u try pasting this whole code in a new vb file and try to run it again. It could be possible that this error is occuring it some other place but VS is showing it at this location. VS does do this some time.
0
bsdotnetCommented:
       if (myConnection.State.ToString() == "Open")
        {
            myConnection.Close();
        }
        myConnection.Open();
        DataSet shop1DS = new DataSet();
        MySqlDataAdapter shop1DA = new MySqlDataAdapter(strSql, myConnection);
        shop1DA.Fill(shop1DS, "days_takings");
        ......
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.