Object reference not set to an instance of an object?

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

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...
--------------------
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()
{
//
// 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);

}
 
H

Hermit Dave

before you can use a connection object you need to do the following
SqlConnection myCon;
myCon = new SqlConnection("can hve the connection string here");
then use
myCon.open();

unlike c++, you have to explicity use new operator to create the object.
in C++ SqlConnection myCon; would have created the SqlConnection object on
stack
using new would have created it in heap.

in C# you can only create object in the heap and have to use 'new;

HTH
 

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

No members online now.

Forum statistics

Threads
473,800
Messages
2,569,656
Members
45,399
Latest member
JettTancre
Top