{"record":{"id":"4132a44bd48776ef","repo":"tursodatabase/turso","slug":"column-ordinal-ordinal-is-out-of-range-4132a4","errorCode":null,"errorMessage":"column ordinal {ordinal} is out of range","messagePattern":"column ordinal (.+?) is out of range","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoDataReader.cs","lineNumber":334,"sourceCode":"        };\n    }\n\n    private void EnsureOpen()\n    {\n        if (IsClosed)\n            throw new InvalidOperationException(\"The data reader is closed.\");\n    }\n\n    private void RunExternalIo()\n    {\n        _syncConnection?.RunExternalIo();\n    }\n\n    private void ValidateOrdinal(int ordinal)\n    {\n        ArgumentOutOfRangeException.ThrowIfNegative(ordinal);\n        if (ordinal >= FieldCount)\n            throw new IndexOutOfRangeException($\"column ordinal {ordinal} is out of range\");\n    }\n}\n\ninternal static class DataReaderCompatibility\n{\n    public static DataTable CreateSchemaTable(DbDataReader reader)\n    {\n        var schema = new DataTable(\"SchemaTable\");\n        schema.Columns.Add(SchemaTableColumn.ColumnName, typeof(string));\n        schema.Columns.Add(SchemaTableColumn.ColumnOrdinal, typeof(int));\n        schema.Columns.Add(SchemaTableColumn.ColumnSize, typeof(int));\n        schema.Columns.Add(SchemaTableColumn.NumericPrecision, typeof(short));\n        schema.Columns.Add(SchemaTableColumn.NumericScale, typeof(short));\n        schema.Columns.Add(SchemaTableColumn.IsUnique, typeof(bool));\n        schema.Columns.Add(SchemaTableColumn.IsKey, typeof(bool));\n        schema.Columns.Add(\"BaseServerName\", typeof(string));\n        schema.Columns.Add(\"BaseCatalogName\", typeof(string));\n        schema.Columns.Add(SchemaTableColumn.BaseColumnName, typeof(string));","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data/TursoDataReader.cs#L316-L352","documentation":"ValidateOrdinal guards column-index access on TursoDataReader: negative ordinals or ordinals >= FieldCount throw IndexOutOfRangeException with the message 'column ordinal {ordinal} is out of range'. It backs GetDataTypeName and GetFieldType.","triggerScenarios":"Calling GetFieldType(ordinal)/GetDataTypeName(ordinal) with an index beyond the result set's column count or a negative value — typically from hardcoded ordinals, stale ordinal caches, or iterating past the schema's column count.","commonSituations":"Hardcoding column indices after changing the SELECT list; reusing ordinals captured from a previous query's schema; mapping DTOs with more fields than the query selects.","solutions":["Resolve ordinals at runtime with reader.GetOrdinal(\"columnName\") instead of hardcoding indices.","Check ordinal >= 0 && ordinal < reader.FieldCount before access.","Fix the SELECT statement so it returns the expected number of columns."],"exampleFix":"// before\nvar type = reader.GetFieldType(5); // query only returns 3 columns\n// after\nint ord = reader.GetOrdinal(\"created_at\");\nvar type = reader.GetFieldType(ord);","handlingStrategy":"validation","validationCode":"if (ordinal >= 0 && ordinal < reader.FieldCount)\n    return reader.GetFieldType(ordinal);","typeGuard":"static bool IsValidOrdinal(DbDataReader r, int ordinal) => ordinal >= 0 && ordinal < r.FieldCount;","tryCatchPattern":"try { return reader.GetFieldType(ordinal); }\ncatch (IndexOutOfRangeException ex) when (ex.Message.Contains(\"column ordinal\"))\n{ throw new DataException($\"Ordinal {ordinal} invalid; FieldCount={reader.FieldCount}\", ex); }","preventionTips":["Use GetOrdinal(name) instead of hardcoded indices.","Recompute ordinals whenever the SELECT list changes.","Add Debug.Assert(ordinal < reader.FieldCount) in mapping code."],"tags":["csharp","dotnet","datareader","bounds"],"backgroundTag":"index-out-of-range","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-09-06T07:15:26.990Z","contentChangedAt":"2026-09-06T07:15:26.990Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}