Calling Properties, Methods from other .aspx

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

Tomas Kepic

Hi all, I've found an answer for my previous problem.

I you want to redirect to another page and still access to objects in
"original" page you should use:

Server.Transfer("main.aspx");

and then in "main.aspx" page in Page_Load assign it:

if (!Page.IsPostBack)
{
Default originalPage = (Default) Context.Handler; // here Default is
name of my Original page [ OriginalPage originalPage = (OriginalPage)
Context.Handler; ]
}


Am I right, or is here some other solution?

Thanx, Tomas



Tomas Kepic said:
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.
 

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,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top