user3127227
user3127227

Reputation: 37

retrieving one field from datatable C#, not working

I'm making a project where you see a list of movies in a listbox and there's a button with view movie description using listbox.value.

Below is my function to retrieve the description from a database. But i'm getting the following error:

Error 1 An object reference is required for the non-static field, method, or property 'Entertainment.FilmsDetails.filmId' H:\Visual Studio 2008\Projects\Entertainment\Entertainment\FilmsDetails.cs 43 39 Entertainment

 public static string DetailMovie(int movieId)
    {
        string command;
        command = "SELECT description" +
            "FROM MovieDescription" +
            " WHERE movieId=  " + movieId;
        OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString);
        DataTable datatable = new DataTable();
        adapter.Fill(datatable);

        object o = datatable.Rows[0]["description"];
    }

Upvotes: 1

Views: 83

Answers (1)

ohiodoug
ohiodoug

Reputation: 1513

The sql command you are passing to your db looks like the following:

SELECT descriptionFROM MovieDescription WHERE movieID= 1

Make sure to add a space between description and FROM

Start with that.

UPDATE

My best guess without seeing more code is that you are trying to access a property or member variable that is not static within your static method. The only variable that you aren't creating within the method itself is the connectionString, which if it isn't marked as static, would throw this compiler error.

// connectionString is possibly not static
OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionString);

Does your function need to be static, or can you make the connectionString static?

UPDATE 2

You also will need to return a value out of your function.

Upvotes: 3

Related Questions