Object reference not set to an instance of an object

  • Thread starter Thread starter Mark Wilson
  • Start date Start date
M

Mark Wilson

For some reason I am getting this error even though (as you can see from the
code in the Initialize Component procedure) the object reference has been
established... I am at my wit's end.. Thanks for any advice...

--------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


public class x : System.Web.UI.Page
{

protected System.Data.SqlClient.SqlConnection sqlConnection1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here


sqlConnection1.Open(); <-- Error occurs on this line
}

private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "packet size=4096;user id=XXX;data
source=XXX;persist security info=False;initial catalog=XXX;password=XXX";
this.Load += new System.EventHandler(this.Page_Load);

}
 
I think this error is occurring because you are never calling the
InitializeComponent() function, and it means the reference has not been
create when you try to open the connection.
 
Hi Joe,

I forgot to copy and paste the page Init code...here it is:

-------------------
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();

base.OnInit(e);
}
 
Back
Top