3

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?

  • I am sorry if you have already considered but it seems to me it seems like trying to cast char into string. – Hasan Emrah Süngü May 09 '17 at 07:28
  • Do you have access to the Database? Because is obviously a value in the datatable that .Net can't cast to string and can't determine its type. Maybe if you study all the values you could find which one is. Anyway, is really strange indeed – Pikoh May 09 '17 at 07:51

1 Answers1

-1

Replace this

if (obj != null && obj != DBNull.Value && ((string) obj).Length > this.MaxLength)

with

if (obj != null && obj != DBNull.Value && (Convert.ToString(obj)).Length > this.MaxLength)

UPDATE

This question is already posted visit the following link: [InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.]

The answer given in above link is to do toString with the obj but best practice is Convert.Tostring() to handle null exceptions.

I hope this will solve your problem.

Community
  • 1
  • 1
SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19
  • That code is [part of the .Net framework](http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/Data/System/Data/DataColumn@cs/1305376/DataColumn@cs). I don't think OP wants to modify anything there – Pikoh May 09 '17 at 07:55
  • http://stackoverflow.com/questions/21216641/invalidcastexception-unable-to-cast-object-of-type-system-dbnull-to-type-sy – SynozeN Technologies May 09 '17 at 08:00
  • Nice link. Can you tell me how can you change that in .Net Framework source code? – Pikoh May 09 '17 at 08:02
  • The issue cannot be related to DBNull as the InvalidCastException would include say that the from type in the InvalidCast is 'System.DBNull' and the if statement includes protection against DBNull. Somehow we received an object from the data reader that has no type name. We're trying to work out how this happened. – Steven Pearson May 10 '17 at 08:53