Click here to Skip to main content
15,885,951 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
How to avoid the Object reference not set to an instance error? I am getting this error only after setup and deployment. It works fine in debug and release mode inside the visual studio environment? Initially the control starts from getproject() function. Then, it calls the property obOpenedSqlConnection.


public class OutlookDataAccess
    {

        #region Public Methods and Properties
        SqlConnection _sqlConnection; //= new SqlConnection(); // null;
        SqlCommand obSqlCommand;
        SqlDataAdapter obSqlDataAdapter;

        SqlParameter parm;

        //SqlConnection _sqlConnection = new SqlConnection();

        public SqlConnection obOpenedSqlConnection
        {
            get
            {
                MessageBox.Show("Before Open");
                if (_sqlConnection != null)
                {
                    MessageBox.Show("inside Open");
                    if (_sqlConnection.State == ConnectionState.Closed)
                    {
                        try
                        {
                            MessageBox.Show( "Before Open");
                           _sqlConnection.Open();                         
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Open Exception");
                            throw ex;
                        }
                    }
                    return _sqlConnection;
                }
                else
                { 
                    var connectionString = ConfigurationManager.ConnectionStrings["OutlookConnectionStringLocal"].ConnectionString;
                    
                    MessageBox.Show("Before Connection creation");
                    _sqlConnection = new SqlConnection(connectionString);
                    MessageBox.Show("After Connection creation");
                    return _sqlConnection;  
                }
            }
        }

        private void CloseSqlConnection()
        {
            if (_sqlConnection != null)
            {
                if (_sqlConnection.State == ConnectionState.Open)
                    _sqlConnection.Close();
            }
        }

        private void ClearCommandParameters()
        {
            if (obSqlCommand != null)
            {
                obSqlCommand.Parameters.Clear();
            }
        }

        private DataTable GetOutputDataTable(SqlDataAdapter sqlDataAdapter)
        {
            DataTable dataTable = new DataTable();
            if (sqlDataAdapter != null)
            {
                sqlDataAdapter.Fill(dataTable);
                ClearCommandParameters();
            }
            return dataTable;
        }

        public DataTable GetProject(string emailId)
        {
            try
            {
                MessageBox.Show("inside get project");
               
                obSqlCommand = new SqlCommand("sp_GetClientContact", obOpenedSqlConnection);
                //MessageBox.Show(emailId, "getprojectid");
                obSqlCommand.CommandType = CommandType.StoredProcedure;
                obSqlCommand.Parameters.AddWithValue("@emailId", emailId);
                obSqlDataAdapter = new SqlDataAdapter(obSqlCommand);


            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString(), "invalid object");
                throw exp;

            }
            finally
            {
                CloseSqlConnection();

            }
            return GetOutputDataTable(obSqlDataAdapter);
        }
}
Posted
Updated 24-Feb-17 0:23am
Comments
Santhosh Kumar Jayaraman 7-Aug-12 8:23am    
Where you get this error?which line?

My guess to you problem is that
var connectionString = ConfigurationManager.ConnectionStrings["OutlookConnectionStringLocal"].ConnectionString;
is returning null as your live config probably differs from you debug one.


Personally I'd create a connection for each call and put it up for Garbage collection at the end of the call, though there are reasons for not doing this. There are a few other thing wrong with your class IMO:

1.
obOpenedSqlConnection
Should be a method.
2. Don't catch the gerneic exception.
3. Consider a using statement when you istantiate your commant, this will auto-close your connection, but may also dispose it.
4. Consider methods to: Create the connection, Create a new command object and fill its parameters (the clearig feels brittle).
5. Creating the DataAdapter in the
GetOutputDataTable
reducing its scope.
 
Share this answer
 
create object of _sqlConnection using new keyword
C#
SqlConnection _sqlConnection = new SqlConnection();
 
Share this answer
 
Comments
Gokulnath007 7-Aug-12 8:43am    
_sqlConnection = new SqlConnection(connectionString);

Am using this here already?
The best way to avoid this error is, always create a new instance of the objects or classes which you are going to use. Sometimes we use to forget this little point and without initializing and creating instance of the object we use to use this. That's why this kind of exception comes.

In your condition check for the coding where you are using null objects to assign values. Trace the code carefully.

--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900