Solved

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

Posted on 2006-06-12
7
3,092 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

734 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