C#C
C#3y ago
15 replies
Alix

error about datareader

When trying to insert some fake clients into my database i am getting this error

Error DBConnectThere is already an open DataReader associated with this Connection which must be closed first.

when i search for this error i get redirect to the sdk code. So does this mean that there is a bug in the sdk or am i doing something wrong??

    private void AddFakeClient()
    {
        string clientName = GenerateRandomClientName();
        string clientEmail = GenerateRandomClientEmail();
        string clientCompany = GenerateRandomClientCompany();
        Debug.WriteLine("opening create db");

        using (DBConnect db = new DBConnect())
        {
            MySqlDataReader userRow = null;

            try
            {
                db.Open();
                Debug.WriteLine("opening create query");
                string selectQuery = "SELECT id, username FROM users";
                userRow = db.ExecuteReader(selectQuery);

                var Session = Application.Current.Resources;

                if (Session["username"] != null && Session["id"] != null)
                {
                    username = Session["username"].ToString();
                    foreignid = Session["id"].ToString();
                }
                else
                {
                    Debug.WriteLine("Session username or id is null.");
                    return;
                }

                if (userRow.HasRows)
                {
                    while (userRow.Read())
                    {
                        string userId = userRow["id"].ToString();
                        string userName = userRow["username"].ToString();
                        string insertQuery = "INSERT INTO clients (Client, email, Company, foreignid) VALUES ('" +
                                             clientName + "', '" + clientEmail +
                                             "', '" + clientCompany + "', '" + foreignid + "')";
                        db.ExecuteNonQuery(insertQuery);
                    }
                }
                else
                {
                    DisplayAlert("Error", "Account not detectable.", "OK");
                }
            }
            catch (Exception ex)
            {
                DisplayAlert("Error", $"{ ex.Message }",  "OK");
            }
            finally
            {
                Debug.WriteLine("closing create  query");
                userRow?.Close();
                Debug.WriteLine("closing create db");
                db.Close();
            }
        }
    }
`
Was this page helpful?