Click here to Skip to main content
Rate this: bad
good
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;
            }
            else
            {
                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+"";
                conn.Open();
                OleDbCommand cmd = new OleDbCommand(_strQry, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("MOBILE_NO", _mobileno);
                string name = Convert.ToString(cmd.ExecuteScalar());
                setvalue(_mobileno);
                return name;
                
 
}
 
when i call this setvalue..it gives me error
 
An object reference is required for the non-static field, method, or property
 
help me solve this...
Posted 5-Apr-11 22:03pm
Edited 5-Apr-11 22:04pm
v2
Comments
Wayne Gaylard at 6-Apr-11 4:10am
   
I don't know who univoted you, This doesn't deserve it.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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
  Permalink  
Rate this: bad
good
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).
 
—SA
  Permalink  
Comments
Albin Abel at 6-Apr-11 15:36pm
   
If the vote would be a static then default all the answers show a 5? :)
SAKryukov at 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. --SA
Espen Harlinn at 6-Apr-11 17:24pm
   
Good points :)
SAKryukov at 6-Apr-11 18:01pm
   
Thank you, Espen. --SA

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 576
1 Sergey Alexandrovich Kryukov 230
2 Tadit Dash 215
3 OriginalGriff 183
4 Peter Leow 130
0 Sergey Alexandrovich Kryukov 9,325
1 OriginalGriff 5,473
2 Peter Leow 4,150
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid