[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Passing Data with Server.Transfer - ASP.NET/C#

Posted on 2003-12-10
19
Medium Priority
?
4,637 Views
Last Modified: 2012-08-14

I've seen a few articles on passing data with the Server.Transfer method but they don't address my problem.

I will have many pages that will Server.Transfer to the same page, lets call these PageA, PageB, PageC, etc.  There receiving page we'll call Page2. So, I may have the following transfer situations:
PageA to Page2
PageB to Page2
PageC to Page2,
etc.

The sending pages will be made up of forms that all search different database tables.  They have their own logic to build SQL clauses. What I want to do is pass 2 strings, the SQL clause and the table name.  My problem is, all the examples I've seen cast the Context.Handler to the page that was doing the sending. I will not know this before hand.

So how do I pass two strings in C# with the above example?

-cp-
0
Comment
Question by:cpetenes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 2
19 Comments
 
LVL 8

Expert Comment

by:Dranizz
ID: 9914526
Set to public attributes for your 2 strings in the Page1 class

like

Public String Query;
Public String TableName;

server.Transfer("Page2.aspx",true);

And at page2 load you'll do this

System.Collections.Specialized.NameValueCollection ColForm;
'Get the data from the source form
colForm=Request.Form

and gets the value from the source form

Query =  colForm["Query"]

0
 
LVL 35

Expert Comment

by:YZlat
ID: 9914555
just put your strings into Context.Item dictionary on your PageA inside Page_Load on Page PostBack:

Sub Page_Load(source as Object, e as EventArgs)
      Dim strFirst as string="SELECT * FROM...";
      Dim strSecond as string="myTable";

      ....
      If Page.IsPostBack Then
            Context.Items["FirstString"] = strFirst ;
            Context.Items["SecondString"] = strSecond;
            Server.Transfer("Page2.aspx");
      End If
End Sub

then on your Page2 you'll retrieve your strings inside the Page_Load procedure using Request object

and you'll do that on every page.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 9914828
replace Server.Transfer("Page2.aspx");

with
Server.Transfer("Page2.aspx", true);
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:cpetenes
ID: 9915786

Hmmm, I get the general concept of both strategies.  I'm having trouble getting either to work.

With YZLat's method:

PageA.aspx.cs:

private void btnSearch_Click(object sender, System.EventArgs e)
{
   // do stuff ...
   Context.Items["FirstString"] = strFirst ;
   Context.Items["SecondString"] = strSecond;

   Server.Transfer("Page2.aspx", true);
}

Page2.aspx.cs:

private string SqlParameters, tableName;

private void Page_Load(object sender, System.EventArgs e)
{

   SqlParameters =  Request.Form["FirstString"];
   tableName = Request.Form["SecondString"]
                  
   lblParam.Text = SqlParameters;
   lblTable.Text = tableName;
}

Is it any better to just use two Session variables?


0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9915848
It depends if you want to waste server memory.

What about my strategie, did you try?
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9930019
Dranizz:

Yes, my first goal is to waste as much memory as possible.

J/K.

For your method I tried:

PageA.aspx.cs:

public string SqlParameters;
public string tableName;

private void btnSearch_Click(object sender, System.EventArgs e)
{
  // ...do stuff

   SqlParameters = "parameter string";
   tableName = "aTableNameString";

   Server.Transfer("PrevPurch.aspx", true);

}

Page2.aspx.cs:

private void Page_Load(object sender, System.EventArgs e)
{
   System.Collections.Specialized.NameValueCollection colForm;
   colForm = Request.Form;

   lblParam.Text = colForm["SqlParameters"];
   lblTable.Text = colForm["tableName"];

}

I'm just trying to display the passed data in a lable.  It didn't work (compiles fine). I'm not sure if it's just something I'm doing wrong.

Is there any downside to just using two session variables?


0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930035
just performance
it will slow down a little bit.

I'm checking for the transfer problem
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930045
add EnableViewStateMac="false"  to page directive, like this

<%@ Page Language="vb" AutoEventWireup="false" EnableViewStateMac="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication23.WebForm1"%>

this will probably make the transfer work
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930048
check that ViewState is set to TRUE, cause It won't work if not set true
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930273
so?
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9930449
Ok,

For the EnableViewStateMac, which file, the sending or receiving? (I put it in both).

Check that ViewState is set to TRUE where?  I know you can set that for individual controls, but I'm not sure what you mean.  

Thanks for the continued help .....

0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930495
Hey,I'm there for the points! joke

>For the EnableViewStateMac, which file, the sending or receiving? (I put it in both).
Hum, for sending, but if you plan to use transfer in the receiving ones too, then set it there too.

>Check that ViewState is set to TRUE where?  I know you can set that for individual controls, but I'm not sure what >you mean.

Viewstate for the forms. It,s in the properties of the forms. But you can set it in the web.config. and into page directive
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9930657

I'm still having problems.  

ViewState for a form is TRUE by default, is it not?

Let me ask you this,  ... , we're trying to pass the strings along in a "form-like" fashion, correct?  In my sending page, PageA above, how does just declaring two public variables make it part of the form to be accessible by colForm in Page2?
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9930745
should be, but maybe you should set it.

well, it's like getting a reference of the PageA class.



Try putting the values in controls, like a textbox, and putting them invisible but with viewstate enabled on the controls themself. So that we use them like variables, but not visible to users. Maybe it has to be on controls, because of viewstate.

then get them with the name of the control, like this
 lblTable.Text = colForm["txtBox"];

Try it, and tell me if it's work.
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9931092

That didn't work either. What does work though is:

Create a "get" procedure on page 1 like:

public string getstuff
{
   get
   {
      return tbPass1.Text;
   }
}

Then on the second page:

Page1 p = Context.Handler as Page1;

lblTable.Text = p.getstuff;


Maybe you just can't pull right off a form with a Server.Transfer.  You ideas led me to a solution, so you get the points :-)
0
 
LVL 8

Accepted Solution

by:
Dranizz earned 750 total points
ID: 9931245
It has to work, I mean the transfer method, I'v took that examplt in a Microsoft book :)
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9931279

Good point! It makes comlete sense to me. I'm sure there is something small I'm doing.

I do have a solution, but I'll keep tooling with it out of pure curiosity.
0
 
LVL 8

Expert Comment

by:Dranizz
ID: 9931301
Ok, I've tested it myself, it's working

Here is what I did

-created 2 textbox and enabling viewstate for each
-add EnableViewStateMac="false" in the page directive
-Added server.Transfer("webform3.aspx",true) in a button event
-in the receiving form, in the load event, request.form("txtBoxName").tostring
 and I got what I typed in the textbox of my other form.
0
 
LVL 1

Author Comment

by:cpetenes
ID: 9931920
Ahhh, that pesky little ".tostring" is what was missing ....
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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