Click here to Skip to main content
15,506,385 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

This is my code for Configuration.cs

C#
namespace DataAccess
{
    public class Connection
    {
        public static Social_NetwokingEntities GetContext()
        {
            Social_NetwokingEntities fdc = new Social_NetwokingEntities(ConfigurationManager.ConnectionStrings["FisharooConnectionString"].ToString());
            return fdc;
        }
    }
}


and the code for another file using this file is :
C#
private Connection conn;

        public AccountRepository()
        {
            conn=new Connection();
        }

         
        public Accounts GetAccountByID(int AccountID)
        {
            Accounts account = null;
            using (Social_NetwokingEntities dc = conn.GetContext())
            {
                account = (from a in dc.Accounts where a.AccountID == AccountID select a).First();
            }
            return account;
        }


when i type
C#
using(Social_NetworkingEntities dc = conn.GetContext()) 
i get an error "cannot be accessed by an instance, qualify it with a type.
i dont understand what is the problem..
please help me.

Thank you.
Posted
Updated 11-May-12 15:36pm
v2

I know this is not the answer to your question, but wanted you to know that your Linq statement could be much improved:

account = dc.Account.FirstOrDefault(i => i.AccountID == AccountID);

Not sure what your real problem is though.
 
Share this answer
 
Comments
Sipherz 11-May-12 22:53pm    
anyways thanx!
VJ Reddy 12-May-12 0:21am    
Good point. 5!
Sandeep Mewara 12-May-12 3:58am    
My 5!
The reason is that in the Class
C#
public class Connection
    {
        public static Social_NetwokingEntities GetContext()
        {
            Social_NetwokingEntities fdc = new Social_NetwokingEntities(ConfigurationManager.ConnectionStrings["FisharooConnectionString"].ToString());
            return fdc;
        }
    }

the GetContext is declared as a static method, so it can be accessed through the Type Connection and not through the instance of the class Connection. Hence, the above error message is displayed.
Further, I think the following code may suffice instead of the second code block mentioned in the question
C#
public Accounts GetAccountByID(int AccountID)
{
    using (Social_NetwokingEntities dc = Connection.GetContext())
    {
        return dc.Accounts.FirstOrDefault(a => a.AccountID == AccountID);
    }
}
 
Share this answer
 
Comments
Sandeep Mewara 12-May-12 3:58am    
5!
VJ Reddy 12-May-12 4:06am    
Thank you, Sandeep.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900