Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
CONSTANT
-----------
namespace InternationalLaundry
{
    public class Constant
    {
        // SELECT AND EMPTY VALUE
        public static string EMPTY_LIST_ITEM = " ---Select--- ";
        public static string EMPTY_LIST_ITEM_VALUE = "0";

        public static string laundry_db = "InternationalLaundry.Properties.Settings.InternationallaundryConnectionString";
    }
}


CLASS LIBRARY
----------------     
   public Database db;

        public CustomerDataManagement(string csstring)
        {
            db = DatabaseFactory.CreateDatabase(csstring);
        }
        
 //VIEW CUSTOMER REPORT
 private const string USP_Get_Customer_Data = "USP_Get_Customer_Data";  


//VIEW CUSTOMER REPORT
        public DataSet SELECT()
        {
            try
            {
                DbCommand com = db.GetStoredProcCommand(USP_Get_Customer_Data);
                return db.ExecuteDataSet(com);
            }
            catch (Exception)
            {
                throw;
            }
        
        }

PRESENTATION LAYER
------------------------

using Ilc = CustomerManagement;

   public partial class CustomerApplication : Form
    {

        public Ilc.CustomerDataManagement obj;
     

        public CustomerApplication()
        {      
            InitializeComponent();
            Binddata();
        }
   private void CustomerApplication_Load(object sender, EventArgs e)
        {       
            obj = new CustomerManagement.CustomerDataManagement(Constant.laundry_db);

        }
        private void Binddata()
        {
          
                DataSet dsCustomers;                         ---------->PROBLEM
                dsCustomers = obj.SELECT();
                this.dataGridView1.DataSource = dsCustomers.Tables[0];
               
        }

PROBLEM IS:
---------------
object reference not set to an instance of an object
Posted
Updated 23-Mar-13 23:37pm
v2
Comments
Richard MacCutchan 24-Mar-13 4:36am    
What is the value of obj at this point?

It looks like obj isn't initialized when you try to use it in the Binddata method.


You initialize obj in the CustomerApplication_Load method (I dont see where it is called but, I guess it is an event-handler for the Load event). When you call Binddata in the constructor, the Load event hasn't raised yet...


Try to call Binddata in CustomerApplication_Load. Something like:


C#
private void CustomerApplication_Load(object sender, EventArgs e)
{ 
    obj = new CustomerManagement.CustomerDataManagement(Constant.laundry_db);
    Binddata();
}
 
Share this answer
 
Comments
Maciej Los 24-Mar-13 6:18am    
It could be the one of many reasons, because only the part of code was provided.
The obj is being called after the FormLoad Event. So what this means is that when you call binddata(); in the CustomerApplication Constructor the obj has not been set to the reference of an object yet because the FormLoad Event has not yet been fired. To fix this do as Shmuel stated above or in the CustomerApplication Constructor add obj = new CustomerManagement.CustomerDataManagement(Constant.laundry_db); before binddata(); and remove the FormLoad Event.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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