Click here to Skip to main content
15,671,597 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
hi guys...i have a static method which is called by javascript,
now i want to call another server side method from that static method..
 public static  string getData(string _mobileno)
            if (_mobileno == null || _mobileno.Length == 0)
                return String.Empty;
                OleDbConnection conn = null;
                string cnstring = ConfigurationSettings.AppSettings["ConnectionString"];
                conn = new OleDbConnection(cnstring);
                string _strQry = "Select distinct VISITOR_NAME from VISITOR_REG where MOBILE_NO=" + _mobileno+"";
                OleDbCommand cmd = new OleDbCommand(_strQry, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("MOBILE_NO", _mobileno);
                string name = Convert.ToString(cmd.ExecuteScalar());
                return name;


when i call this gives me error

An object reference is required for the non-static field, method, or property

help me solve this...
Updated 2-Jan-17 20:24pm
Wayne Gaylard 6-Apr-11 4:10am    
I don't know who univoted you, This doesn't deserve it.

The SetValue method would need to be declared static, or you need to create an instance of the class that the SetValue method belongs to, and call the method from the instance. In this case I think declaring SetValue as static would be the way to go.

Hope this helps
Share this answer
You cannot continue development until you learn what a type is and what is the instance. This is your problem.
Methods (non-static) methods work with instances. A static methods does not have a reference to an instance (passed implicitly as "this" to all instance methods), so the syntax of the call uses the name of the type. As a result, static methods cannot use instance methods (there is no known instance to pass). Same thing is with the properties (they are also methods, through accessors. A hint: the syntax for instance method call is: instanceVariableName.MethodName(methodParameters).

Share this answer
Albin Abel 6-Apr-11 15:36pm    
If the vote would be a static then default all the answers show a 5? :)
Sergey Alexandrovich Kryukov 6-Apr-11 15:43pm    
Thank you, but using but that technique would invite some bugs. Better keep it an instance field -- that would be safer.
Espen Harlinn 6-Apr-11 17:24pm    
Good points :)
Sergey Alexandrovich Kryukov 6-Apr-11 18:01pm    
Thank you, Espen.
Which class contains that method? Can you post the method definition for it? You need to access that method using that class object.
Share this answer

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