views:

52

answers:

1

I am trying to use stored procedure in dbml, but I get below error. The Stored procedure has multiple join tables, but it returns a row.

public static List<SP_EMP_MASTER_DETAILResult> GetEmployeeDetail(string userName, string userLocation)
{
    var query = (from q in db.SP_EMP_MASTER_DETAIL(userLocation, userName)
                 select new SP_EMP_MASTER_DETAILResult { ID = q.EMP_ID, Name = q.EMP_NM }).ToList();
    return query;
}

This is an error.

An object reference is required for the non-static field, method, or property 'Tiger.Models.HomeRepository.db'

+2  A: 

Your method is static... IS your db variable static also? You can't reference a non-static class member in your static method.....

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

from http://msdn.microsoft.com/en-us/library/79b3xss3.aspx

klabranche
Can I use IQueryable?
Hoorayo
@Hoorayo: No. Until you make an effort to understand this, you cannot use anything.
Timwi
It's not a problem with linq-to-sql or your list or IQueryable. You have made your method "static" but your variable db is not static and can't be used in a static method.
klabranche
Timwi is absolutely right. Let me figure it out first, even though it sounds complicate for me.
Hoorayo
@Hoorayo: I love you! If only all beginners were like that :)
Timwi
Yes, I am just one month old C# beginner, so your any help will be great. I guess I should not use static since db variable is not a static.
Hoorayo
OR pass in the db variable as a parameter.....
klabranche
You might also want to mention that this is a compiler error, then it's easier for us to figure out what's going on
Jaco Pretorius
Sorry. Yes, it was compiler error.
Hoorayo