What may cause connection to be null and How to reuse null reference object?

233 views Asked by At

If i face null reference exception in code like that :

  DBConnection ifx_conn = new DBConnection(con, false);
  Dictionary<string, string> paramList1 = new Dictionary<string, string>();
  paramList1.Add("from_date", from_date.ToShortDateString());
  paramList1.Add("to_date", to_date.ToShortDateString());


  string cmdText = "select * from permission where emp_num in( " + emplyeeRange + " ) and perm_date>=? and perm_date <=?";
  DataTable permissionDT = ifx_conn.Return_DataTable(cmdText, CommandType.Text, paramList1);//The Exception 


NOTES :

  • This's a part of a long method .
  • I use the same connection ifx_conn with multiple queries.
  • This method exist in loop of 10 iterations .
  • Sometimes it works without exceptions and sometimes after a while i get Null Reference Exception .

MY DBConnection :

public class DBConnection
    {
        #region parameters and properties

            IfxConnection connection;
            IfxCommand command = new IfxCommand();
            IfxTransaction m_current_trans;
            IfxDataReader DR;
            IfxParameter param;
            ConnectionState connectionstate;
            string connection_name;
            //CONSTANT FOR GENERAL EXCEPTION
            int ExCodeConst = -111111;

            public enum stmtOrder
            {
                First = 1,
                inBetween = 2,
                Last = 3,
            }
            public IfxConnection Connection
            {
                get { return connection; }
                set { connection = value; }
            }
            public IfxTransaction current_trans
            {
                get { return m_current_trans; }
                set { m_current_trans = value; }
            }
            public ConnectionState connectionState
            {
                get { return connectionstate; }
                set { connectionstate = value; }
            }
            public string ConnectionName
            {
                get { return connection_name; }
                set { connection_name = value; }
            }

        #endregion

        #region Connection Creation and handeling

                private void Set_Isolation(bool with_transaction)
                {
                    try
                    {
                        Open_Connection();
                        command = new IfxCommand("SET ISOLATION TO DIRTY READ");
                        command.Connection = connection;
                        command.ExecuteNonQuery();
                        if (!with_transaction)
                        {
                            connection.Close();
                            connectionstate = ConnectionState.Closed;
                        }
                    }
                    catch (Exception ex)
                    {
                        ErrMapping.WriteLog(ex.Message);
                    }
                }
                private void Open_Connection()
                {
                    try
                    {
                        if (connection.State == ConnectionState.Closed)
                        {
                            connection.Open();
                            connectionstate = ConnectionState.Open;
                        }

                    }
                    catch (Exception ex)
                    {
                        ErrMapping.WriteLog(ex.Message);
                    }
                }

                /// <summary>
                /// constructor
                /// </summary>
                /// <param name="ConnectionName">Connection Name in your application web.config file in ConnectionStrings section  </param>
                /// <param name="with_transaction">True  : if you will use transaction
                ///                                False : if you will not use transaction </param>
                public DBConnection(string ConnectionName, bool with_transaction)
                {
                    connection = new IfxConnection(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
                    this.connection_name = ConfigurationManager.ConnectionStrings[ConnectionName].Name;
                    command = new IfxCommand();
                    Set_Isolation(with_transaction);
                }
                /// <summary>
                /// Begin Transaction
                /// </summary>
                public void Begin_Transaction()
                {
                    if (this.connection.State == ConnectionState.Open)
                    {
                        this.current_trans = this.connection.BeginTransaction(IsolationLevel.Serializable);
                    }
                }
                /// <summary>
                /// Close Connection
                /// </summary>
                public void Close_Connection()
                {
                    if (connection!= null && connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                        connectionstate = ConnectionState.Closed;
                    }
                }
                /// <summary>
                /// Destructor
                /// </summary>
                 ~DBConnection()
                {
                    Close_Connection();
                     if(connection!= null)
                    connection.Dispose();
                }
        #endregion
  }

What may cause this exception ?and how to resume after failure ?

0

There are 0 answers