Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re:

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


    }


    }

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •