bulent

bulent

  • NA
  • 15
  • 0

There is already an open DataReader associated with this Command which must be closed first

Jun 3 2009 3:59 PM


I want to get values from two columns, concatenate them, and want to write it in another column in the same row (but unfortunatly can't). There is an error on the myCmd2.ExecuteNonQuery(); line saying "There is already an open DataReader associated with this Command which must be closed first.". How can I fix it? Thanks.
SqlConnection con = new SqlConnection("data source=localhost\\SQLEXPRESS;Initial Catalog=dnm;integrated security=SSPI");
con.Open();
string mysql1 = "select col1,col2 from table1";
SqlCommand cmd = new SqlCommand(mysql1, con);

SqlDataReader myReader = null;
myReader = cmd.ExecuteReader(
CommandBehavior.CloseConnection);
while(myReader.Read())
{
string strUnite = myReader.GetString(0).ToString().Trim() +"_"+ myReader.GetString(1).ToString().Trim();
string mysql2 = "INSERT INTO table1(col3) VALUES ('" + strUnite + "')";
SqlCommand myCmd2 = new SqlCommand(mysql2, con);
myCmd2.ExecuteNonQuery();
}

Answers (12)