[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-06-12
7
Medium Priority
?
3,226 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Assisted Solution

by:PoeticAudio
PoeticAudio earned 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

868 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