Search code examples
c#sqlms-accessoledb

'No data exists for the row/column.' Oledb Exception


connection.Open();
OleDbCommand command = new OleDbCommand("SELECT [Names] FROM Test", 
connection);

OleDbDataReader reader = command.ExecuteReader();
string result = reader.GetValue(0).ToString();
        MessageBox.Show(result);
        connection.Close();

Could anyone help? I'm getting 'No data exists for the row/column.' this error thrown


Solution

  • You are not calling Read Method

        OleDbDataReader reader = command.ExecuteReader();
        if(reader.Read())
        {
           string result = reader.GetValue(0).ToString();
           MessageBox.Show(result);
         }
        connection.Close();
    

    This will just read the first row from the result.If you want all the rows then you will need to write some thing like this

        OleDbDataReader reader = command.ExecuteReader();        
        List<string> data = new List<string>();
        while(reader.Read())
        {
          data.Add(reader.GetValue(0).ToString());
        }        
        connection.Close();