The connection is already open and the connection state is connecting.

Hello,

I am getting this  error time to time randomly. My web application is built on asp.net 3.5 which uses ODBC 5.1 driver to connect to mysql database. I have a separate database class which handles the connection, execute, fetching and all. The application runs fine for sometime then suddenly the users start getting this error after which the application fails to insert select from database.  

I have posted in this forum couple of times now and no one is really contributing or helping me. Please..Any help will be highly appreciated..

Many thanks,
Viv
0
vivekkrish2001
11/7/2008 2:40:10 AM
asp.net.object-datasource 16182 articles. 0 followers. Follow

9 Replies
1713 Views

Similar Articles

[PageSpeed] 34

The database abstract looks like this..
***************************************************************************************using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Dbconnect
{
    public static class Database
    {
        private static long _numQueries = 0;

        private static OdbcConnection _connection = null;
        private static bool _isOpen = false, _gettingInput = false, _dbIsBusy = false;
        private static string _dsn = "";
        private static System.Windows.Forms.DialogResult _userInput = System.Windows.Forms.DialogResult.None;

        public static long NumQueries { get { return _numQueries; } set { _numQueries = value; } }

        public static bool isDBBusy() { return _dbIsBusy; }

        public static void setDSN(string dsn)
        {
            if (_isOpen)
                return;

            _dsn = dsn;
            _connection = new OdbcConnection("DSN=" + _dsn);
            //_connection.ConnectionTimeout = 1;
        }

        public static bool useDB()
        {
            //    if (!_isOpen)
            //        return false;

            //    if (_connection.State != ConnectionState.Open)
            //    {
            //        int timePassed = 0;
            //        while (_connection.State != ConnectionState.Open && timePassed = 3000)
            //            return false;
            //    }

            //    _dbIsBusy = true;

            return true;
        }

        public static void finishDB()
        {
            //    _dbIsBusy = false;
        }

        public static bool Connect()
        {
            _isOpen = false;
            try { _connection.Open(); }
            catch (Exception ex)
            {
                /*
                StringBuilder builder = new StringBuilder();
                string sMsg = ex.Message;


                builder.Append("");
                sMsg = sMsg.Replace("\"", "'");
                builder.Append("alert( \"" + sMsg + "\" );");



                builder.Append("");

                HttpContext.Current.Response.Write(builder.ToString());
                */
                   HttpContext.Current.Response.Write(ex.Message);
                return false;

            }

            //if (_connection.State != System.Data.ConnectionState.Open)
            //    return false;

            _isOpen = true;
            return true;
        }

        public static void Disconnect()
        {
            if (null != _connection)
                _connection.Close();

            _isOpen = false;
        }

        public static bool TestConnection()
        {
            bool result = Connect();
            if (result)
               Disconnect();
            return result;
        }

        public static bool DatabaseSynchronizing()
        {
            DataTable dt = new DataTable();
            int numRows = 0;
            bool synchronizing = true;
            if (GetData("select synchronizing from client_branch;", ref numRows, dt))
            {
                if (numRows > 0)
                {
                    if (!GetBoolean(ref synchronizing, dt, 0, "synchronizing"))
                    {
                        synchronizing = true;
                    }
                }
            }
            if (dt != null)
                dt.Dispose();

            return synchronizing;
        }

        public static void WaitUntilReady()
        {
            //bool available = false;
            //while (!available)
            //{
            //    available = !DatabaseSynchronizing();
            //    if (!available)
            //        System.Threading.Thread.Sleep(50);
            //}
        }

        public static bool dbErrorContinue()
        {
            if (_gettingInput)
                return false;

            _gettingInput = true;
            _userInput = System.Windows.Forms.DialogResult.None;
            Disconnect();
            do
            {
                //   _userInput = SmartMessageBox.ShowRetryCancel("Network connection to the local branch server is down.");
            } while (_userInput == System.Windows.Forms.DialogResult.Retry && !Connect());

            _gettingInput = false;
            return (_userInput == System.Windows.Forms.DialogResult.Retry);
        }

        public static string escapeSlashes(string command)
        {
            string result = "";
            for (int i = 0; i  65535)
                {
                    //SmartMessageBox.ShowMessage(
                    // "Error: This update is too large to be synchronized. " +
                    // "Please contact Vandoes Computer Systems and quote the following information:\n" +
                    // "'Update too large' - '" + command.Substring(0, 20) + "'");
                }
                else
                {
                    SendSyncCommand(command);
                }
            }

            _numQueries++;

            return true;
        }

        public static bool RunCalculation(string command, ref object output)
        {
            //if (!useDB())
            //    return false;

            bool success = false;//, abort = false;
            //while (!success && !abort)
            //{
            try
            {
                if (_isOpen == false)
                {
                    Connect();
                }
                OdbcCommand c = _connection.CreateCommand();
                c.CommandText = command;
                c.CommandTimeout = 3;
                c.CommandType = CommandType.Text;
                output = c.ExecuteScalar();
                success = true;
            }
            catch (Exception e)
            {
                //  SmartMessageBox.ShowMessage(e.GetType().ToString() + ": " + e.Message);
                // if (!dbErrorContinue())
                //    abort = true;
            }
            finally
            {
                Disconnect();
            }
            //}

            //finishDB();

            if (!success)
                return false;

            if (output is DBNull)
                return false;

            _numQueries++;
            return true;
        }

        public static bool GetData(string query, ref int numRows, DataTable dt)
        {
            //if (!useDB())
            //    return false;

            bool success = false;//, abort = false;
            //while (!success && !abort)
            //{
            try
            {
                if (_isOpen == false)
                {
                    Connect();
                }
                OdbcCommand c = _connection.CreateCommand();
                c.CommandText = query;
                //c.CommandTimeout = 3;
                c.CommandType = CommandType.Text;
                OdbcDataAdapter da = new OdbcDataAdapter(c);
                numRows = da.Fill(dt);
                //da.Dispose();
                success = true;
            }
            catch
            {
                //SmartMessageBox.ShowMessage(e.GetType().ToString() + ": " + e.Message);
                //if (!dbErrorContinue())
                //    abort = true;
            }
            finally
            {
                Disconnect();
            }
            //}

            //finishDB();

            _numQueries++;
            return success;
        }

        public static bool GetChar(ref char output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = '\0';
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is string))

                if (data.ToString() != "")
                    output = Convert.ToChar(data);
            }

            return true;
        }

        public static bool GetString(ref string output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = "";
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is string))

                output = Convert.ToString(data);
                output.Replace("\\", "");
            }

            return true;
        }

        public static bool GetBlob(ref byte[] output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //    SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = null;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is byte[]))

                if (data is byte[])
                    output = (byte[])data;
                else
                {
                    //   SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'blob'.");
                    return false;
                }
            }

            return true;
        }
        /*
        public static bool GetImage(ref Image output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            byte[] rawData = null;
            MemoryStream myStream = null;
            if (data is DBNull)
                output = null;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is byte[]))

                if (data is byte[])
                {
                    // Get the image from bytes
                    rawData = (byte[])data;
                    if (rawData.Length == 0)
                    {
                        output = null;
                        return true;
                    }
                    try
                    {
                        myStream = new MemoryStream(rawData);
                        output = Bitmap.FromStream(myStream, true, true);
                    }
                    catch (Exception)
                    {
                        output = null;
                       // SmartMessageBox.ShowMessage("Error in database. Data in field '" + field + "' is not a valid image.");
                        return true;
                    }
                }
                else
                {
                //    SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'image'.");
                    return false;
                }
            }

            return true;
        }
        */
        public static bool GetShort(ref short output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int16))

                output = Convert.ToInt16(data);
            }

            return true;
        }

        public static bool GetInt(ref int output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int32))

                output = Convert.ToInt32(data);
            }

            return true;
        }

        public static bool GetULong(ref ulong output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Decimal || data is Int64))

                output = Convert.ToUInt64(data);
            }

            return true;
        }

        public static bool GetLong(ref long output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int64))

                output = Convert.ToInt64(data);
            }

            return true;
        }

        public static bool GetDouble(ref double output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.00;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Double))

                output = Convert.ToDouble(data);
            }

            return true;
        }

        public static bool GetFloat(ref float output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.0f;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Single))

                output = Convert.ToSingle(data);
            }

            return true;
        }

        public static bool GetDecimal(ref decimal output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.00m;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Decimal))

                output = Convert.ToDecimal(data);
            }

            return true;
        }

        public static bool GetBoolean(ref bool output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = false;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int16))

                output = Convert.ToBoolean(data);
            }

            return true;
        }

        public static bool GetDate(ref DateTime output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = DateTime.MinValue;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is DateTime))

                if (data is DateTime)
                    output = (DateTime)data;
                else
                {
                    //     SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'date'.");
                    return false;
                }
            }

            return true;
        }

        public static bool GetTime(ref TimeSpan output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = TimeSpan.MinValue;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is TimeSpan))

                if (data is TimeSpan)
                    output = (TimeSpan)data;
                else
                {
                    //     SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'time'.");
                    return false;
                }
            }

            return true;
        }
    }
}
0
vivekkrish2001
11/7/2008 3:15:48 AM

