• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1319
  • Last Modified:

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();

}


}
0
REA_ANDREW
Asked:
REA_ANDREW
1 Solution
 
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
Independent Software Vendors: 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!

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now