Calling Properties, Methods from other .aspx

Discussion in 'ASP .Net Web Controls' started by Tomas Kepic, Apr 29, 2005.

  1. Tomas Kepic

    Tomas Kepic Guest

    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.
    --
    Tomas
    Tomas Kepic, Apr 29, 2005
    #1
    1. Advertising

  2. Tomas Kepic

    Tomas Kepic Guest

    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" wrote:

    > 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.
    > --
    > Tomas
    Tomas Kepic, May 1, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    1
    Views:
    817
  2. Garth17
    Replies:
    3
    Views:
    3,030
    Brock Allen
    Mar 18, 2005
  3. Oliver Wong
    Replies:
    14
    Views:
    1,617
    Chris Uppal
    Jun 13, 2006
  4. Jim Cain
    Replies:
    7
    Views:
    142
    Brian Candler
    Jul 18, 2003
  5. Kenneth McDonald
    Replies:
    5
    Views:
    313
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page