Hi vivekkrish2001,

From the description, we couldn't understand your question clearly. Could you please tell us the exact error? Also, please format the source code so that we can see it easily.

Thanks.


David Qian
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Wencui
11/11/2008 3:22:33 AM
Hello Wencui,

Thanks for your reply, i almost gave up hope on this asp.net forums. Well i explain it again, i have built an asp.net 3.5 web aaplication whihc uses odbc driver 5.1 to connect to a mysql backend database. The application involves numerous queries and database transactions and is accessed by multiple users at same time. For some time the application works fine without any problem i.e. it performs query insert update etc etc on the database without any problem. After some time say 1 hour of continuos use, the application suddenly crashes. I have placed a HTTP context in my database abstract to write the error on my screen. The error users get is "The connection is already open, the connection state is reconnecting". If i wait for 4-5 mins and ask users to refresh the page then it works without any problem again for next 10-15 mins. I have enabled ODBCconnection pooling as well.

Is there any way for me to attach the database class file here? when i copy paste the formatting goes bad and becomes insensible. Email address or anything?.


Many thanks,

Vivek
0
vivekkrish2001
11/11/2008 4:07:09 AM

 Hello Wencui,

Thanks for your reply, i almost gave up hope on this asp.net forums. Well i explain it again, i have built an asp.net 3.5 web aaplication whihc uses odbc driver 5.1 to connect to a mysql backend database. The application involves numerous queries and database transactions and is accessed by multiple users at same time. For some time the application works fine without any problem i.e. it performs query insert update etc etc on the database without any problem. After some time say 1 hour of continuos use, the application suddenly crashes. I have placed a HTTP context in my database abstract to write the error on my screen. The error users get is "The connection is already open, the connection state is reconnecting". If i wait for 4-5 mins and ask users to refresh the page then it works without any problem again for next 10-15 mins. I have enabled ODBCconnection pooling as well.Is there any way for me to attach the database class file here? Find the databse class scross down

 

