Thread: Simple db problem

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    Simple db problem

    Code:
    OleDbConnection thisConnection = new OleDbConnection("provider = microsoft.jet.oledb.4.0;data source = people.mdb;");
                    OleDbCommand thisCommand = new OleDbCommand("SELECT ID FROM people",thisConnection);
                    thisConnection.Open();
                    OleDbDataReader thisReader = thisCommand.ExecuteReader();
                    Console.WriteLine("Product ID : {0}",
                    thisReader.GetValue(1));
    Can anyone help with this??? I want to read values from a db, but I have this problem...It throws an invalid operation: no data exists for the row column.I have a db with only one column named id, with two fields containing 1 and 2. What should I put on the GetValue argument???

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You have to call Read on the reader, once per row:
    Code:
    while(thisReader.Read())
    {
      object IndexedField = thisReader[0];
      object NamedField = thisReader["NamedField"];
    }
    If you're sure there's just one row you could replace "while" with "if" instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  2. Problem with DB handles
    By pokks in forum C Programming
    Replies: 4
    Last Post: 01-04-2006, 12:06 PM
  3. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  4. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM
  5. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM