Solved

Server.Transfer("...aspx") Object of instance not sent

Posted on 2004-09-11
15
207 Views
Last Modified: 2010-04-15
I have three aspx pages. They are made-up as follows:

1. A login page (with code-beihind class Login.cs)
2. A mailbox page (with code-beihind class mailbox.cs)
3. A message page (with code-beihind class message.cs)

At present the user logs into to a POP server at the login page and when clicking "log-in" is Server.Transfered to the mailbox page which consists of a datagrid displaying all the emails in the user's inbox. The datagrid has a LinkButton for each message and when clicked it should load the message page with the appropriate email displayed. Instead, when clicking any linkButton I get the well-known (Object reference not set to an instance of an object) which refers to the mailbox page NOT the message page, despite the fact that the mailbox page has loaded quite happily and has already successfully carried out the command that the error is referring to!

Any help would be greatly appreciated.
0
Comment
Question by:hive429
  • 10
  • 4
15 Comments
 
LVL 13

Expert Comment

by:BlackTigerX
Comment Utility
places to look:
- the message page OnPageLoad event
- mailbox page OnPageLoad (on the IsPostBack, if you have that) i.e.
if (IsPostBack) //this gets executed every time they click something in the page that is server side
{
}
- mailbox page onButtonClick if you defined those events by code
0
 

Author Comment

by:hive429
Comment Utility
BlackTigerX,
I don't actually have an OnPageLoad event for any of my pages because I never managed to get it working in my code-behind classes. Would you mind putting the bascis on here?
Thanks
0
 
LVL 5

Expert Comment

by:tomasX2
Comment Utility
           private void Page_Load(object sender, System.EventArgs e)
            {
                  if( ! this.IsPostBack ) // This is important because if this is not checked then every thing will be done over uneccessarily...
                  {
                        // Do stuff to load the page for the first time... i.e. fill the mailbox datagrid.
                  }
            }

            private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
                  if( e.CommandName == "Select" )
                  {
                        string mailID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
                        Response.Redirect("message.aspx?mailid=" + mailID);  // Or server.transfer...
                  }
            }
0
 

Author Comment

by:hive429
Comment Utility
Thanks tomasX2, I'll give that a try and will post back around 10 BST.
0
 

Author Comment

by:hive429
Comment Utility
This is my event handler for the data grid:

public void openEmail(Object sender, DataGridCommandEventArgs e) {

  if (((LinkButton)e.CommandSource).CommandName == "selectEmail") {
    Context.Items["selectedEID"] = e.Item.Cells[0].Text;
    Server.Transfer("message.aspx");
  }
 
}

Even if I comment out that code I get the same "Object of instance..." error

I also know its not the event handler because if I carry out the login process individually on each page it works fine.

Also, my Page_Load isn't working. Here is the header I have in message.aspx:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" AutoEventWireup="false" Inherits="MailApp" Src="MailApp.cs" EnableViewStateMac="false" %>

And here is your Page_Load with my stuff included:

private void Page_Load(object sender, System.EventArgs e)
          {
               if( ! this.IsPostBack ) // This is important because if this is not checked then every thing will be done over uneccessarily...
               {
                    // Do stuff to load the page for the first time... i.e. fill the mailbox datagrid.
          stream = (NetworkStream)Context.Items["stream"];

                    // Method that fills the data grid which works fine when including login procedure as part of this pages code-behind class
          retrieveAllEmails();
               }
          }

Any ideas?
0
 
LVL 5

Expert Comment

by:tomasX2
Comment Utility
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" AutoEventWireup="false" Inherits="MailApp" Src="MailApp.cs" EnableViewStateMac="false" %>

Looks like you might need to add the Codebehind="WebForm1.aspx.cs" attribute...
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>


also a good way to do the event for the datagrid... where "ID" is the column in the dataset that has your id

Add a DataKeyField element to the datagrid....
<asp:DataGrid
id=DataGrid1
style="Z-INDEX: 101; LEFT: 148px; POSITION: absolute; TOP: 108px"
runat="server"
Width="428px"
DataKeyField="ID">
</asp:DataGrid>

then

public void openEmail(Object sender, DataGridCommandEventArgs e)
{
              if( e.CommandName == "selectEmail" )
               {
                    string mailID = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
                    Context.Items["selectedEID"] =mailID ;
                    Server.Transfer("message.aspx");
               }
}


0
 

Author Comment

by:hive429
Comment Utility
Hi and thanks for the swift response,

The code-behind file is actually MailApp.cs for the file mailbox.aspx so is it not right to have Src="MailApp.cs"?

Are "Src" and "Codebehind" not interchangeable.

Thanks for the DataKeyField method, I'll give that a try but it isn't intended to solve this issue is it?

Why would the error refer to somewhere in the mailbox.aspx page when that page has been loaded? It must be trying to reload the page or something similar when I do Server.Transfer("message.aspx"). Is this perhaps where the mailbox page is attempting to be reloaded? Interestingly, when the LinkButton is clicked the page attempts to change to mailbox.aspx NOT message.aspx as I understand this is the nature of the Server.Transfer() method. I think the Page_Load could well be the problem but it still has no effect at all when I put it in my class. What would be stopping it from being accessed?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:hive429
Comment Utility
Ok I know why my Page_Load wasn't working. I took out AutoEventWireup="false". But if I do that I get exactly the same problem as before but a page early so that when I try to login the Object of instance... error occurs.

I'm definitely missing something with this Page_Load stuff.

Should all my code-behind classes use the Page_Load method and when should the AutoEventWireup flag be enabled/disabled and what about the EnableViewStateMac.

Sorry to sound so amateur, I've got quite a bit of programming experience but very little knowledge of web-servers and asp.net is my first effort at learning server-side programming etc.

Many thanks for your patience.
0
 

Author Comment

by:hive429
Comment Utility
Ok sorry again, I reckon it was removing the EnableViewStateMac="false" that did it. That's what is preventing my Page_Loads from working. I think that is due to the nature of the Server.Transfer() method.
0
 
LVL 5

Expert Comment

by:tomasX2
Comment Utility
are you just creating a class MailApp.cs and putting that as the source.... how did you create MailApp.cs

try creating a new web application project...

look at the html view of webform1.aspx and the webform1.aspx.cs....

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>

try putting code into the pageload and putting a breakpoint to see if it executes.

0
 

Author Comment

by:hive429
Comment Utility
I presume you are using Visual Studio .net. I'm doing all my coding from scratch in dreamweaver. Daft I know but I don't have VS.

I think the main point is that all the pages with their code-behind classes work fine as long as I login to the POP server for every page. It is only when I try to pass the NetworkStream received from the POP server between pages (and therefore only logging in once) that I have the problem. I think I'm going to have to pull my code to bits and start again.
0
 

Author Comment

by:hive429
Comment Utility
Ok I'm finally making progress. It seems that we are both kinda right but I haven't completely grasped it yet.

I can now login fine and display my mailbox (same as before).
Now when I click on the link button the error is thrown for the message.aspx page not the mailbox.aspx page (this was solved by adding the this.IsPostBack condition to the mailbox.aspx page.

I also know that it is the NetworkStream object that is causing the error in the message.aspx page as I have commented out all other code and am still not having any joy getting a response from it.

I reckon we're nearly there tho.
0
 

Author Comment

by:hive429
Comment Utility
Ok here's a more direct question for once.

When putting objects into the Context.Items[] "store" how often do you have to do it?
Once? Every page?

Here's what I have at the moment:

Login.cs (after the user has entered username and password and then clicks login"):
  Context.Items["stream"] = stream;
  Server.Transfer("mailbox.aspx", true);

MailApp.cs (when the page loads):
  stream = (NetworkStream)Context.Items["stream"];
  retrieveAllEmails();
MailApp.cs (when a LinkButton is clicked):
  Context.Items["selectedEID"] = e.Item.Cells[0].Text;
  Server.Transfer("message.aspx, true");

Message.cs (when the page loads):
  stream = (NetworkStream)Context.Items["stream"];
  // This next line wil cause the error by attempting to use the stream object
  response = getResponse(stream);

So much for direct...

Any ideas?
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 50 total points
Comment Utility
Context.Items[] store is only for the current request....

You could try to use a Session object instead.

MailApp.cs (when the page loads):
  stream = (NetworkStream)Context.Items["stream"];
Session["stream"] = stream;
  retrieveAllEmails();
MailApp.cs (when a LinkButton is clicked):
  Context.Items["selectedEID"] = e.Item.Cells[0].Text;
  Server.Transfer("message.aspx, true");


Message.cs (when the page loads):
  stream = (NetworkStream)Session["stream"];
  // This next line wil cause the error by attempting to use the stream object
  response = getResponse(stream);
0
 

Author Comment

by:hive429
Comment Utility
Spot on, I haven't yet come across Session objects. I only wish I had more points to give you!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now