public partial class Prod : System.Web.UI.Page
{

}

Here i have another class file called clsdbconnection which has connection string function called getconnectiondb().

To use that getconnectiondb() function i need to use the class in the present class prod.

How to use the class, I tried all these ways but it gives error

public partial class Prod : clsdbconnection : System.Web.UI.Page
{

}

public partial class Prod , clsdbconnection : System.Web.UI.Page
{

}

public partial class Prod : clsdbconnection , System.Web.UI.Page
{

}

Dani AI

Generated

The compile errors come from trying to make a page inherit two classes. C# allows single class inheritance (a page can only inherit one base class); that is why the attempts in the first post failed. , and pointed toward composition instead of inheritance — that direction is correct. See the C# inheritance overview for the language rule and rationale: C# inheritance docs.

Practical options that fit Web Forms patterns:

  • Keep the DB helper as a separate, compiled class and call it from the page (composition). Alternatively make the helper method static if it truly has no instance state. Consider defining an interface for the helper if swapping implementations or unit-testing is desired.
  • Store the actual connection string in the application configuration and read it at runtime instead of hard-coding credentials. For example, use ConfigurationManager.ConnectionStrings[...] to retrieve the configured string; see the ConfigurationManager reference: ConfigurationManager docs.

Common troubleshooting checklist (causes of “class not found” or visibility errors):

  • Confirm the helper class is public and in an accessible namespace; add the appropriate using or fully-qualify the type.
  • Ensure the class is compiled into the app (App_Code for Web Forms or a referenced class library).
  • Avoid naming collisions and duplicated class names.
  • Decide whether the helper returns a connection string or an already-open connection; if returning a live connection, ensure callers always dispose it properly (use the language using pattern) — see using statement.

These clarifications build on the replies from this thread: composition is the right approach, but applying the patterns above will avoid common pitfalls and improve maintainability.

Recommended Answers

All 3 Replies

Use code tags

You can't inherit classes in this manner. Create an instance of class clsdbconnection.

Hi,
I think you want to implement your application as a three tier
architecture.
i.e., seperating presentation, business logic and data access
Well i can suggest you some other way.

first you have created a class called Prod

public partial class Prod : System.Web.UI.Page
{

}

Now create a class file clsdbconnection .
when you create class file you will be prompted to save the file in App_Code folder.click ok and that's it.youc can now define the function
getconnectiondb() in the class file.The function can now be accessed
from the class Prod.

In the Page_Load of Prod class create an instance of clsdbconnection

clsdbconnection db=new clsdbconnection ();

call the function and store the value returned by the function in
appropriate variable.


All The Best

Here i have another class file called clsdbconnection which has connection string function called getconnectiondb().

To use that getconnectiondb() function i need to use the class in the present class prod.

How to use the class, I tried all these ways but it gives error

To use the methods/functions of class, you don't need to inherit that class in your present class.

You need to create an instance of the class and call the appropriate method.

For example, to call the getconnectiondb of clsdbconnection in Prod class

clsdbconnection  dbConn = new clsdbconnection ();
dbConn.getconnectiondb ();

If getconnectiondb returns a value, then store it in a appropriate object variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.