Helpful Information
 
 
Category: ASP.NET
Error : DataReader associated with this Connection which must be closed first

Following is my Code behind : when i try to run the programm it gives error saying "There is already an open DataReader associated with this Connection which must be closed first."


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using myutility;


namespace tryreader
{

public class viewusers : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid usergrid;

private void bindgrid()
{
SqlDataAdapter da=new SqlDataAdapter("select a.fname,a.lname,a.userid,a.upwd,b.dept as dept,a.regdt,a.active from issueusers a, department b where a.dept=b.id",myutility.connection.getinstance());
DataSet ds=new DataSet();
da.Fill(ds);
usergrid.DataSource=ds;
usergrid.DataBind();



}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (! IsPostBack)
{
bindgrid();
}
}

public SqlDataReader GetValues(String tablename)
{

string sqlsel="select * from department";
SqlCommand cmd=new SqlCommand(sqlsel,myutility.connection.getinstance());

return cmd.ExecuteReader();

}
}




thanx in Advance

Hard to tell without seeing this code, too.
myutility.connection.getinstance()

I never use a connection as just one instance. I use the sqlconnection object and let .net handle allocating it.

here is the singletone connection code:

namespace myutility
{
/// <summary>
/// Summary description for datatier.
/// </summary>
public class connection
{
static SqlConnection instance=null;
static SqlTransaction tran;
public static SqlConnection getinstance()
{
if(instance==null)
{
string constr = ConfigurationSettings.AppSettings["myconn"];
SqlConnection conn=new SqlConnection();
conn.ConnectionString=constr;
conn.Open();
instance=conn;
}
return instance;
}
public static void close()
{
instance.Close() ;
}
public static SqlTransaction trans()
{
tran=instance.BeginTransaction();
return tran;
}
public static void commit()
{
tran.Commit();
}


}


}

Where is end transaction?

This really looks odd, though.
There's no thread safety, a MUST in a singleton.
Perhaps you should read this over.
http://www.yoda.arachsys.com/csharp/singleton.html










privacy (GDPR)