How to call a function from another page

G

Guest

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("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// 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("AccessConnection");
// 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
 
G

Guest

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.
 
O

Octavio Hernandez

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
 
G

Guest

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 said:
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 said:
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("SQLConnection");
// Return "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=PasswordProtect;Data Source=(local)"
break;
case "access":
sConnect = ConfigurationSettings.AppSettings.Get("AccessConnection");
// 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("AccessConnection");
// 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top