Many thanks,

Vivek

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Dbconnect
{
    public static class Database
    {
        private static long _numQueries = 0;

        private static OdbcConnection _connection = null;
        private static bool _isOpen = false, _gettingInput = false, _dbIsBusy = false;
        private static string _dsn = "";
        private static System.Windows.Forms.DialogResult _userInput = System.Windows.Forms.DialogResult.None;

        public static long NumQueries { get { return _numQueries; } set { _numQueries = value; } }

        public static bool isDBBusy() { return _dbIsBusy; }

        public static void setDSN(string dsn)
        {
            if (_isOpen)
                return;

            _dsn = dsn;
            _connection = new OdbcConnection("DSN=" + _dsn);
            //_connection.ConnectionTimeout = 1;
        }

        public static bool useDB()
        {
            //    if (!_isOpen)
            //        return false;

            //    if (_connection.State != ConnectionState.Open)
            //    {
            //        int timePassed = 0;
            //        while (_connection.State != ConnectionState.Open && timePassed < 3000)
            //        {
            //            System.Threading.Thread.Sleep(500);
            //            timePassed += 500;
            //        }
            //        if (timePassed >= 3000)
            //            return false;
            //    }

            //    _dbIsBusy = true;

            return true;
        }

        public static void finishDB()
        {
            //    _dbIsBusy = false;
        }

        public static bool Connect()
        {
            _isOpen = false;
            try { _connection.Open(); }
            catch (Exception ex)
            {
                /*
                StringBuilder builder = new StringBuilder();
                string sMsg = ex.Message;


                builder.Append("<script language='javascript'>");
                sMsg = sMsg.Replace("\"", "'");
                builder.Append("alert( \"" + sMsg + "\" );");



                builder.Append("</script>");

                HttpContext.Current.Response.Write(builder.ToString());
                */
                   HttpContext.Current.Response.Write(ex.Message);
                return false;

            }

            //if (_connection.State != System.Data.ConnectionState.Open)
            //    return false;

            _isOpen = true;
            return true;
        }

        public static void Disconnect()
        {
            if (null != _connection)
                _connection.Close();

            _isOpen = false;
        }

        public static bool TestConnection()
        {
            bool result = Connect();
            if (result)
               Disconnect();
            return result;
        }

        public static bool DatabaseSynchronizing()
        {
            DataTable dt = new DataTable();
            int numRows = 0;
            bool synchronizing = true;
            if (GetData("select synchronizing from client_branch;", ref numRows, dt))
            {
                if (numRows > 0)
                {
                    if (!GetBoolean(ref synchronizing, dt, 0, "synchronizing"))
                    {
                        synchronizing = true;
                    }
                }
            }
            if (dt != null)
                dt.Dispose();

            return synchronizing;
        }

        public static void WaitUntilReady()
        {
            //bool available = false;
            //while (!available)
            //{
            //    available = !DatabaseSynchronizing();
            //    if (!available)
            //        System.Threading.Thread.Sleep(50);
            //}
        }

        public static bool dbErrorContinue()
        {
            if (_gettingInput)
                return false;

            _gettingInput = true;
            _userInput = System.Windows.Forms.DialogResult.None;
            Disconnect();
            do
            {
                //   _userInput = SmartMessageBox.ShowRetryCancel("Network connection to the local branch server is down.");
            } while (_userInput == System.Windows.Forms.DialogResult.Retry && !Connect());

            _gettingInput = false;
            return (_userInput == System.Windows.Forms.DialogResult.Retry);
        }

        public static string escapeSlashes(string command)
        {
            string result = "";
            for (int i = 0; i < command.Length; i++)
            {
                if (command[i] == '\'')
                {
                    result += "\\";
                }
                result += command[i];
            }
            return result;
        }

        public static bool SendSyncCommand(string command)
        {
            //return true;
            return RunCommand("insert into `update_queue` (command) values ('" + escapeSlashes(command) + "');", false);
        }

        public static bool RunCommand(string command, bool sync)
        {
            //if (!useDB())
            //   return false;

            bool success = false;//, abort = false;
            //while (!success && !abort)
            //{
            try
            {
                if (_isOpen == false)
                {
                    Connect();
                }
                OdbcCommand c = _connection.CreateCommand();
                c.CommandText = command;
                c.CommandTimeout = 3;
                c.CommandType = CommandType.Text;
                c.ExecuteNonQuery();
                success = true;
            }
            catch //(Exception e)
            {
                //   SmartMessageBox.ShowMessage(e.GetType().ToString() + ": " + e.Message);
                //if (!dbErrorContinue())
                //    abort = true;
            }
            finally
            {
                Disconnect();
            }
            //}

            //finishDB();

            if (!success)
                return false;

            if (sync)
            {
                // Add to list of updates to send
                if (command.Length > 65535)
                {
                    //SmartMessageBox.ShowMessage(
                    // "Error: This update is too large to be synchronized. " +
                    // "Please contact Vandoes Computer Systems and quote the following information:\n" +
                    // "'Update too large' - '" + command.Substring(0, 20) + "'");
                }
                else
                {
                    SendSyncCommand(command);
                }
            }

            _numQueries++;

            return true;
        }

        public static bool RunCalculation(string command, ref object output)
        {
            //if (!useDB())
            //    return false;

            bool success = false;//, abort = false;
            //while (!success && !abort)
            //{
            try
            {
                if (_isOpen == false)
                {
                    Connect();
                }
                OdbcCommand c = _connection.CreateCommand();
                c.CommandText = command;
                c.CommandTimeout = 3;
                c.CommandType = CommandType.Text;
                output = c.ExecuteScalar();
                success = true;
            }
            catch (Exception e)
            {
                //  SmartMessageBox.ShowMessage(e.GetType().ToString() + ": " + e.Message);
                // if (!dbErrorContinue())
                //    abort = true;
            }
            finally
            {
                Disconnect();
            }
            //}

            //finishDB();

            if (!success)
                return false;

            if (output is DBNull)
                return false;

            _numQueries++;
            return true;
        }

        public static bool GetData(string query, ref int numRows, DataTable dt)
        {
            //if (!useDB())
            //    return false;

            bool success = false;//, abort = false;
            //while (!success && !abort)
            //{
            try
            {
                if (_isOpen == false)
                {
                    Connect();
                }
                OdbcCommand c = _connection.CreateCommand();
                c.CommandText = query;
                //c.CommandTimeout = 3;
                c.CommandType = CommandType.Text;
                OdbcDataAdapter da = new OdbcDataAdapter(c);
                numRows = da.Fill(dt);
                //da.Dispose();
                success = true;
            }
            catch
            {
                //SmartMessageBox.ShowMessage(e.GetType().ToString() + ": " + e.Message);
                //if (!dbErrorContinue())
                //    abort = true;
            }
            finally
            {
                Disconnect();
            }
            //}

            //finishDB();

            _numQueries++;
            return success;
        }

        public static bool GetChar(ref char output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = '\0';
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is string))

                if (data.ToString() != "")
                    output = Convert.ToChar(data);
            }

            return true;
        }

        public static bool GetString(ref string output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = "";
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is string))

                output = Convert.ToString(data);
                output.Replace("\\", "");
            }

            return true;
        }

        public static bool GetBlob(ref byte[] output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //    SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = null;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is byte[]))

                if (data is byte[])
                    output = (byte[])data;
                else
                {
                    //   SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'blob'.");
                    return false;
                }
            }

            return true;
        }
        /*
        public static bool GetImage(ref Image output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            byte[] rawData = null;
            MemoryStream myStream = null;
            if (data is DBNull)
                output = null;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is byte[]))

                if (data is byte[])
                {
                    // Get the image from bytes
                    rawData = (byte[])data;
                    if (rawData.Length == 0)
                    {
                        output = null;
                        return true;
                    }
                    try
                    {
                        myStream = new MemoryStream(rawData);
                        output = Bitmap.FromStream(myStream, true, true);
                    }
                    catch (Exception)
                    {
                        output = null;
                       // SmartMessageBox.ShowMessage("Error in database. Data in field '" + field + "' is not a valid image.");
                        return true;
                    }
                }
                else
                {
                //    SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'image'.");
                    return false;
                }
            }

            return true;
        }
        */
        public static bool GetShort(ref short output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int16))

                output = Convert.ToInt16(data);
            }

            return true;
        }

        public static bool GetInt(ref int output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int32))

                output = Convert.ToInt32(data);
            }

            return true;
        }

        public static bool GetULong(ref ulong output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Decimal || data is Int64))

                output = Convert.ToUInt64(data);
            }

            return true;
        }

        public static bool GetLong(ref long output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = -1;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int64))

                output = Convert.ToInt64(data);
            }

            return true;
        }

        public static bool GetDouble(ref double output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.00;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Double))

                output = Convert.ToDouble(data);
            }

            return true;
        }

        public static bool GetFloat(ref float output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.0f;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Single))

                output = Convert.ToSingle(data);
            }

            return true;
        }

        public static bool GetDecimal(ref decimal output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = 0.00m;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Decimal))

                output = Convert.ToDecimal(data);
            }

            return true;
        }

        public static bool GetBoolean(ref bool output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = false;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is Int16))

                output = Convert.ToBoolean(data);
            }

            return true;
        }

        public static bool GetDate(ref DateTime output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                // SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = DateTime.MinValue;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is DateTime))

                if (data is DateTime)
                    output = (DateTime)data;
                else
                {
                    //     SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'date'.");
                    return false;
                }
            }

            return true;
        }

        public static bool GetTime(ref TimeSpan output, DataTable dt, int row, string field)
        {
            DataColumn column = dt.Columns[field];
            if (column == null)
            {
                //  SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' does not exist.");
                return false;
            }
            object data = dt.Rows[row][column];
            if (data is DBNull)
                output = TimeSpan.MinValue;
            else
            {
                // Write Invalid data type to log file...
                //if (!(data is TimeSpan))

                if (data is TimeSpan)
                    output = (TimeSpan)data;
                else
                {
                    //     SmartMessageBox.ShowMessage("Error in database. Field '" + field + "' not of type 'time'.");
                    return false;
                }
            }

            return true;
        }
    }
}
 

 

 

 

