T
Tomas Kepic
Hi,
I have a problem with calling method or properties which is used in other
..aspx form. Is always throws me an error:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
I'm using something like this:
******************** "default.aspx.cs" **************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Oracle.DataAccess.Client;
namespace OraGenRep
{
public class Default : System.Web.UI.Page
{
private String _connStr;
private OracleConnection _conn;
protected System.Web.UI.WebControls.Label MyLabel;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
try
{
_connStr= "UserId=TESTUSER;DataSource=PO8;Password=aa;Persist Security
Info=true;";
_conn = new OracleConnection(ConnectionString);
Conn.Open();
Response.Redirect("main.aspx?");
}
catch(OracleException ex)
{
MyLabel.Text = ex.Message;
}
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
public String ConnectionString
{
get { return _connStr; }
}
public OracleConnection Conn
{
get { return _conn; }
}
}
}
******************************************************
main.aspx
******************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 OraGenRep.Include;
using Oracle.DataAccess.Client;
namespace OraGenRep
{
public class main : System.Web.UI.Page
{
protected Default def;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// !!! here is raised an error (if I uncomment next line
Label1.Text ...):
// Object reference not set to an instance of an object.
// Label1.Text = def.ConnectionString;
<<-------------------------
if (!Page.IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
String query = "select * from freespace";
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter(query, def.Conn);
// <<-here it fails again. def.Conn is null, Why, if it was innitialized in
"default.aspx" ???????
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
************************************
I want to know how can I use objects declared in "default.aspx" in
"main.aspx"? If I inicialize for example Conn in "default.aspx" how can I use
it in "main.aspx"?
Please help me someone what I'm doing wrong? I'm really beginner in ASP.NET.
I have a problem with calling method or properties which is used in other
..aspx form. Is always throws me an error:
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
I'm using something like this:
******************** "default.aspx.cs" **************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 Oracle.DataAccess.Client;
namespace OraGenRep
{
public class Default : System.Web.UI.Page
{
private String _connStr;
private OracleConnection _conn;
protected System.Web.UI.WebControls.Label MyLabel;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
try
{
_connStr= "UserId=TESTUSER;DataSource=PO8;Password=aa;Persist Security
Info=true;";
_conn = new OracleConnection(ConnectionString);
Conn.Open();
Response.Redirect("main.aspx?");
}
catch(OracleException ex)
{
MyLabel.Text = ex.Message;
}
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
public String ConnectionString
{
get { return _connStr; }
}
public OracleConnection Conn
{
get { return _conn; }
}
}
}
******************************************************
main.aspx
******************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 OraGenRep.Include;
using Oracle.DataAccess.Client;
namespace OraGenRep
{
public class main : System.Web.UI.Page
{
protected Default def;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
// !!! here is raised an error (if I uncomment next line
Label1.Text ...):
// Object reference not set to an instance of an object.
// Label1.Text = def.ConnectionString;
<<-------------------------
if (!Page.IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
String query = "select * from freespace";
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter(query, def.Conn);
// <<-here it fails again. def.Conn is null, Why, if it was innitialized in
"default.aspx" ???????
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}
************************************
I want to know how can I use objects declared in "default.aspx" in
"main.aspx"? If I inicialize for example Conn in "default.aspx" how can I use
it in "main.aspx"?
Please help me someone what I'm doing wrong? I'm really beginner in ASP.NET.