Click here to Skip to main content
14,327,370 members
Rate this:
Please Sign up or sign in to vote.
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
Rate this:
Please Sign up or sign in to vote.

Solution 3

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

Albin Abel 6-Apr-11 15:36pm
If the vote would be a static then default all the answers show a 5? :)
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 :)
Thank you, Espen.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Which class contains that method? Can you post the method definition for it? You need to access that method using that class object.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100