user970225
user970225

Reputation: 145

Calling ConnectionString from another class in c#

I've added a connection string in web.config. I made a class where a string calls it -

namespace WebApplication1.Classes
{
    public class Connections
    {
        public string DBConn = 
            ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString;
    }
}

Now from my default.aspx.cs page I want to call this DBConn so I can use it in that page.

 namespace WebApplication1
 {
     public partial class _Default : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             Console.WriteLine(Classes.Connections.DBConn);
         }
     }
 }

The Classes.Connections.DBConn is not working. This is the error I get.

An object reference is required for the non-static field, method, or property 'WebApplication1.Classes.Connections.DBConn'

What am I doing wrong?

Upvotes: 1

Views: 7206

Answers (8)

Alex
Alex

Reputation: 35409

To access the connection string as your classes are currently written, do the following:

using WebApplication1.Classes;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Console.WriteLine(new Connections().DBConn);
        }
    }
}

It's a widely accepted practice to create a static class for application wide settings - similar to other examples shown.

Upvotes: 1

JotaBe
JotaBe

Reputation: 39004

You can do 2 things:

1) instance your class

Connections c = new Connections();

then use c.DbConn

2) make the method static

  namespace WebApplication1.Classes
  {
    public class Connections
    {
      public static string DBConn = 
          ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString;
    }
  }

Upvotes: 1

zimdanen
zimdanen

Reputation: 5626

In order to use a variable without an instance, it must be declared static. Read more on the static keyword here.

If there's no reason for your class to be instantiated either, you should mark both the class and the variable as static:

namespace WebApplication1.Classes 
{ 
 public static class Connections 
 { 
   public static string DBConn = ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString; 
 } 
}

The static on the DBConn variable allows it to be accessed from the class itself (i.e., without an instance). The static on the Connections class prevents the class from being instantiated, since there is no reason to do so.

Upvotes: 1

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

Since your variable isnt static, you need to instantiate the class Connections like this:

Connections x = new Connections();

Then you will be able to access it via

x.DBConn

Other solution would be to make your variable static

public static string DBConn=...

Upvotes: 1

Vano Maisuradze
Vano Maisuradze

Reputation: 5899

You need to use static keyword.

namespace WebApplication1.Classes
{
 public static class Connections
 {
   public static string DBConn = ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString;
 }
}

Upvotes: 1

FishBasketGordo
FishBasketGordo

Reputation: 23132

Your field isn't static. In order to call it like this:

Classes.Connections.DBConn // Using the class Connections, 
                           // not an instance of the class Connections

You have to declare the field like this:

public static string DBConn = "etc";

For such a class, it might behoove you to declare the whole class as static in fact. For more information, check out this article.

Upvotes: 3

PaulG
PaulG

Reputation: 7102

Try

public static string DBConn = ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString;  }

The reason it's not visible is because you have to make it static

Upvotes: 1

Kirk Woll
Kirk Woll

Reputation: 77546

To fix, pretty much just follow the error message by making it static instead:

public static string DBConn = ConfigurationManager.ConnectionStrings["HomeDB"].ConnectionString;

Since this expression Classes.Connections.DBConn is a static reference (you don't have an instance of Connectinos) you need to make it static if you want to access it in this fashion.

Upvotes: 1

Related Questions