Monday, March 26, 2012

Object Reference not set to an instance

I am getting this error: " Object Reference not set to an instance "

User Control Code

// returns DataSet
public SqlDataReader GetData()
{

SqlCommand myCommand = new SqlCommand("SELECT * FROM Categories",myConnection);

myCommand.CommandType = CommandType.Text;

myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
return dr;

}
<code
Aspx Page Code

<code
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
WebUserControl1 wc = new WebUserControl1();
myDataGrid.DataSource = wc.GetData();
myDataGrid.DataBind();

}
}

Where u have defined myConnection object?

Is it accessible in public SqlDataReader GetData() function?
Where ismyconnection variable declared and instantiated? It is the suspect because I don't see its declaratiion and instantiation in the local scope of GetData method.
Hi,

Well I draged and Dropped the myConnection object from the ToolBox. Which I guess makes it protected ? Is this wrong ?

0 comments:

Post a Comment