I have the following class:
public class Data
{
static public SqlDataReader ExecutSql(string sql)
{
var cmd = new SqlCommand(sql, SqlCon.Conn);
var data = cmd.ExecuteReader();
return data;
}
}
It’s called from an asp.net webpage and I’m getting the error: “There is already an open DataReader associated with this command which must be closed first.”
Clearly I’m instantiating a new SqlCommand the line right before I execute the data reader. I am new to web development (my background is WinForms) but even so I can’t comprehend how I can already have an open DataReader associated with a Command that was literally just created?? I could possibly understand if it was a multi-threading sort of issue, but I’m stepping throught the code in the debugger and getting this error.
Someone want to tell me what I’m missing here?
Did you already called this method before ? Your method is returning an SqlDataReader and i am not sure whether it is being closed properly.As per msdn,
I would suggest you to read the data from your reader and close the reader and return the new type( a DataTable / DataSet or your custom class filled with properties)