473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to call a function from another page

Hi,

I have the following private function in one page:

private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnecti on");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
default: //access
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;

}
return sConnect;
}

After changing it to public, another page still can not call it. I should
duplicate the same function.

Any suggestions? thanks. -Dale
Nov 17 '05 #1
4 16349
Did you add (using) in top of the page you want to include that function into?

If the function (Connect) is included within a namespace called
(HelloNameSpace) for example, you should add this line in the top of the page
that should re-use that function

using HelloNameSpace;

When you want to call this function you should call it using the name of the
class that includes it (if any).

Hope this could help you.

Nov 17 '05 #2
Did you add (using) in top of the page you want to include that function into?

If the function (Connect) is included within a namespace called
(HelloNameSpace) for example, you should add this line in the top of the page
that should re-use that function

using HelloNameSpace;

When you want to call this function you should call it using the name of the
class that includes it (if any).

Hope this could help you.

Nov 17 '05 #3
Dale,

I think you should make the method not only public, but also static. This
way you'll be able to call it without having to refer to a concrete instance
of the page. For instances, if you make:

public class FirstPage : Page
{
public static string Connect()
{
// method code here
}
// etc...
}

Then you can call this function from ANY other page of your site by using

string cn = FirstPage.Connect();

Regards - Octavio

"dale zhang" <da*******@discussions.microsoft.com> escribió en el mensaje
news:AB**********************************@microsof t.com...
Hi,

I have the following private function in one page:

private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnecti on");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
default: //access
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;

}
return sConnect;
}

After changing it to public, another page still can not call it. I should
duplicate the same function.

Any suggestions? thanks. -Dale

Nov 17 '05 #4
Octavio,

Thank you very much. I got this working by using class (a bit OO
programming). I think your method is simpler, should be better.

-Dale

"Octavio Hernandez" wrote:
Dale,

I think you should make the method not only public, but also static. This
way you'll be able to call it without having to refer to a concrete instance
of the page. For instances, if you make:

public class FirstPage : Page
{
public static string Connect()
{
// method code here
}
// etc...
}

Then you can call this function from ANY other page of your site by using

string cn = FirstPage.Connect();

Regards - Octavio

"dale zhang" <da*******@discussions.microsoft.com> escribió en el mensaje
news:AB**********************************@microsof t.com...
Hi,

I have the following private function in one page:

private string Connect()
{
string sConnect;
// this value could go directly in the Global.asax.vb declarations
switch (((string)(Application["DBType"])).ToLower())
{
case "sqlserver":
sConnect = ConfigurationSettings.AppSettings.Get("SQLConnecti on");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;
default: //access
sConnect = ConfigurationSettings.AppSettings.Get("AccessConne ction");
// Return "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\DBs\PasswordProtect.mdb;Persist Security Info=False"
break;

}
return sConnect;
}

After changing it to public, another page still can not call it. I should
duplicate the same function.

Any suggestions? thanks. -Dale


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Titus Cheung | last post by:
Hello, I want to write some code such that it'll update (ie insert, delete, etc) some data to/from a mySQL database whenever a user hit an HTML form button (ie Submit). Now what I find annoying...
2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
1
by: cheezebeetle | last post by:
ok, so I am having problems passing in an ASPX function into the Javascript in the codebehind page. I am simply using a confirm call which when they press "OK" they call this ASPX function, when...
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
6
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I...
4
by: ruca | last post by:
Hi, Can anyone tell me how can I call a function of my Web Page from javascritp???? I have a grid that when I click in a cell I want to call another function present in that same page? It is...
3
by: Dean Slindee | last post by:
Would like to call the TabShow function on WebForm InOutHost from another WebForm in same project, but cannot figure out how to get a reference. Can this be done? In this statement: Call...
3
by: KaNos | last post by:
Hi, "robot script pages" are html+javascript pages, can be played in aspx player. So in this tech, robot call aspx player's function (an interface is sheared) and wait a result synchronously with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.