System.InvalidCastException RRS feed

  • Question


    I am getting the following message:  "System.InvalidCastException:  Cast from type 'DBNull' to type 'String' is not valid.  at Microsoft.VisualBasic.CompilerServices.StringType.FromObject(Object value) at ...."


    This has all of a sudden started appearing in two separate applicaitons, one build on .net 1.1 and the other on .net 2.0 and only in one site where the apps are installed.  I can run the app from anywhere else and there is no problem.  I wrote some code to provide messages as to exactly where it was failing and it makes no sense.  There are not any null values being assigned to strings.


    I can run the app from within visual studio IDE and connect to clients database and there is no error when i run it that way.  I have re-deployed the same version that i ran from IDE to client and I get the error.


    Any help would be greatly appreciated, clients and boss are upset.



    Wednesday, September 12, 2007 2:19 PM

All replies

  • You need to make sure you are checking for DBNull in areas where Null can exist.  IsDbNull()
    This can be in many places.  It exists somewhere otherwise you wouldn't get the error.

    You can get this error if you use a datareader and don't check for DBNull

    Dim FIeld As String = dbReader("FieldName")

    or use a dataset and don't check for DBNulll

    Dim answer As String = "The answer is " & ds.Table(0).Rows(0).Item("Field")

    Or perhaps a control (like some INfragistics controls) returns DBNull.Value for its Null condition

    Me.TextBox.Text = Me.NUmericControl.Value & " is somethign"

    Wednesday, September 12, 2007 2:30 PM