1

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?

3
  • @PaulG - I was wondering the same thing. The question is clear and has all the details to answer it. Commented May 4, 2012 at 19:47
  • I dont know either. Was there a similar question already, cause I looked and didnt find one Commented May 4, 2012 at 19:48
  • From the available options of what makes a downvote, the most reasonable is "no research effort". Type "An object reference is required for the non-static field, method, or property" into google and guess whether the answer is on the first link available or not :) Commented May 4, 2012 at 21:29

8 Answers 8

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

Try

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

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

1 Comment

Sounds like something else went wrong (maybe a DB error), you should use Try Catch and try to zero in on the problem.
1

You need to use static keyword.

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

Comments

1

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

Comments

1

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.

Comments

1

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;
    }
  }

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.