Wednesday, June 02, 2004 - Posts

There is already an open DataReader associated with this Connection which must be closed first. (Try reader.Close(); )

Ok so I wanted to get just 3 little thing from my DB and i used this code in the costructor of my Component:

PosterCommand = new System.Data.SqlClient.SqlCommand();
PosterCommand.CommandText = @"SELECT firstName, name, tussenVoegsel, costumerID FROM customerInfo WHERE (costumerID = @id)";
PosterCommand.Connection =
this.sqlConnection2;
PosterCommand.Parameters.Add(
new System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.Float, 8, "id"));

After this i used the function to get the info:

public string Poster(double ID)
{
PosterCommand.Parameters["@id"].Value = ID;
reader = PosterCommand.ExecuteReader();
reader.Read();
string Poster = string.Format("{0}{1}{2}",reader[0].ToString(),reader[2].ToString(),reader[1].ToString());
return Poster;
}

But when i was running the function for the second time i go the error There is already an open DataReader associated with this Connection which must be closed first. And then i found the blody problem i needed to CLOSE THE DATAREADER !!! so now the function is like this:

public string Poster(double ID)
{
PosterCommand.Parameters["@id"].Value = ID;
reader = PosterCommand.ExecuteReader();
reader.Read();
string Poster = string.Format("{0}{1}{2}",reader[0].ToString(),reader[2].ToString(),reader[1].ToString());

reader.Close();
return Poster;
}

Have fun and Good Netting