Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding multiple databound controls

Status
Not open for further replies.

PureDevelopers

IS-IT--Management
Aug 25, 2006
18
US
I have a page with two controls that are databound:
I am using a single connection with two commands, and the code is breaking when I attempt to execute the second reader.
The error says "There is already an open DataReader associated with this Command which must be closed first.", but it is a totally seperate command. I am confused. Is this not how you do this??

string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConnection);
Conn.Open();

string SQLLevel = "SELECT [ID] = NULL, [Name] = '--' UNION SELECT [ID], [Name] FROM [Level]";
SqlCommand cmdLevel = new SqlCommand(SQLLevel, Conn);
this.ddlLevel.DataSource = cmdLevel.ExecuteReader();
this.ddlLevel.DataTextField = "Name";
this.ddlLevel.DataValueField = "ID";
this.ddlLevel.DataBind();
cmdLevel.Dispose();

string SQLSide = "SELECT [ID] = NULL, [Name] = '--' UNION SELECT [ID], [Name] FROM [Side]";
SqlCommand cmdSide = new SqlCommand(SQLSide, Conn);
this.ddlSide.DataSource = cmdSide.ExecuteReader();
this.ddlSide.DataTextField = "Name";
this.ddlSide.DataValueField = "ID";
this.ddlSide.DataBind();
cmdSide.Dispose();
 
I don't think you can share a connection over multiple readers because a datareader represents an open connection to the database. If you want to run two simultaneous readers then you need two connections and it's not correct way.
So you can try explicitly closing the datareader after it's use and then opening second reader.
If you have SQL 2005, then try by using MultipleActiveResultsets (MARs) with Connection String.

Sharing the best from my side...

--Prashant--
 
From MSDN,
Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You will not be able to execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.

[winks]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top