0
vivekkrish2001
11/11/2008 4:12:44 AM

Hi vivekkrish2001,

I suggest you to format your code well so that we can help you better.

From the error message, it's probably caused by the connection pool. If the connetions in the pool are not released in time, the following requests will be delayed. My suggestion is to check the connection pool and ensure each connection can be released in time.

Thanks.


David Qian
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Wencui
11/11/2008 10:00:23 AM

Hi vivkey

Whenever open the connection, you have to check the niether connection is open or close. so you have to use the below code whenever open the connection.

if (Con.State=ConnectionState.open) Con.close();

Con.Open();

here Con is the Connection Objects. all the best for u task.


Thiru

Hope this help someone in the future
0
pathey77
11/11/2008 10:41:24 AM
Hi Wencui,

When you say format the code what do you mean? Sorry for that..

Regarding connection pool is concerned how will release the connections in time. If you see my connection objects are static in nature therefore there might be a case where a single connection is created and used again and again..

Thanks,
Vivek
0
vivekkrish2001
11/11/2008 11:02:30 AM

 Hi Pathey,

I have even tried that but of no use.. this is my latest connect() function

 

 public static bool Connect()
        {
            setDSN("vivek25_kirly");
           // _isOpen = false;
            try
            {
               
                if (_connection == null || _connection.State != ConnectionState.Open)
                    _connection.Open();

                _isOpen = true;


                return true;

           
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);

              //  Disconnect();
                return false;

            }
    

         
        }

 

