Solved

An object reference is required for the nonstatic field...

Posted on 2006-06-12
7
3,013 Views
Last Modified: 2008-02-07
hELLO EXPERTS,

I am getting the below error;-

Compiler Error Message: CS0120: An object reference is required for the nonstatic field, method, or property 'AuctionsDB.makeConnection()'

Source Error:

 

Line 36:     private static DataSet query(string sql)
Line 37:     {
Line 38:         OleDbConnection conn = makeConnection();
Line 39:         OleDbCommand cmd = new OleDbCommand(sql, conn);
Line 40:         OleDbDataAdapter adp = new OleDbDataAdapter();

Source File: c:\inetpub\wwwroot\test\App_Code\Class1.cs    Line: 38


I am doing the follwoing in code;-

public class TestDB
{
    //ACCESS OLEDb CONNECTION
    private OleDbConnection makeConnection()
    {
               //offline connection string
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Inetpub\\www_root\\test\\database\\testmdb";
        return new OleDbConnection(connStr);
    }

 

    //run SQLstatement on the database and return a result(i.e SELECT)
    private static DataSet query(string sql)
    {
        OleDbConnection conn = makeConnection();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataAdapter adp = new OleDbDataAdapter();
        adp.SelectCommand = cmd;
        DataSet ds = new DataSet();
        adp.Fill(ds, "Results");
        return ds;
    }

    //run SQLstatement on the database and DO NOT return a result (i.e INSERT, UPDATE, DELETE)
    private void update(string sql)
    {
        OleDbConnection conn = makeConnection();
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    public AuctionsDB() { }

    public static System.Data.DataSet GetVehiclesByMake(int VehicleType)
    {
      //sample return datatable query
       StringBuilder sb = new StringBuilder();
     sb.Append("SELECT * FROM TEST_VIEW");
       return query(sb.ToString());  
    }

   
}


I am using the tutorial at the following link as a guide;- http://www.asp.net/QuickStart/util/srcview.aspx?path=~/aspnet/samples/data/GridViewDAL.src&file=GridViewObject_cs\App_Code\AuthorsDB.cs&lang=C%23+Source.

But rather than create a connection for every single query, am trying to do this just once.

1. What am I doing wrong to cause the above error.
2. My  knowledge of classes is not excellent, i am just getting my head round it; So please also look through the rest of the code and point out any mistakes I may be making.

Please show example code for solution.


Many Thanks

Clara





0
Comment
Question by:claracruz
  • 3
  • 2
  • 2
7 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 16884959
1) everything that is to be used in a static method must be eitehr static or part of a reference object.
- if use a methos, thathmethod must be static. or you must create the object that holds the method and then use it through the reference
- if property, then teh property must be declared static (if it has a logic doing that. if it's incorrect, you need to revise the decision of making the original method static)
2) I'll do that in about 10 minutes :)
0
 
LVL 28

Expert Comment

by:2266180
ID: 16884999
first, after reading the code: according to good practice, there is no reason to make "query" method a static method. why did you make it static?
0
 
LVL 28

Expert Comment

by:2266180
ID: 16885011
also
public static System.Data.DataSet GetVehiclesByMake(int VehicleType)
should be
public static DataSet GetVehiclesByMake(int VehicleType) // no need for the namespace reference ;)

I see now the reason for the static :) I didn't look over that article before. I can't say that it's incorrect, but it's not a good practice. If you follow their design and do everything within the same method, then static can be usefull, otherwise, remain at using normal references: like

TestDB test = new TestDB();
test.query();

and methods and properties are non-static in TestDB.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Assisted Solution

by:PoeticAudio
PoeticAudio earned 250 total points
ID: 16885272
   //run SQLstatement on the database and return a result(i.e SELECT)
    private static DataSet query(string sql)
    {
        OleDbConnection conn = makeConnection();    <----------- THIS is definitely your problem.
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataAdapter adp = new OleDbDataAdapter();
        adp.SelectCommand = cmd;
        DataSet ds = new DataSet();
        adp.Fill(ds, "Results");
        return ds;
    }

You're calling makeConnection which is a non-static method... that will not work because MakeConnection requires an instance of your class, while query method doesn't (because it's static)
0
 
LVL 6

Expert Comment

by:PoeticAudio
ID: 16885281
oh, by the way, to solveit you would just need to have MakeConnection static
0
 
LVL 4

Author Comment

by:claracruz
ID: 16885574
thanks guys...

On this particular problem;-

first, after reading the code: according to good practice, there is no reason to make "query" method a static method. why did you make it static?

what do I replace this;-

private static DataSet query(string sql)

with...

I prefer to stick with good practice principles..
0
 
LVL 4

Author Comment

by:claracruz
ID: 16885666
hi guys,  Thanks for that...

got it
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now