Click here to Skip to main content
12,629,978 members (32,723 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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 23:03pm
Updated 5-Apr-11 23:04pm
v2
Comments
Wayne Gaylard 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 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 6-Apr-11 15:36pm
   
If the vote would be a static then default all the answers show a 5? :)
SAKryukov 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 6-Apr-11 17:24pm
   
Good points :)
SAKryukov 6-Apr-11 18:01pm
   
Thank you, Espen.
--SA
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  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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