Object reference not set to an instance of an object

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);

}
 
J

Joe

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.
 
M

Mark Wilson

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top