Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys now am trying to check if the user exist in a Database and yes the code Behind works. The problems comes when Im using a Class, I get the following error: "Object reference not set to an instance of an object."
And it highlights this line in the code: "reader = dbCmd.ExecuteReader();"

Below is the code for my Class

C#
public class Class1
{
    OleDbConnection dbConn;
    DataSet ds;
    OleDbDataAdapter dbAdapter;
    OleDbCommand dbCmd;
    DataTable dataTable;
    OleDbDataReader reader;
    public void dbConnection()
    {
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= H:/SCHOOL OF ICT/ITP 2000/Semester 2/Database System/App_Data/dbICTWorxScheduling.mdb ";
        try
        {
            // set up and open connection
            dbConn = new OleDbConnection(connString);
            dbConn.Open();
        }
        catch (OleDbException exp)
        {
            exp.ToString();
        }
       
    }
	public Class1()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable CognosGrid()
    {
       
        dbConnection();
        string sql = "SELECT * FROM ICTWorxEducationMembers";
        dbAdapter = new OleDbDataAdapter();
        dbCmd = new OleDbCommand(sql, dbConn);
        dbAdapter.SelectCommand = dbCmd;
        reader = dbCmd.ExecuteReader();
        dataTable = new DataTable();
        reader.Close();
        dbConn.Close();
        dbAdapter.Fill(dataTable);
        
        return dataTable;
        
    }
}


This is the code behind the button

C#
protected void btnTest_Click(object sender, EventArgs e)
    {
       

        bool found = false;

        DataTable dataTable = myClass.CognosGrid();
         reader = dbCmd.ExecuteReader();

        while (reader.Read())
        {
   if (txtusername.Text == reader.GetString(0) &&     
        txtpassword.Text == reader.GetString(1))
            {
         MessageBox.Show("Welldone", " Found");

                Response.Redirect("Default.aspx");
                found = true;
            }

        }
        if (!found)
        {

            MessageBox.Show("The account does not exist. Please Register first", "Not Found");

        }

    }
}


Edit TR : Added <pre> block to clean up first code snippet.
Posted
Updated 21-Sep-10 2:49am
v2

yes, the error line "Object reference not set to an instance of an object" suggests that objects are not instantiated, before they are being used. You can try two things here. Either make your method "CognosGrid()" static so that you can call it directly :

1. public static DataTable CognosGrid()
{
//your code
}



or make an object of the class in which the method resides and then use that object to call that particular method :




C#
protected void btnTest_Click(object sender, EventArgs e)
    {

        bool found = false;

          Class1 objClass= new Class1();                 //<---
        DataTable dataTable = objClass.CognosGrid();     //<---
         reader = dbCmd.ExecuteReader();
        while (reader.Read())
        {
   if (txtusername.Text == reader.GetString(0) &&
        txtpassword.Text == reader.GetString(1))
            {
         MessageBox.Show("Welldone", " Found");
                Response.Redirect("Default.aspx");
                found = true;
            }
        }
        if (!found)
        {
            MessageBox.Show("The account does not exist. Please Register first", "Not Found");
        }
    }


}


Hope, this would solve your problem. Please provide vote if this was useful.


Anurag
@cheers@
 
Share this answer
 
hey buddy , check this code . ur problem is solved. still if u find any prob let me know. and dont forget to vote if my answer is corret ;)

OleDbConnection dbConn;
       DataSet ds;
       OleDbDataAdapter dbAdapter;
       OleDbCommand dbCmd;
       DataTable dataTable;
       OleDbDataReader reader;
       public void dbConnection()
   {
       string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= H:/SCHOOL OF ICT/ITP 2000/Semester 2/Database System/App_Data/dbICTWorxScheduling.mdb ";
       try
       {
           // set up and open connection
           dbConn = new OleDbConnection(connString);
           dbConn.Open();
       }
       catch (OleDbException exp)
       {
           exp.ToString();
       }

   }

       public DataTable CognosGrid()
   {

       dbConnection();
       dataTable = new DataTable();

       string sql = "SELECT * FROM ICTWorxEducationMembers";
       dbAdapter = new OleDbDataAdapter();
       dbCmd = new OleDbCommand(sql, dbConn);
       dbAdapter.SelectCommand = dbCmd;

       dbAdapter.Fill(dataTable);

       dbConn.Close();
       return dataTable;

   }


protected void btnTest_Click(object sender, EventArgs e)
    {


        bool found = false;

        DataTable dataTable = myClass.CognosGrid();

        foreach (DataRow dr in dataTable.Rows)
        {
            if (txtusername.Text == dr["ColumnName"].ToString() && txtpassword.Text == dr["ColumnPassword"].ToString())
            {
                MessageBox.Show("Welldone", " Found");
                found = true;
                Response.Redirect("Default.aspx");
                
            }

        }

        if (!found)
            MessageBox.Show("The account does not exist. Please Register first", "Not Found");
        
    }
 
Share this answer
 
Comments
Anele Ngqandu 23-Sep-10 5:24am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Anele Ngqandu 23-Sep-10 5:24am    
Hey thanx alot it worx!!
Make sure that before clicking on your Test button your dbCmd object is created and it is not null , so write db connectivity code in !ISPOSTBACK
 
Share this answer
 
Many Mistakes in the Class.

C#
catch (OleDbException exp)
{
exp.ToString();
}

Use Alert or Lable Text to Display the Catched Message,

MIDL
reader = dbCmd.ExecuteReader();
dataTable = new DataTable();
reader.Close();
dbConn.Close();
dbAdapter.Fill(dataTable);


Close the connection after dbAdapter Fills Data to DataTable.

C#
reader = dbCmd.ExecuteReader();


No Need of this if you are Filling Data to Datatable using Adapter.

and Finally your Error Object reference not set to an instance of an object.

That is because Some of the Objects are not initialized and you are attempting to use that object.

Hope it Helps,

Mark as answer if Liked.
 
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