Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

}


}
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
Avatar of Ahmad1012
Ahmad1012

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

}


}
I still get the same error
try:

myConnection.Close();
myConnection.Dispose();
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of bsdotnet
bsdotnet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial