I have a .Net mystery on my hands.
We received the following stack trace
---> System.InvalidCastException: Unable to cast object of type '' to type 'System.String'.
at System.Data.DataColumn.IsMaxLengthViolated()
at System.Data.DataTable.EnableConstraints()
at System.Data.DataTable.EndLoadData()
at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
The MSSQL database table that triggered this error contained only ints and nullable nvarchar columns of various lengths.
The table contained both DBNulls and string values.
No matter what we try we are unable to trigger an InvalidCastException where the first type's name is displayed as empty string '' We tried every type we imagine the db query could return.
- DBNull
- int
- int?
- SqlString
We always receive the correct type name in the InvalidCastException
We decompiled the IsMaxLengthViolated code suspecting it constructed its own InvalidCasetException with a malformed message but found only a normal cast
if (obj != null && obj != DBNull.Value && ((string) obj).Length > this.MaxLength)
This message was seemingly generated by the .Net framework.
Is there any known way to trigger such a malformed InvalidCastException that we are missing?