Hello, I have a problem with my connection to the emulator mySql when I log on to my games and I run a command me his crash, is when I see her in my my emulator me this error: Error Message - Connection must Be valid and open (System.InvalidOperationException), its shows me with this error "int = Affected mCommand.ExecuteNonQuery ();" is "Adapter.Fill (DataSet)" (see in the code).
Thank you for helping me, please. Because it is very urgently

Here is my code:

using System;
using System.Data;

using MySql.Data;
using MySql.Data.MySqlClient;

namespace bote.story
{

    public class SqlDatabaseClient : IDisposable
    {
        private int mId;
        private double mLastActivity;
        private MySqlConnection mConnection;
        private MySqlCommand mCommand;
        private bool mAvailable;

        public int Id
        {
            get
            {
                return mId;
            }
        }

        public bool Available
        {
            get
            {
                return mAvailable;
            }

            set
            {
                mAvailable = value;
            }
        }

        public double TimeInactive
        {
            get
            {
                return UnixTimestamp.GetCurrent() - mLastActivity;
            }
        }

        public SqlDatabaseClient(int Id, MySqlConnection Connection)
        {
            mId = Id;
            mConnection = Connection;
            mCommand = new MySqlCommand();
            mCommand.Connection = mConnection;
            mAvailable = true;

            UpdateLastActivity();
        }

        /// <summary>
        /// Called when released from using() scope - does not actually dispose, just marks as available
        /// </summary>
        public void Dispose()
        {
            mAvailable = true;
            UpdateLastActivity();
            Output.WriteLine("(Sql) Released client " + mId + " for availability.", OutputLevel.DebugInformation);
        }

        /// <summary>
        /// Called when being removed 
        /// </summary>
        public void Close()
        {
            mConnection.Close();
            mCommand.Dispose();

            mConnection = null;
            mCommand = null;
        }

        private void UpdateLastActivity()
        {
            mLastActivity = UnixTimestamp.GetCurrent();
        }

        public void ClearParameters()
        {
            mCommand.Parameters.Clear();
        }

        public void SetParameter(string Key, object Value)
        {

            mCommand.Parameters.Add(new MySqlParameter(Key, Value));
            try
            {
                mCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {

            }
        }

        public void ResetCommand()
        {
            mCommand.CommandText = null;

            ClearParameters();
        }

public int ExecuteNonQuery(string CommandText)
        {


                mCommand.CommandText = CommandText;


            int Affected = mCommand.ExecuteNonQuery();

            ResetCommand();
            return Affected;



        }

public DataSet ExecuteQuerySet(string CommandText)
{
    DataSet DataSet = new DataSet();

    mCommand.CommandText = CommandText;

    using (MySqlDataAdapter Adapter = new MySqlDataAdapter(mCommand))
    {

        Adapter.Fill(DataSet);

    }

    ResetCommand();
    return DataSet;
}

        public DataTable ExecuteQueryTable(string CommandText)
        {
            DataSet DataSet = ExecuteQuerySet(CommandText);
            return DataSet.Tables.Count > 0 ? DataSet.Tables[0] : null;
        }

        public DataRow ExecuteQueryRow(string CommandText)
        {
            DataTable DataTable = ExecuteQueryTable(CommandText);
            return DataTable.Rows.Count > 0 ? DataTable.Rows[0] : null;
        }

        public object ExecuteScalar(string CommandText)
        {
            mCommand.CommandText = CommandText;

            object ReturnValue = mCommand.ExecuteScalar();

            ResetCommand();
            return ReturnValue;
        }
    }
}

I see a mConnection.Close(), but no mConnection.Open()

A yes.. As I begin my in coding, I would like to know or add the mConnection.Open()
Please

Line 54 perhaps.

SqlDatabaseClient(int Id, MySqlConnection Connection) this is not a process to assign a open Connection object to a newly declared Connection Object. And the exception arises from here. Only pass the ConnectionString .
The codes should be

 public SqlDatabaseClient(int Id, ConnStr as String)
        {
            mId = Id;
            mConnection.ConnectionString = ConnStr;
            mConnection.Open()
            mCommand = new MySqlCommand();
            mCommand.Connection = mConnection;
            mAvailable = true;
            UpdateLastActivity();
        }

Here ConnStr for connection string.

Problem : The connection is already open

Shark 1: Your code does not work

Help please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.