Solved

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

Posted on 2006-06-12
7
3,115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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