0
vivekkrish2001
11/11/2008 11:16:03 AM

Hi vivekkrish2001,

When you say format the code what do you mean?

I just suggest you to post your source code in a good format so that we can read it easily. Otherwise, the code is not readable and it will get less replies. Please use the tool named "source code" in the right side of the panel when you compose the question. It can help you format the code well.

Regarding connection pool is concerned how will release the connections in time. If you see my connection objects are static in nature therefore there might be a case where a single connection is created and used again and again

Are you sure the max number of the connections is big enough? Also, if some operation needs much time, you can think about use asynchronous connection. All of this is to keep more connections in the pool usable.

Thanks.

 


David Qian
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Wencui
11/12/2008 9:00:16 AM
Reply:

Similar Artilces:

The connection is already Open (state=Connecting) when populating DataGrid
I get the following error when populating a DataGrid: Source: System.DataThe connection is already Open (state=Connecting).   at System.Data.OleDb.OleDbConnection.Open()   at System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection, ConnectionState& originalState)   at System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTab...

not connected when I am connected
What do I need to change on my 'puter when the icon in the toolbar shows and tells me I am connected and the error message tells me I am not and I can not go to web pages, get or send emails. I have to close down and restart to get connected again. This only happens if I am on line and leave the 'puter for an hour or so. As long as I am using it I don't have a problem. This just started about a month ago. I am on very slow dialup, DSL and Cable not in my area, thanks you SBC/ATT tks Elaine wrote: > What do I need to change on my 'puter when the icon in ...

