By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,850 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,850 IT Pros & Developers. It's quick & easy.

How to call a function from another page

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.