I have created two different connections. One for DataReader and second for NonQuery.
DataReaderConnect = DBUtils.GetDBConnection(_DbUserName, _DbPassword);
DataReaderConnect.Open();
NonQueryConnect = DBUtils.GetDBConnection(_DbUserName, _DbPassword);
NonQueryConnect.Open();
I have 2 methods that is using those connections
public async Task UpdateUser(cUser.UserObject User)
{
string SQL = $"UPDATE {_DbName}.dbUserTable SET Name = @Name....";
using MySqlCommand Command = new MySqlCommand(SQL, NonQueryConnect);
Command.Parameters.AddWithValue("@Name", User.Name);
...
await Command.ExecuteNonQueryAsync().ConfigureAwait(false);
}
public async Task<cUser.UserObject> GetUser(int ttvUserID)
{
List<cUser.UserObject> Users = new List<cUser.UserObject>();
string SQL = $"SELECT * FROM {_DbName}.dbUserTable WHERE TwitchID = @ID";
using MySqlCommand Command = new MySqlCommand(SQL, DataReaderConnect);
Command.Parameters.AddWithValue("@ID", ttvUserID);
using var sqlReader = await Command.ExecuteReaderAsync().ConfigureAwait(false);
while (await sqlReader.ReadAsync().ConfigureAwait(false))
{
Users.Add(new cUser.UserObject()
{
dbID = Convert.ToInt32(sqlReader[0]),
TwitchID = Convert.ToInt32(sqlReader[1]),
Name = sqlReader[2].ToString(),
isSub = Convert.ToInt32(sqlReader[3]),
isVip = Convert.ToInt32(sqlReader[4])
});
}
}
But im getting exception "There is already an open DataReader associated with this Connection which must be closed first." And I cant figure out why. Im not using DataReader for NonQuery and vice versa
edit: I figured it out and it was just me being silly and apparently I can't read. .NET not only cant support DataReader and NonQuery on the same connection but also more then one DataReader on the same connection.