Reputation: 91
I used to call several functions with this connection string:
class Solders_DB
{
SqlConnection connection;
SqlCommand query;
String command;
public Solders_DB()
{
connection = new SqlConnection();
connection.ConnectionString = "Server=localhost;Database=Scheduling_Employee ;Trusted_Connection=True;MultipleActiveResultSets=True;";
query = new SqlCommand();
}
As you see I used this MultipleActiveResultSets=True;
in my connection but in this function :
command = @"SELECT [GID] FROM [Scheduling_Employee].[dbo].[Solder] where [ID]=@ID";
query.CommandText = command;
query.Parameters.Clear();
query.Parameters.AddWithValue("@ID", id);
Object o= query.ExecuteScalar();
I faced with this error:
There is already an open datareader associated with this command which must be closed first
Upvotes: 0
Views: 636
Reputation: 482
The code in your question is not complete. Please explain yourself better for further assistance.
When using a SqlConnection is recommended to use the 'using' statement like this:
using (SqlConnection SqlConn = new SqlConnection(ConnString))
{
try
{
SqlConn.Open();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return null;
}
}
You're trying to read data from the first execution of your query, please update your question and put the complete code and the error stack.
Upvotes: 1
Reputation: 11
Its usually when there was another query before that one and has not yet stopped executing inside the database engine (maybe it was a heavy script). You could try query = new SqlCommand();
, query.Cancel()
or
while(query.Connection.State != System.Data.ConnectionState.Open){ Threading.Sleep(100);}
Upvotes: 0