0
public static void DropDownList_Bind(DropDownList list_name,string DtaTxtField,string DataValueField,String Procedure)
   {
       OpenConnection();
       com = new SqlCommand(Procedure, con); 
       com.CommandType = CommandType.StoredProcedure;
       SqlDataReader dtr = com.ExecuteReader();
       list_name.DataSource = dtr;
       list_name.DataTextField = DtaTxtField;
       list_name.DataValueField = DataValueField;
       list_name.DataBind();
       dtr.Close();
       CloseConnection();
   }
public static void OpenConnection()
    {
        if(con.State==ConnectionState.Closed)
            con.Open();

    }
    public static void CloseConnection()
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }

This is my Bussiness logic code for binding DropDownList,it is giving an execption There is already an open DataReader associated with this Command which must be closed first. on SqlDataReader dtr = com.ExecuteReader(); .please give some suggestions. Thanx in advance

6
  • Can you show me code for OpenConnection() and CloseConnectin() ? Commented Jul 11, 2013 at 11:26
  • In first time ,has your data binding complete successfully ? Commented Jul 11, 2013 at 11:36
  • Yes ,first time it is binding succesfully . Commented Jul 11, 2013 at 11:39
  • has CloseConnection() fired ? Commented Jul 11, 2013 at 11:40
  • Do you have other code that is reading data from the database and running this code inside that? For example using (SqlDataReader rdr = cmd.ExecuteQuery()) { while (rdr.Read()) { DropDownList_Bind(rdr[0], rdr[1], rdr[2]); } } Commented Jul 11, 2013 at 11:42

1 Answer 1

1

You need to make “MultipleActiveResultSets=True” in your connection string.

I have written a blog post at following link.

http://www.dotnetjalps.com/2013/06/Entity-Framework-There-is-already-an-open-DataReader-associated-with-this-Command.html

Regards,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.