Timeout connecting with .NET connection object
I have a SQL Anywhere 9 database that I can connect to fine via Sybase Central or ISQL. However, when I attempt to connect using the AsaConnection object from the IAnywhere.Data.AsaClient namespace, the open connection call times out. The code works fine on my development box, but I cannot get the connection to work on the production box. Any ideas? Thanks, David ...

Execute Reader requires an open and valid connection. Connection's current state is open.
I am getting this message intermittently.  I have MARS = True in my connection string.  Any idea where I could be missing this? Thanks, Eva Do you have literally have MARS = True in your connection string?  If so try MultipleActiveResultSets=True I have the full thing, I was just too lazy to type it....

Connection object connecting to server
I'm moving from Sybase Central Java Edition 3.2 to EAServer 6.1 and Powerbuilder 8 to PB 10.5. The PB app has been migrated and to the best of my knowledge, I've set up EAServer 6 the same way as Sybase Central was set up. When attempting to login to the application using my loginID and password, I receive the following message: this connection object is not connected to a server. If I use the same login that's used for EAServer 6.1 then it's fine. Any constructive input would be appreciated. Thanks. Developer wrote... > I'm moving from Sybase Central Java...

Connect to LAN (Was connect to NET)
Almost time for a new header, the old was getting too long and the focus is now different. So .... Story so far - Mepis 6 - setting LAN IP would upset external site addressing but the ISP hung in there without dropping the line. I have, at last, succeeded in connecting to the web via dial up AND keeping it running while seeing a LAN. I can't point directly to what I did but I did keep notes along the way as to what NOT to touch. Call it pure luck, deft fingerwork, incantations and imprecations. Next problem - 5 PC's. (5 Machine names) A, B, C, D, E. (4 are Win XP...

test for open connection before connection.open command
Hi I am trying to work together a peice of code that will basically state: If MyCon.Open Then 'do nothing Else MyCon.Open() End If This is because occasionally the SQL.ExecuteReader() occasionally times out and when the user trys again the page errors and states that the MyCon is already open. Has anyone had to deal with this before? Thanks for any help, Skippy. Try this - If MyCon.state = ConnectionState.open Then 'do nothing else ... In fact, if you require only to check connection is open and if not open the connection then fo...

connection pooling and open connections
Hi Dmitry, I have connection pooling enabled and I'm connecting to MySQL with 2 AdConnection at the same time. Is it normal that I see 2 active connections on MySQL management and not just one? I thought that when using connection pooling they would share the same one. Thanks, Lorenzo Hello Lorenzo > Is it normal that I see 2 active connections on MySQL management and not > just one? FireDAC does not open the pooled connections before they will be really needed. Instead FireDAC returns open connections back to pool instead of closing them, when they are no l...

The connection was not closed. The connection's current state is open.
in my code, i have two identical calls to the same function, both supposed to return string values from one field each. when i try to preview the application in ie i get a server error on the last row in this call - even though the identical call two rows earlier was ok. i have several similar calls following, and when i tried commenting out the one giving me problems the next one did the same thing. this is what the call looks like. the complaint from the server is on row 8 (unless i comment it out, and it complains about the next call on row 10...) 1  &...

The connection was not closed. The connection's current state is open
I'm  not sure why I am still getting this connection errorwww.photopizzaz.biz/allcode.txtWhen is Microsoft going to get rid of VB.NET! resolved it, please disregard this thread.When is Microsoft going to get rid of VB.NET! Okay, But you might want to remove that code for security reasons(from the server).-Blake NiemyjskiBlog - Website: windowscoding.com...

SFtp connect
The following Ftp connect to a sftp site has the right port/usrname/password [verified thru filezilla], but when I try to connect via TIDFTP -- I get "Connection Closed Gracefully" upon ftp->Connect();; The ftp properties are below the code. I have no other Indy objects on the form. It ocurred to me that I may need something for sftp. ftp->Connect(); if (ftp->Connected()) { ::Sleep(1); } object ftp: TIdFTP AutoLogin = True Host = 'ftp.dailyaccess.com' Password ='XXXXX' Port = 21 Username = 'charles.leggette' ProxySet...

The connection is already Open (state=Open, Fetching).
Hello again. This problem relates to a Business Object that I've created. I get the above error when I try t call a Sub within the business object that inserts into the database. I have various other Subs and Functions that Select from the database and they don't seem to have a problem. I can't see where the connection is being left open... But I've been staring at this all day now so theres a good chance I'm missing something obvious... The VB for the business object is below. The sub in the aspx page that calls it is: Sub Button1_Click(Source As Object, E As EventArgs)dim objForum ...

Error:The connection is already Open (state=Open)
Hi i got an error "The connection is already Open (state=Open)" when connect to the database but if i check the connection state before open the database, i got another error; if(data.Conn.State!=ConnectionState.Open)      data.Conn.Open();   thanks in advance.   and I found that the connection state is closed before i open the database   really in puzzle Hi, could you please provide the relevant pieces of code that's not working? This makes it easier to find what's wrong and provide more accurate answers. Grz, Kris.Read my blog. Handy Fire...

The connection was not closed. The connection's current state is open. #2
Hey guys. I've got a really weird problem that just drives me mad. I've got a class written in VB that does some actions on a table in my access database. It looks something like this :Public Class Fund_Func Dim k As HttpContextPrivate Conn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & k.Current.Server.MapPath("DataBase.mdb") & ";")  Public Sub Edit_Snap(ByVal Funct As Fund_Info)Dim Cmd As New OleDbCommand("Edit_Snap", Conn) Cmd.CommandType = CommandType.StoredProcedureDim p As N...

