473,396 Members | 1,864 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,396 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 16362
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.