I tried calling this method from multiple threads, trying to get the identifier from the same string. I always get this exception in the line where I create the SqlDataReader:
There is already an open DataReader associated with this Command, which should be closed first.
I do not know where the problem is. I use the lock () operator, so I use the command only once, and then I recycle it. Its new to database programming, so I donβt know where my error is.
Thanks!
public int UsernameGetID(string username) { using (var command = new SqlCommand("SELECT user_id FROM " + ServerConstants.Database.TableUserInformation + " WHERE username = @Username", connection)) { lock (command) { SqlParameter param = new SqlParameter("@Username", SqlDbType.VarChar, username.Length); param.Value = username; command.Parameters.Add(param); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { return (int)reader[0]; } else {
Pacha source share