Web resources about - The connection is already open and the connection state is connecting. - asp.net.object-datasource

Connection - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

iMedia Connection: Interactive Marketing News, Features, Podcasts and Video - iMediaConnection.com
High-quality data, if not used properly, can still lead marketers to make bad decisions. Consider these common ways that numbers are used to ...

HTTP persistent connection - Wikipedia, the free encyclopedia
... tacked on to an existing protocol. If the browser supports keep-alive, it adds an additional header to the request: Following this, the connection ...

CareerSonar Turns Facebook Friends Into Job Connections
Looking for a job ? Among your Facebook friends lies the potential for employment. CareerSonar , a new service, brings together a person’s connections ...

Kings Cross identities arrested in connection with murder
Two Kings Cross identities&nbsp;have been arrested in connection with the murder of a man at Sydney hotel.

Faith: Spirits lift in connection with home
We need to more adequately understand the spirituality of our unique place on earth.

FBI mulls connection between Super Bowl, CA fiber optic cable cuttings
... authorities, who have been baffled by more than a dozen attacks on San Francisco Bay Area data lines, are probing whether there is any connection ...

Tons Of Methane Are Spewing Out Of California, And There’s A Connection To The Governor
The out of control leak has forced thousands of residents to flee their homes. Gov. Brown's sister is a paid board member at the company that ...

Report: Man arrested in connection with Ashley Olsen's murder
Italian media agency reports that a non-EU male has been arrested in the murder of the 35-year-old American

Toyota and Kymeta rely on satellites to power this Mirai's data connection
They're hoping to achieve gigabit wireless speeds within the next few years.

Resources last updated: 1/20/2016 1:09:55 PM