Solved

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

Posted on 2006-06-12
7
2,989 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:
ciuly 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:ciuly
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:ciuly
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

12 Experts available now in Live!

Get 1:1 Help Now