Saturday, March 24, 2012

Passing variables between pages [CASE CLOSED]

Hi,
I just wanted to know how can i pass my variables from one web page to the other?
For example, I want to pass the server string on the 1st page to other pages. How do i go around doing it?
Pls helpPassing Parameteres from One Page to another (http://aspnet.4guysfromrolla.com/articles/020205-1.aspx)
Thanks a lot Ali. But I don't really understand C#. Is it possible to explain in VB codes? :blush:
Thanks a lot Ali. But I don't really understand C#. Is it possible to explain in VB codes? :blush:
Ok the easiest way to pass data between pages is to use Query String. Suppose you have two variables ([i] firstName and lastName[i]) on Page1 and you want to pass them onto page2, then on Page1 you can build a url like this Dim redirectUrl As String
redirectUrl = "Page2.aspx?firstName=" & firstName & ";&lastName=" & lastName" And then use the redirect method of response like this Response.Redirect(redirectUrl) And in page2 you can access the querystring variables like this Request.QueryString("firstName")

Does this make any sense?
Ideally, connection strings should be held in the web.config file, which looks something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultLanguage="vb" debug="true" />
</system.web>
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI;uid=sa;pwd=sa;" />
</appSettings>
</configuration>

You need to replace the value of the ConnectionInfo key with your own connection string (and you'll probably want to turn debug off at some point, bearing in mind that the settings in this file apply to all of your pages).

You then retrieve this value in your pages using:
ConfigurationSettings.AppSettings("ConnectionInfo")

However, to just pass the contents of a variable (eg. strForeName = "Jim", strSurname = "Jones"), then you can use the QueryString.

First, when you want to get to the new page, you use:
Response.Redirect("some_page.aspx?forename=" & strForeName & "&surname=" & strSurname)

These variables can then be retrieved in some_page.aspx using this syntax:

Dim strForeName As String = Request.QueryString("forename")
Dim strSurname As String = Request.QueryString("surname")

This can be extended for more variables just by extending the line you use when you redirect to some_page.aspx (eg. "some_page.aspx?var1=value&var2=value&var3=value"). Note the use of the question mark for the first value and ampersand for the following values.
Thanks a lot dude! You really solved my problem.
Greatest thanks to Ali. You're the man! :bigyello: :bigyello:

0 comments:

Post a Comment